curl 'localhost:9200/bank/_search?q=*&pretty' { "took" : 10, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1000, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "1", "_score" : 1.0, "_source":{"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"[email protected]","city":"Brogan","state":"IL"} }, { "_index" : "bank", "_type" : "account", "_id" : "6", "_score" : 1.0, "_source":{"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"[email protected]","city":"Dante","state":"TN"} }, { "_index" : "bank", "_type" : "account", "_id" : "13", "_score" : 1.0, "_source":{"account_number":13,"balance":32838,"firstname":"Nanette","lastname":"Bates","age":28,"gender":"F","address":"789 Madison Street","employer":"Quility","email":"[email protected]","city":"Nogal","state":"VA"} }, { "_index" : "bank", "_type" : "account", "_id" : "18", "_score" : 1.0, "_source":{"account_number":18,"balance":4180,"firstname":"Dale","lastname":"Adams","age":33,"gender":"M","address":"467 Hutchinson Court","employer":"Boink","email":"[email protected]","city":"Orick","state":"MD"} }, { "_index" : "bank", "_type" : "account", "_id" : "20", "_score" : 1.0, "_source":{"account_number":20,"balance":16418,"firstname":"Elinor","lastname":"Ratliff","age":36,"gender":"M","address":"282 Kings Place","employer":"Scentric","email":"[email protected]","city":"Ribera","state":"WA"} }, { "_index" : "bank", "_type" : "account", "_id" : "25", "_score" : 1.0, "_source":{"account_number":25,"balance":40540,"firstname":"Virginia","lastname":"Ayala","age":39,"gender":"F","address":"171 Putnam Avenue","employer":"Filodyne","email":"[email protected]","city":"Nicholson","state":"PA"} }, { "_index" : "bank", "_type" : "account", "_id" : "32", "_score" : 1.0, "_source":{"account_number":32,"balance":48086,"firstname":"Dillard","lastname":"Mcpherson","age":34,"gender":"F","address":"702 Quentin Street","employer":"Quailcom","email":"[email protected]","city":"Veguita","state":"IN"} }, { "_index" : "bank", "_type" : "account", "_id" : "37", "_score" : 1.0, "_source":{"account_number":37,"balance":18612,"firstname":"Mcgee","lastname":"Mooney","age":39,"gender":"M","address":"826 Fillmore Place","employer":"Reversus","email":"[email protected]","city":"Tooleville","state":"OK"} }, { "_index" : "bank", "_type" : "account", "_id" : "44", "_score" : 1.0, "_source":{"account_number":44,"balance":34487,"firstname":"Aurelia","lastname":"Harding","age":37,"gender":"M","address":"502 Baycliff Terrace","employer":"Orbalix","email":"[email protected]","city":"Yardville","state":"DE"} }, { "_index" : "bank", "_type" : "account", "_id" : "49", "_score" : 1.0, "_source":{"account_number":49,"balance":29104,"firstname":"Fulton","lastname":"Holt","age":23,"gender":"F","address":"451 Humboldt Street","employer":"Anocha","email":"[email protected]","city":"Sunriver","state":"RI"} } ] } }
took参数表示的是话费的时间为10ms,
time_out表示这次查询是否设置了超时
_share告诉我们搜索了5个分片, 5个搜索成功了,0个失败
hits表示搜出的结果
total表示搜出了1000个结果
hits表示当前展示的结果,默认只有10条
上述那种方式可以用以下方式替换
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} } }'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "from":10, "size":10 }'
要注意条数是从0开始的
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "sort":{"balance":{"order":"desc"}} }'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_all": {} }, "_source":["account_number","balance"] }'
注意用方括号[]来括起两个字段
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": {"account_number":20} } }' { "took" : 40, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "20", "_score" : 1.0, "_source":{"account_number":20,"balance":16418,"firstname":"Elinor","lastname":"Ratliff","age":36,"gender":"M","address":"282 Kings Place","employer":"Scentric","email":"[email protected]","city":"Ribera","state":"WA"} } ] } }
可以看到之匹配account_number为20的数据,
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": {"address":"mill"} } }' { "took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 4, "max_score" : 2.8025851, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "970", "_score" : 2.8025851, "_source":{"account_number":970,"balance":19648,"firstname":"Forbes","lastname":"Wallace","age":28,"gender":"M","address":"990 Mill Road","employer":"Pheast","email":"[email protected]","city":"Lopezo","state":"AK"} }, { "_index" : "bank", "_type" : "account", "_id" : "136", "_score" : 2.4560115, "_source":{"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"[email protected]","city":"Urie","state":"IL"} }, { "_index" : "bank", "_type" : "account", "_id" : "345", "_score" : 2.4560115, "_source":{"account_number":345,"balance":9812,"firstname":"Parker","lastname":"Hines","age":38,"gender":"M","address":"715 Mill Avenue","employer":"Baluba","email":"[email protected]","city":"Blackgum","state":"KY"} }, { "_index" : "bank", "_type" : "account", "_id" : "472", "_score" : 2.4560115, "_source":{"account_number":472,"balance":25571,"firstname":"Lee","lastname":"Long","age":32,"gender":"F","address":"288 Mill Street","employer":"Comverges","email":"[email protected]","city":"Movico","state":"MT"} } ] } }
可以发现address只要包含mill就匹配出来,而且不区分大小写,但是之前我们设定account_number为20的,那么像200这样的应该也可以搜出来的啊?
这个其实是只要是字符串那么就是做匹配包含,如果是数值类型那么就是必须相等.
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match": {"address":"mill lane"} } }' { "took" : 15, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 19, "max_score" : 3.3953483, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "136", "_score" : 3.3953483, "_source":{"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"[email protected]","city":"Urie","state":"IL"} }, { "_index" : "bank", "_type" : "account", "_id" : "970", "_score" : 1.0273236, "_source":{"account_number":970,"balance":19648,"firstname":"Forbes","lastname":"Wallace","age":28,"gender":"M","address":"990 Mill Road","employer":"Pheast","email":"[email protected]","city":"Lopezo","state":"AK"} }, { "_index" : "bank", "_type" : "account", "_id" : "345", "_score" : 0.888273, "_source":{"account_number":345,"balance":9812,"firstname":"Parker","lastname":"Hines","age":38,"gender":"M","address":"715 Mill Avenue","employer":"Baluba","email":"[email protected]","city":"Blackgum","state":"KY"} }, { "_index" : "bank", "_type" : "account", "_id" : "472", "_score" : 0.888273, "_source":{"account_number":472,"balance":25571,"firstname":"Lee","lastname":"Long","age":32,"gender":"F","address":"288 Mill Street","employer":"Comverges","email":"[email protected]","city":"Movico","state":"MT"} }, { "_index" : "bank", "_type" : "account", "_id" : "449", "_score" : 0.88407093, "_source":{"account_number":449,"balance":41950,"firstname":"Barnett","lastname":"Cantrell","age":39,"gender":"F","address":"945 Bedell Lane","employer":"Zentility","email":"[email protected]","city":"Swartzville","state":"ND"} }, { "_index" : "bank", "_type" : "account", "_id" : "742", "_score" : 0.88407093, "_source":{"account_number":742,"balance":24765,"firstname":"Merle","lastname":"Wooten","age":26,"gender":"M","address":"317 Pooles Lane","employer":"Tropolis","email":"[email protected]","city":"Bentley","state":"ND"} }, { "_index" : "bank", "_type" : "account", "_id" : "231", "_score" : 0.8275912, "_source":{"account_number":231,"balance":46180,"firstname":"Essie","lastname":"Clarke","age":34,"gender":"F","address":"308 Harbor Lane","employer":"Pharmacon","email":"[email protected]","city":"Fillmore","state":"MS"} }, { "_index" : "bank", "_type" : "account", "_id" : "921", "_score" : 0.8275912, "_source":{"account_number":921,"balance":49119,"firstname":"Barbara","lastname":"Wade","age":29,"gender":"M","address":"687 Hoyts Lane","employer":"Roughies","email":"[email protected]","city":"Sattley","state":"CO"} }, { "_index" : "bank", "_type" : "account", "_id" : "631", "_score" : 0.80940115, "_source":{"account_number":631,"balance":21657,"firstname":"Corrine","lastname":"Barber","age":32,"gender":"F","address":"447 Hunts Lane","employer":"Quarmony","email":"[email protected]","city":"Wyano","state":"IL"} }, { "_index" : "bank", "_type" : "account", "_id" : "908", "_score" : 0.80940115, "_source":{"account_number":908,"balance":45975,"firstname":"Mosley","lastname":"Holloway","age":31,"gender":"M","address":"929 Eldert Lane","employer":"Anivet","email":"[email protected]","city":"Biehle","state":"MS"} } ] } }
可以发现查出了19条数据,这种方式可以用下面这种方式替换
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "match_phrase": {"address":"mill lane"} } }' { "took" : 6, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 4.8004513, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "136", "_score" : 4.8004513, "_source":{"account_number":136,"balance":45801,"firstname":"Winnie","lastname":"Holland","age":38,"gender":"M","address":"198 Mill Lane","employer":"Neteria","email":"[email protected]","city":"Urie","state":"IL"} } ] } }
因此只需要把match替换成match_phrase即可.
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
可以发现和上述结果一样.
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }'
可以发现有981条
curl -XPOST 'localhost:9200/bank/_search?pretty' -d ' { "query": { "bool": { "must": [ { "match": { "age": "40" } } ], "must_not": [ { "match": { "state": "ID" } } ] } } }' { "took" : 6, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 43, "max_score" : 1.0, "hits" : [ { "_index" : "bank", "_type" : "account", "_id" : "474", "_score" : 1.0, "_source":{"account_number":474,"balance":35896,"firstname":"Obrien","lastname":"Walton","age":40,"gender":"F","address":"192 Ide Court","employer":"Suremax","email":"[email protected]","city":"Crucible","state":"UT"} }, { "_index" : "bank", "_type" : "account", "_id" : "479", "_score" : 1.0, "_source":{"account_number":479,"balance":31865,"firstname":"Cameron","lastname":"Ross","age":40,"gender":"M","address":"904 Bouck Court","employer":"Telpod","email":"[email protected]","city":"Nord","state":"MO"} }, { "_index" : "bank", "_type" : "account", "_id" : "549", "_score" : 1.0, "_source":{"account_number":549,"balance":1932,"firstname":"Jacqueline","lastname":"Maxwell","age":40,"gender":"M","address":"444 Schenck Place","employer":"Fuelworks","email":"[email protected]","city":"Oretta","state":"OR"} }, { "_index" : "bank", "_type" : "account", "_id" : "878", "_score" : 1.0, "_source":{"account_number":878,"balance":49159,"firstname":"Battle","lastname":"Blackburn","age":40,"gender":"F","address":"234 Hendrix Street","employer":"Zilphur","email":"[email protected]","city":"Wanamie","state":"PA"} }, { "_index" : "bank", "_type" : "account", "_id" : "885", "_score" : 1.0, "_source":{"account_number":885,"balance":31661,"firstname":"Valdez","lastname":"Roberson","age":40,"gender":"F","address":"227 Scholes Street","employer":"Delphide","email":"[email protected]","city":"Chilton","state":"MT"} }, { "_index" : "bank", "_type" : "account", "_id" : "948", "_score" : 1.0, "_source":{"account_number":948,"balance":37074,"firstname":"Sargent","lastname":"Powers","age":40,"gender":"M","address":"532 Fiske Place","employer":"Accuprint","email":"[email protected]","city":"Umapine","state":"AK"} }, { "_index" : "bank", "_type" : "account", "_id" : "998", "_score" : 1.0, "_source":{"account_number":998,"balance":16869,"firstname":"Letha","lastname":"Baker","age":40,"gender":"F","address":"206 Llama Court","employer":"Dognosis","email":"[email protected]","city":"Dunlo","state":"WV"} }, { "_index" : "bank", "_type" : "account", "_id" : "40", "_score" : 1.0, "_source":{"account_number":40,"balance":33882,"firstname":"Pace","lastname":"Molina","age":40,"gender":"M","address":"263 Ovington Court","employer":"Cytrak","email":"[email protected]","city":"Silkworth","state":"OR"} }, { "_index" : "bank", "_type" : "account", "_id" : "165", "_score" : 1.0, "_source":{"account_number":165,"balance":18956,"firstname":"Sims","lastname":"Mckay","age":40,"gender":"F","address":"205 Jackson Street","employer":"Comtour","email":"[email protected]","city":"Tilden","state":"DC"} }, { "_index" : "bank", "_type" : "account", "_id" : "177", "_score" : 1.0, "_source":{"account_number":177,"balance":48972,"firstname":"Harris","lastname":"Gross","age":40,"gender":"F","address":"468 Suydam Street","employer":"Kidstock","email":"[email protected]","city":"Yettem","state":"KY"} } ] } }
可以发现查询到了43条