文章目录
概述
ES7的search api既可以在url中使用,也可以在请求体中使用
URI search
普通URI查询
指定字段中包含某值,比如以下查询表示检索movies文档中标题中包含2012的信息,其中profile为true表示显示查询怎么执行的:
GET /movies/_search?q=2012&df=title
{
"profile": "true"
}
结果如下,最后的profile字段显示了查询过程的相关信息:
{
"took" : 38,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 6,
"relation" : "eq"
},
"max_score" : 11.352903,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "72378",
"_score" : 11.352903,
"_source" : {
"genre" : [
"Action",
"Drama",
"Sci-Fi",
"Thriller"
],
"year" : 2009,
"title" : "2012",
"@version" : "1",
"id" : "72378"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "80505",
"_score" : 9.539693,
"_source" : {
"genre" : [
"Action",
"Sci-Fi"
],
"year" : 2009,
"title" : "2012: Supernova",
"@version" : "1",
"id" : "80505"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "102848",
"_score" : 9.539693,
"_source" : {
"genre" : [
"Sci-Fi"
],
"year" : 2012,
"title" : "Armageddon 2012",
"@version" : "1",
"id" : "102848"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "84017",
"_score" : 7.2301807,
"_source" : {
"genre" : [
"Animation",
"Documentary"
],
"year" : 2010,
"title" : "2012: Time for Change",
"@version" : "1",
"id" : "84017"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "105254",
"_score" : 5.3040524,
"_source" : {
"genre" : [
"Adventure",
"Comedy"
],
"year" : 2013,
"title" : "Crystal Fairy & the Magical Cactus and 2012",
"@version" : "1",
"id" : "105254"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "100444",
"_score" : 3.9138498,
"_source" : {
"genre" : [
"Comedy"
],
"year" : 2012,
"title" : "Rumble in the Air-Conditioned Auditorium: O'Reilly vs. Stewart 2012, The",
"@version" : "1",
"id" : "100444"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[JhcR-XkxT2uY3UubTpfZAQ][movies][0]",
"searches" : [
{
"query" : [
{
"type" : "TermQuery",
"description" : "title:2012",
"time_in_nanos" : 1121411,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 45802,
"match" : 0,
"next_doc_count" : 9,
"score_count" : 6,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 32544,
"advance_count" : 2,
"score" : 34714,
"build_scorer_count" : 4,
"create_weight" : 404150,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 604179
}
}
],
"rewrite_time" : 10574,
"collector" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 136241
}
]
}
],
"aggregations" : [ ]
}
]
}
}
如果不指定查询字段,就会在所有字段中匹配相关字符串:
GET /movies/_search?q=2012
{
"profile": "true"
}
结果中会多出来一个这样的结果:
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "91485",
"_score" : 1.0,
"_source" : {
"genre" : [
"Action",
"Adventure"
],
"year" : 2012,
"title" : "Expendables 2, The",
"@version" : "1",
"id" : "91485"
}
}
如果不在URL中加df参数,也可以用字段:
的方式实现指定字段匹配:
GET /movies/_search?q=title:2012
{
"profile": "true"
}
Phrase查询
GET /movies/_search?q=title:"Beautiful Mind"
{
"profile": "true"
}
结果如下:
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 13.790279,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4995",
"_score" : 13.790279,
"_source" : {
"genre" : [
"Drama",
"Romance"
],
"year" : 2001,
"title" : "Beautiful Mind, A",
"@version" : "1",
"id" : "4995"
}
}
]
}
term查询
把phrase查询里的""换成()就是泛查询(term查询):
GET /movies/_search?q=title:(Beautiful Mind)
{
"profile": "true"
}
结果如下:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 62,
"relation" : "eq"
},
"max_score" : 13.790279,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4995",
"_score" : 13.790279,
"_source" : {
"genre" : [
"Drama",
"Romance"
],
"year" : 2001,
"title" : "Beautiful Mind, A",
"@version" : "1",
"id" : "4995"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "47404",
"_score" : 8.71145,
"_source" : {
"genre" : [
"Adventure",
"Animation",
"Comedy",
"Fantasy",
"Romance",
"Sci-Fi"
],
"year" : 2004,
"title" : "Mind Game",
"@version" : "1",
"id" : "47404"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "3912",
"_score" : 8.665281,
"_source" : {
"genre" : [
"Comedy",
"Drama"
],
"year" : 2000,
"title" : "Beautiful",
"@version" : "1",
"id" : "3912"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "74064",
"_score" : 8.665281,
"_source" : {
"genre" : [
"Drama",
"Thriller"
],
"year" : 2009,
"title" : "Beautiful",
"@version" : "1",
"id" : "74064"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "3540",
"_score" : 7.5117273,
"_source" : {
"genre" : [
"Drama",
"Mystery",
"Romance"
],
"year" : 2000,
"title" : "Passion of Mind",
"@version" : "1",
"id" : "3540"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "83222",
"_score" : 7.5117273,
"_source" : {
"genre" : [
"Crime",
"Drama"
],
"year" : 1985,
"title" : "Trouble in Mind",
"@version" : "1",
"id" : "83222"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "100591",
"_score" : 7.5117273,
"_source" : {
"genre" : [
"Drama",
"Romance"
],
"year" : 1933,
"title" : "Mind Reader, The",
"@version" : "1",
"id" : "100591"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "106202",
"_score" : 7.5117273,
"_source" : {
"genre" : [
"Documentary"
],
"year" : 2012,
"title" : "Free The Mind",
"@version" : "1",
"id" : "106202"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "108248",
"_score" : 7.5117273,
"_source" : {
"genre" : [
"Documentary"
],
"year" : 2013,
"title" : "Into the Mind",
"@version" : "1",
"id" : "108248"
}
},
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "94",
"_score" : 7.28132,
"_source" : {
"genre" : [
"Comedy",
"Drama",
"Romance"
],
"year" : 1996,
"title" : "Beautiful Girls",
"@version" : "1",
"id" : "94"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[JhcR-XkxT2uY3UubTpfZAQ][movies][0]",
"searches" : [
{
"query" : [
{
"type" : "BooleanQuery",
"description" : "title:beautiful title:mind",
"time_in_nanos" : 625436,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 62,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 174990,
"match" : 11667,
"next_doc_count" : 85,
"score_count" : 62,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 43063,
"advance_count" : 2,
"score" : 55469,
"build_scorer_count" : 4,
"create_weight" : 191572,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 148459
},
"children" : [
{
"type" : "TermQuery",
"description" : "title:beautiful",
"time_in_nanos" : 204096,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 6,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 49,
"compute_max_score_count" : 6,
"compute_max_score" : 14774,
"advance" : 8089,
"advance_count" : 69,
"score" : 33415,
"build_scorer_count" : 6,
"create_weight" : 118350,
"shallow_advance" : 4009,
"create_weight_count" : 1,
"build_scorer" : 25322
}
},
{
"type" : "TermQuery",
"description" : "title:mind",
"time_in_nanos" : 67249,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 6,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 14,
"compute_max_score_count" : 6,
"compute_max_score" : 6391,
"advance" : 14938,
"advance_count" : 21,
"score" : 7946,
"build_scorer_count" : 6,
"create_weight" : 31189,
"shallow_advance" : 1932,
"create_weight_count" : 1,
"build_scorer" : 4799
}
}
]
}
],
"rewrite_time" : 5789,
"collector" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 88641
}
]
}
],
"aggregations" : [ ]
}
]
}
}
布尔查询
与逻辑:AND
GET /movies/_search?q=title:(Beautiful AND Mind){ "profile": "true"}
结果如下:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 13.790279,
"hits" : [
{
"_index" : "movies",
"_type" : "_doc",
"_id" : "4995",
"_score" : 13.790279,
"_source" : {
"genre" : [
"Drama",
"Romance"
],
"year" : 2001,
"title" : "Beautiful Mind, A",
"@version" : "1",
"id" : "4995"
}
}
]
},
"profile" : {
"shards" : [
{
"id" : "[JhcR-XkxT2uY3UubTpfZAQ][movies][0]",
"searches" : [
{
"query" : [
{
"type" : "BooleanQuery",
"description" : "+title:beautiful +title:mind",
"time_in_nanos" : 1536592,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 10206,
"match" : 0,
"next_doc_count" : 1,
"score_count" : 1,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 30201,
"advance_count" : 2,
"score" : 5287,
"build_scorer_count" : 4,
"create_weight" : 178873,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 1312016
},
"children" : [
{
"type" : "TermQuery",
"description" : "title:beautiful",
"time_in_nanos" : 163234,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 6,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 1,
"compute_max_score_count" : 4,
"compute_max_score" : 4920,
"advance" : 2941,
"advance_count" : 19,
"score" : 634,
"build_scorer_count" : 6,
"create_weight" : 106483,
"shallow_advance" : 1800,
"create_weight_count" : 1,
"build_scorer" : 46419
}
},
{
"type" : "TermQuery",
"description" : "title:mind",
"time_in_nanos" : 71323,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 0,
"shallow_advance_count" : 6,
"set_min_competitive_score" : 0,
"next_doc" : 0,
"match" : 0,
"next_doc_count" : 0,
"score_count" : 1,
"compute_max_score_count" : 4,
"compute_max_score" : 21195,
"advance" : 5648,
"advance_count" : 21,
"score" : 3146,
"build_scorer_count" : 6,
"create_weight" : 29046,
"shallow_advance" : 4756,
"create_weight_count" : 1,
"build_scorer" : 7493
}
}
]
}
],
"rewrite_time" : 7520,
"collector" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 14684
}
]
}
],
"aggregations" : [ ]
}
]
}
}
以上也可以使用%2B来实现:
GET /movies/_search?q=title:(Beautiful %2BMind)
{
"profile": "true"
}
要求某一字符串不存在:NOT
GET /movies/_search?q=title:(Beautiful NOT Mind)
{
"profile": "true"
}
指定范围
比如年份>2000年:
GET /movies/_search?q=year:>2000
{
"profile": "true"
}
通配符查询
查询标题以b开头的电影:
GET /movies/_search?q=title:b*
{
"profile": "true"
}
对beautiful进行模糊匹配,~后面的数字一般和要匹配的单词数一致:
GET /movies/_search?q=title:beautifl~1
{
"profile": "true"
}
phrase查询里也可以用~来进行模糊匹配:
GET /movies/_search?q=title:"Lord Rings"~2
{
"profile": "true"
}
请求体查询
对某字段进行排序
GET /kibana_sample_data_ecommerce/_search
{
"sort": [
{
"order_date": {
"order": "desc"
}
}
],
"query": {
"match_all": {}
}
}
指定保留的字段
GET /kibana_sample_data_ecommerce/_search
{
"_source": ["order_date"],
"query": {
"match_all": {}
}
}
使用脚本添加字段
GET /kibana_sample_data_ecommerce/_search
{
"script_fields": {
"new_field": {
"script": {
"lang": "painless",
"source": "doc['order_date'].value + '_szc'"
}
}
},
"query": {
"match_all": {}
}
}
逻辑操作符
用请求体查询进行匹配时,多个字符串默认是用or的逻辑进行匹配:
POST movies/_search
{
"query": {
"match": {
"title": "Last Christmas"
}
}
}
and逻辑需要指定:
POST movies/_search
{
"query": {
"match": {
"title": {
"query": "Christmas Vacation",
"operator": "and"
}
}
}
}
match_phrase
match_phrase可以进行phrase匹配,也可以指定单词间隔数
POST movies/_search
{
"query": {
"match_phrase": {
"title": {
"query": "one love",
"slop": 1
}
}
}
}
query_string和simple_query_string
先插入两条数据:
PUT /users/_doc/1
{
"name": "Jordan Henderson",
"about": "Hadoop, java, Android"
}
PUT /users/_doc/2
{
"name": "Jordan Milna",
"about": "C++"
}
query_string
然后先进行query_string,来匹配name字段,要求同时包含乔丹和亨德森:
POST users/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "Jordan AND Henderson"
}
}
}
也可以对多个字段进行匹配:
POST users/_search
{
"query": {
"query_string": {
"fields": ["name", "about"],
"query": "(Jordan AND Henderson) OR (java AND Android)"
}
}
}
simple_query_string
而simple_query_string中把AND、OR当成字符串来处理,要用+、|来代替:
POST users/_search
{
"query": {
"simple_query_string": {
"query": "Jordan+Henderson",
"fields": ["name"]
}
}
}
也可以使用default_operator来指定操作符,默认为or:
POST users/_search
{
"query": {
"simple_query_string": {
"query": "Jordan Henderson",
"fields": ["name"]
, "default_operator": "AND"
}
}
}