搜索文档:
1、返回特定字段:
如果不希望返回整个源文档,可以只返回源中的那几个字段
如下命令
curl -XGET "localhost:9200/bank/_search?pretty" '
> {
> "query":{"match_all":{}},
> "_source":["account_number","balance"]
> }'
这段请求体里面的内容就类似于select中
select account_number,balance from bank;
但是如果按照如上命令就会出现下图状况,指定字段无效
所以要按照如下指令
curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d '
{
"query":{"match_all":{}},
"_source": ["account_number","balance"]
}'
curl -XGET "localhost:9200/bank/_search?pretty" '
{
"query":{"match":{"account_number":20}}}'
这段请求体中的条件类似于sql中的如下语句
select * from bank where account_number=20
同上面一样命令没有指定参数,所以无法显示完全,但是有些书上或者博客上是没有指明出来的,所以避免错误,建议直接打全
结果如下
语句如下
curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d '
{
"query":{"match":{"account_number":20}}}'
然后是返回address字段中包含词mill的所有账户信息
下面的语句类似于
select * from banl where address like “%mill%”
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d '
{
"query":{"match":{"address":"mill"}}}'
下面是返回的结果
{
"took" : 35,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 5.4032025,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "970",
"_score" : 5.4032025,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "136",
"_score" : 5.4032025,
"_source" : {
"account_number" : 136,
"balance" : 45801,
"firstname" : "Winnie",
"lastname" : "Holland",
"age" : 38,
"gender" : "M",
"address" : "198 Mill Lane",
"employer" : "Neteria",
"email" : "winnieholland@neteria.com",
"city" : "Urie",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "345",
"_score" : 5.4032025,
"_source" : {
"account_number" : 345,
"balance" : 9812,
"firstname" : "Parker",
"lastname" : "Hines",
"age" : 38,
"gender" : "M",
"address" : "715 Mill Avenue",
"employer" : "Baluba",
"email" : "parkerhines@baluba.com",
"city" : "Blackgum",
"state" : "KY"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "472",
"_score" : 5.4032025,
"_source" : {
"account_number" : 472,
"balance" : 25571,
"firstname" : "Lee",
"lastname" : "Long",
"age" : 32,
"gender" : "F",
"address" : "288 Mill Street",
"employer" : "Comverges",
"email" : "leelong@comverges.com",
"city" : "Movico",
"state" : "MT"
}
}
]
}
}
3、布尔查询
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
> "query": {
> "bool": {
> "must":[
> {"match":{"address":"mill"}},
> {"match":{"address":"lane"}}
> ]
> }
> }
> }'
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 9.507477,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "136",
"_score" : 9.507477,
"_source" : {
"account_number" : 136,
"balance" : 45801,
"firstname" : "Winnie",
"lastname" : "Holland",
"age" : 38,
"gender" : "M",
"address" : "198 Mill Lane",
"employer" : "Neteria",
"email" : "winnieholland@neteria.com",
"city" : "Urie",
"state" : "IL"
}
}
]
}
}
这段命令的意思,用sql来理解一下就是
select * from bank where address contains “mill” and contains “lane”;
must 就是必须同时满足
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
"query": {
"bool": {
"should":[
{"match":{"address":"mill"}},
{"match":{"address":"lane"}}
]
}
}
}'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 19,
"relation" : "eq"
},
"max_score" : 9.507477,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "136",
"_score" : 9.507477,
"_source" : {
"account_number" : 136,
"balance" : 45801,
"firstname" : "Winnie",
"lastname" : "Holland",
"age" : 38,
"gender" : "M",
"address" : "198 Mill Lane",
"employer" : "Neteria",
"email" : "winnieholland@neteria.com",
"city" : "Urie",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "970",
"_score" : 5.4032025,
"_source" : {
"account_number" : 970,
"balance" : 19648,
"firstname" : "Forbes",
"lastname" : "Wallace",
"age" : 28,
"gender" : "M",
"address" : "990 Mill Road",
"employer" : "Pheast",
"email" : "forbeswallace@pheast.com",
"city" : "Lopezo",
"state" : "AK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "345",
"_score" : 5.4032025,
"_source" : {
"account_number" : 345,
"balance" : 9812,
"firstname" : "Parker",
"lastname" : "Hines",
"age" : 38,
"gender" : "M",
"address" : "715 Mill Avenue",
"employer" : "Baluba",
"email" : "parkerhines@baluba.com",
"city" : "Blackgum",
"state" : "KY"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "472",
"_score" : 5.4032025,
"_source" : {
"account_number" : 472,
"balance" : 25571,
"firstname" : "Lee",
"lastname" : "Long",
"age" : 32,
"gender" : "F",
"address" : "288 Mill Street",
"employer" : "Comverges",
"email" : "leelong@comverges.com",
"city" : "Movico",
"state" : "MT"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "1",
"_score" : 4.1042743,
"_source" : {
"account_number" : 1,
"balance" : 39225,
"firstname" : "Amber",
"lastname" : "Duke",
"age" : 32,
"gender" : "M",
"address" : "880 Holmes Lane",
"employer" : "Pyrami",
"email" : "amberduke@pyrami.com",
"city" : "Brogan",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "70",
"_score" : 4.1042743,
"_source" : {
"account_number" : 70,
"balance" : 38172,
"firstname" : "Deidre",
"lastname" : "Thompson",
"age" : 33,
"gender" : "F",
"address" : "685 School Lane",
"employer" : "Netplode",
"email" : "deidrethompson@netplode.com",
"city" : "Chestnut",
"state" : "GA"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "556",
"_score" : 4.1042743,
"_source" : {
"account_number" : 556,
"balance" : 36420,
"firstname" : "Collier",
"lastname" : "Odonnell",
"age" : 35,
"gender" : "M",
"address" : "591 Nolans Lane",
"employer" : "Sultraxin",
"email" : "collierodonnell@sultraxin.com",
"city" : "Fulford",
"state" : "MD"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "568",
"_score" : 4.1042743,
"_source" : {
"account_number" : 568,
"balance" : 36628,
"firstname" : "Lesa",
"lastname" : "Maynard",
"age" : 29,
"gender" : "F",
"address" : "295 Whitty Lane",
"employer" : "Coash",
"email" : "lesamaynard@coash.com",
"city" : "Broadlands",
"state" : "VT"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "715",
"_score" : 4.1042743,
"_source" : {
"account_number" : 715,
"balance" : 23734,
"firstname" : "Tammi",
"lastname" : "Hodge",
"age" : 24,
"gender" : "M",
"address" : "865 Church Lane",
"employer" : "Netur",
"email" : "tammihodge@netur.com",
"city" : "Lacomb",
"state" : "KS"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "449",
"_score" : 4.1042743,
"_source" : {
"account_number" : 449,
"balance" : 41950,
"firstname" : "Barnett",
"lastname" : "Cantrell",
"age" : 39,
"gender" : "F",
"address" : "945 Bedell Lane",
"employer" : "Zentility",
"email" : "barnettcantrell@zentility.com",
"city" : "Swartzville",
"state" : "ND"
}
}
]
}
}
如上命令将must换成了should,那么其含义就是
select * from bank where address contains “mill” or contains “lane”;
多个条件中只需要满足一个即可,即为或者的意思
下面来一个组合实例
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
"query":{
"bool": {
"must":[
{"match":{"age":"40"}}
],
"must_not":[{"match":{"state":"ID"}}]}}}'
{
"took" : 25,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 43,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "obrienwalton@suremax.com",
"city" : "Crucible",
"state" : "UT"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "cameronross@telpod.com",
"city" : "Nord",
"state" : "MO"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "jacquelinemaxwell@fuelworks.com",
"city" : "Oretta",
"state" : "OR"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "battleblackburn@zilphur.com",
"city" : "Wanamie",
"state" : "PA"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "valdezroberson@delphide.com",
"city" : "Chilton",
"state" : "MT"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "sargentpowers@accuprint.com",
"city" : "Umapine",
"state" : "AK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "lethabaker@dognosis.com",
"city" : "Dunlo",
"state" : "WV"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "pacemolina@cytrak.com",
"city" : "Silkworth",
"state" : "OR"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "simsmckay@comtour.com",
"city" : "Tilden",
"state" : "DC"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "harrisgross@kidstock.com",
"city" : "Yettem",
"state" : "KY"
}
}
]
}
}
命令太难看了,用json规范化一下:
curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
{
"query": {
"bool": {
"must": [{
"match": {
"age": "40"
}
}],
"must_not": [{
"match": {
"state": "ID"
}
}]
}
}
}'
这段的命令可以用sql写成
select * from bank where age = 40 and state != “ID”;
分页查询
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'
> {
> "query":{
> "match_all":{}},"from":0,"size":2}'
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "amberduke@pyrami.com",
"city" : "Brogan",
"state" : "IL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "hattiebond@netagy.com",
"city" : "Dante",
"state" : "TN"
}
}
]
}
}
多条件查询
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
"query":{"bool":{"must":[{"match":{"age":"23"}}],"filter":{"range":{"balance":{"gt":10000}}}}}}'
{
"took" : 40,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 35,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_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" : "fultonholt@anocha.com",
"city" : "Sunriver",
"state" : "RI"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "311",
"_score" : 1.0,
"_source" : {
"account_number" : 311,
"balance" : 13388,
"firstname" : "Vinson",
"lastname" : "Ballard",
"age" : 23,
"gender" : "F",
"address" : "960 Glendale Court",
"employer" : "Gynk",
"email" : "vinsonballard@gynk.com",
"city" : "Fairforest",
"state" : "WY"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "436",
"_score" : 1.0,
"_source" : {
"account_number" : 436,
"balance" : 27585,
"firstname" : "Alexander",
"lastname" : "Sargent",
"age" : 23,
"gender" : "M",
"address" : "363 Albemarle Road",
"employer" : "Fangold",
"email" : "alexandersargent@fangold.com",
"city" : "Calpine",
"state" : "OR"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "734",
"_score" : 1.0,
"_source" : {
"account_number" : 734,
"balance" : 20325,
"firstname" : "Keri",
"lastname" : "Kinney",
"age" : 23,
"gender" : "M",
"address" : "490 Balfour Place",
"employer" : "Retrotex",
"email" : "kerikinney@retrotex.com",
"city" : "Salunga",
"state" : "PA"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "765",
"_score" : 1.0,
"_source" : {
"account_number" : 765,
"balance" : 31278,
"firstname" : "Knowles",
"lastname" : "Cunningham",
"age" : 23,
"gender" : "M",
"address" : "753 Macdougal Street",
"employer" : "Thredz",
"email" : "knowlescunningham@thredz.com",
"city" : "Thomasville",
"state" : "WA"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "830",
"_score" : 1.0,
"_source" : {
"account_number" : 830,
"balance" : 45210,
"firstname" : "Louella",
"lastname" : "Chan",
"age" : 23,
"gender" : "M",
"address" : "511 Heath Place",
"employer" : "Conferia",
"email" : "louellachan@conferia.com",
"city" : "Brookfield",
"state" : "OK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "842",
"_score" : 1.0,
"_source" : {
"account_number" : 842,
"balance" : 49587,
"firstname" : "Meagan",
"lastname" : "Buckner",
"age" : 23,
"gender" : "F",
"address" : "833 Bushwick Court",
"employer" : "Biospan",
"email" : "meaganbuckner@biospan.com",
"city" : "Craig",
"state" : "TX"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "943",
"_score" : 1.0,
"_source" : {
"account_number" : 943,
"balance" : 24187,
"firstname" : "Wagner",
"lastname" : "Griffin",
"age" : 23,
"gender" : "M",
"address" : "489 Ellery Street",
"employer" : "Gazak",
"email" : "wagnergriffin@gazak.com",
"city" : "Lorraine",
"state" : "HI"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "160",
"_score" : 1.0,
"_source" : {
"account_number" : 160,
"balance" : 48974,
"firstname" : "Hull",
"lastname" : "Cherry",
"age" : 23,
"gender" : "F",
"address" : "275 Beaumont Street",
"employer" : "Noralex",
"email" : "hullcherry@noralex.com",
"city" : "Whipholt",
"state" : "WA"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "533",
"_score" : 1.0,
"_source" : {
"account_number" : 533,
"balance" : 13761,
"firstname" : "Margarita",
"lastname" : "Diaz",
"age" : 23,
"gender" : "M",
"address" : "295 Tapscott Street",
"employer" : "Zilodyne",
"email" : "margaritadiaz@zilodyne.com",
"city" : "Hondah",
"state" : "ID"
}
}
]
}
}
全文检索:
curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
> "query":{"match":{"address":"walk"}}}'
{
"took" : 14,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 11,
"relation" : "eq"
},
"max_score" : 4.465189,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "777",
"_score" : 4.465189,
"_source" : {
"account_number" : 777,
"balance" : 48294,
"firstname" : "Adkins",
"lastname" : "Mejia",
"age" : 32,
"gender" : "M",
"address" : "186 Oxford Walk",
"employer" : "Datagen",
"email" : "adkinsmejia@datagen.com",
"city" : "Faywood",
"state" : "OK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "362",
"_score" : 4.465189,
"_source" : {
"account_number" : 362,
"balance" : 14938,
"firstname" : "Jimmie",
"lastname" : "Dejesus",
"age" : 26,
"gender" : "M",
"address" : "351 Navy Walk",
"employer" : "Ecolight",
"email" : "jimmiedejesus@ecolight.com",
"city" : "Berlin",
"state" : "ME"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "557",
"_score" : 4.465189,
"_source" : {
"account_number" : 557,
"balance" : 3119,
"firstname" : "Landry",
"lastname" : "Buck",
"age" : 20,
"gender" : "M",
"address" : "558 Schweikerts Walk",
"employer" : "Protodyne",
"email" : "landrybuck@protodyne.com",
"city" : "Edneyville",
"state" : "AL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "61",
"_score" : 4.465189,
"_source" : {
"account_number" : 61,
"balance" : 6856,
"firstname" : "Shawn",
"lastname" : "Baird",
"age" : 20,
"gender" : "M",
"address" : "605 Monument Walk",
"employer" : "Moltonic",
"email" : "shawnbaird@moltonic.com",
"city" : "Darlington",
"state" : "MN"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "66",
"_score" : 4.465189,
"_source" : {
"account_number" : 66,
"balance" : 25939,
"firstname" : "Franks",
"lastname" : "Salinas",
"age" : 28,
"gender" : "M",
"address" : "437 Hamilton Walk",
"employer" : "Cowtown",
"email" : "frankssalinas@cowtown.com",
"city" : "Chase",
"state" : "VT"
}
},
从部分结果可以看出来,这是类似于like查询的,只要包含walk就可以
再来一个例子
curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
"query":{"match":{"address":"walk 114"}}}'
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 14,
"relation" : "eq"
},
"max_score" : 5.6544485,
"hits" : [
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "688",
"_score" : 5.6544485,
"_source" : {
"account_number" : 688,
"balance" : 17931,
"firstname" : "Freeman",
"lastname" : "Zamora",
"age" : 22,
"gender" : "F",
"address" : "114 Herzl Street",
"employer" : "Elemantra",
"email" : "freemanzamora@elemantra.com",
"city" : "Libertytown",
"state" : "NM"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "662",
"_score" : 5.6544485,
"_source" : {
"account_number" : 662,
"balance" : 10138,
"firstname" : "Daisy",
"lastname" : "Burnett",
"age" : 33,
"gender" : "M",
"address" : "114 Norman Avenue",
"employer" : "Liquicom",
"email" : "daisyburnett@liquicom.com",
"city" : "Grahamtown",
"state" : "MD"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "649",
"_score" : 5.6544485,
"_source" : {
"account_number" : 649,
"balance" : 20275,
"firstname" : "Jeanine",
"lastname" : "Malone",
"age" : 26,
"gender" : "F",
"address" : "114 Dodworth Street",
"employer" : "Nixelt",
"email" : "jeaninemalone@nixelt.com",
"city" : "Keyport",
"state" : "AK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "777",
"_score" : 4.465189,
"_source" : {
"account_number" : 777,
"balance" : 48294,
"firstname" : "Adkins",
"lastname" : "Mejia",
"age" : 32,
"gender" : "M",
"address" : "186 Oxford Walk",
"employer" : "Datagen",
"email" : "adkinsmejia@datagen.com",
"city" : "Faywood",
"state" : "OK"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "362",
"_score" : 4.465189,
"_source" : {
"account_number" : 362,
"balance" : 14938,
"firstname" : "Jimmie",
"lastname" : "Dejesus",
"age" : 26,
"gender" : "M",
"address" : "351 Navy Walk",
"employer" : "Ecolight",
"email" : "jimmiedejesus@ecolight.com",
"city" : "Berlin",
"state" : "ME"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "557",
"_score" : 4.465189,
"_source" : {
"account_number" : 557,
"balance" : 3119,
"firstname" : "Landry",
"lastname" : "Buck",
"age" : 20,
"gender" : "M",
"address" : "558 Schweikerts Walk",
"employer" : "Protodyne",
"email" : "landrybuck@protodyne.com",
"city" : "Edneyville",
"state" : "AL"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "61",
"_score" : 4.465189,
"_source" : {
"account_number" : 61,
"balance" : 6856,
"firstname" : "Shawn",
"lastname" : "Baird",
"age" : 20,
"gender" : "M",
"address" : "605 Monument Walk",
"employer" : "Moltonic",
"email" : "shawnbaird@moltonic.com",
"city" : "Darlington",
"state" : "MN"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "66",
"_score" : 4.465189,
"_source" : {
"account_number" : 66,
"balance" : 25939,
"firstname" : "Franks",
"lastname" : "Salinas",
"age" : 28,
"gender" : "M",
"address" : "437 Hamilton Walk",
"employer" : "Cowtown",
"email" : "frankssalinas@cowtown.com",
"city" : "Chase",
"state" : "VT"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "97",
"_score" : 4.465189,
"_source" : {
"account_number" : 97,
"balance" : 49671,
"firstname" : "Karen",
"lastname" : "Trujillo",
"age" : 40,
"gender" : "F",
"address" : "512 Cumberland Walk",
"employer" : "Tsunamia",
"email" : "karentrujillo@tsunamia.com",
"city" : "Fredericktown",
"state" : "MO"
}
},
{
"_index" : "bank",
"_type" : "_doc",
"_id" : "148",
"_score" : 4.465189,
"_source" : {
"account_number" : 148,
"balance" : 3662,
"firstname" : "Annmarie",
"lastname" : "Snider",
"age" : 34,
"gender" : "F",
"address" : "857 Lafayette Walk",
"employer" : "Edecine",
"email" : "annmariesnider@edecine.com",
"city" : "Hollins",
"state" : "OH"
}
}
]
}
}
这里面匹配的并不是匹配包含有114和walk的数据,es在查询的时候是通过倒排索引,有114或者walk的都会被查询到,简而言之,这个查询条件被分成了两个单词,如果想要查询两者同时包含的,那么如下操作
curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
"query":{"match_phrase":{"address":"walk 114"}}}'
{
"took" : 15,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
用match_phrase就会发现查出来的就是什么都没有了
分组查询(terms)
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
"aggs":{"balance_group":{"terms":{"field":"balance"}}},"size":0}'
{
"took" : 18,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"balance_group" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 985,
"buckets" : [
{
"key" : 22026,
"doc_count" : 2
},
{
"key" : 23285,
"doc_count" : 2
},
{
"key" : 36038,
"doc_count" : 2
},
{
"key" : 39063,
"doc_count" : 2
},
{
"key" : 45493,
"doc_count" : 2
},
{
"key" : 1011,
"doc_count" : 1
},
{
"key" : 1031,
"doc_count" : 1
},
{
"key" : 1110,
"doc_count" : 1
},
{
"key" : 1133,
"doc_count" : 1
},
{
"key" : 1172,
"doc_count" : 1
}
]
}
}
}
记得加size,不然会出现原始数据
求平均值(avg)
[hp@localhost ~]$ curl -XGET "localhost:9200/bank/_search?pretty" -H 'Content-Type:application/json' -d'{
"aggs":{"balance_avg":{"avg":{"field":"balance"}}},"size":0}'
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1000,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"balance_avg" : {
"value" : 25714.837
}
}
}
curl -XPUT "localhost:9200/bank/_mapping?pretty"
{
"properties": {
"name": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword",
"index": true
},
"tel": {
"type": "keyword",
"index": false
}
}
}
curl -XGET "localhost:9200/bank/_mapping?pretty"
由于在第一篇文章中我把yml的配置给改了,所以这里面的localhost改成的ip
可能是因为集群的原因,必须指明ip才能找到节点,结果如下
[hp@localhost ~]$ curl -XGET "192.168.189.129:9200/bank/_mapping?pretty"
{
"bank" : {
"mappings" : {
"properties" : {
"account_number" : {
"type" : "long"
},
"address" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"age" : {
"type" : "long"
},
"balance" : {
"type" : "long"
},
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"email" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"employer" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"firstname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"lastname" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"state" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}