Elasticsearch提供了基于JSON的完整查询DSL(特定域语言)来定义查询。将查询DSL视为查询的AST(抽象语法树),它由两种子句构成:
- 叶子查询子句:叶子查询子句中寻找一个特定的值在某一特定领域。
- 复合查询子句:复合查询子句包括其他叶子查询或复合查询,并用于以逻辑方式组合多个查询。
Match
返回与提供的文本、数字、日期或布尔值匹配的文档。匹配之前对提供的文本进行分析。
该match查询是用于执行全文检索的标准查询,其中包括模糊匹配的选项。
查询参数
<field>:(必填,对象)要搜索的字段。
参数说明
query:(必填)文本、数字、日期或布尔值。在提供的<field>中查找。
match查询会在搜索前分析所有提供的文本。这意味着match查询可以在text字段中搜索已分析的标记,而不是确切的词。
实例
GET /bank/_search
{
"query": {
"match": {
"address": {
"query": "mill lane"
}
}
}
}
可简写为
{
"query": {
"match": {
"address": "mill lane"
}
}
}
结果
{
"took": 4,
"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"
}
}
.........
}
]
}
}
根据
倒排索引搜索出以上数据,包含mill lane或mil或lane的文档。
Match phrase
match _phrase会分析文本,根据分析的文本创建一个phrase查询。
实例
GET /bank/_search
{
"query": {
"match_phrase": {
"address": "mill lane"
}
}
}
结果
{
"took": 21,
"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"
}
}
]
}
}
Multi match
建立在match查询上的多匹配查询,允许多字段查询。
实例
GET /bank/_search
{
"query": {
"multi_match": {
"query": "mill movico",
"fields": ["address", "city"]
}
}
}
结果
{
"took": 49,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 6.5059485,
"hits": [
{
"_index": "bank",
"_type": "_doc",
"_id": "472",
"_score": 6.5059485,
"_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": "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"
}
}
]
}
}
Match_all 查询
最简单的查询,匹配所有文档,所有文档得分都为1.0。
默认情况下,
hits返回匹配到的前10个文档。
GET /bank/_search
{
"query": {
"match_all": {}
}
}
结果
{
"took": 1,
"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"
}
}
.....
]
}
}
复合查询
Boolean 型
一种查询方式,用于匹配与其他查询组合相匹配的文档。
bool查询采用的是更好匹配法,因此每个匹配项must或should的得分相加,便是_score每个文档的最终结果。
实例
must
子句(查询)必须出现在匹配的文档中,并有助于得分。
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "F"
}
},
{
"match": {
"address": "mill"
}
}
]
}
}
}
结果
{
"took": 46,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 6.1104345,
"hits": [
{
"_index": "bank",
"_type": "_doc",
"_id": "472",
"_score": 6.1104345,
"_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"
}
}
]
}
}
must_not
子句(查询)不得出现在匹配的文档中,不会用于计分。
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": 38
}
}
]
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 6.0824604,
"hits": [
{
"_index": "bank",
"_type": "_doc",
"_id": "970",
"_score": 6.0824604,
"_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"
}
}
]
}
}
should
子句(查询)应该出现在匹配的文档中,并有助于得分。即在满足其他条件的基础上,又满足此条件的,会提升得分。
GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": 18
}
}
],
"should": [
{
"match": {
"lastname": "Wallace"
}
}
]
}
}
}
结果
{
"took": 88,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 12.585751,
"hits": [
{
"_index": "bank",
"_type": "_doc",
"_id": "970",
"_score": 12.585751,
"_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": 6.0824604,
"_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": 6.0824604,
"_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"
}
}
]
}
}
filter
子句(查询)必须出现在匹配的文档中,不会用于计分。GET /bank/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "M"
}
},
{
"match": {
"address": "mill"
}
}
],
"must_not": [
{
"match": {
"age": 18
}
}
],
"should": [
{
"match": {
"lastname": "Wallace"
}
}
],
"filter": [
{
"range": {
"age": {
"gte": 18,
"lte": 30
}
}
}
]
}
}
}
结果
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 12.585751,
"hits": [
{
"_index": "bank",
"_type": "_doc",
"_id": "970",
"_score": 12.585751,
"_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"
}
}
]
}
}
术语查询
Term 查询
返回提供字段中包含确切术语的文档。
避免使用
term对text字段使用查询。要搜索
text字段值,请改用match查询。
term查询不会分析搜索词,term查询仅搜索提供的确切术语。意味着用term搜索text字段时查询可能会返回不准确或没有结果。
查询参数
<field>:(必填,对象)要搜索的对象。
参数说明
value:(必须,字符串)希望在提供<field>中找到术语。要返回文档,该术语必须与字段值完全匹配,包括空格和大小写字母。
实例
GET /bank/_search
{
"query": {
"term": {
"balance": "39225"
}
}
}
结果
{
"took": 20,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"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"
}
}
]
}
}
Terms 查询
返回提供字段中包含一个或多个确切术语的文档。
term查询和terms查询相同,不同在于terms可以搜索多个值。
查询参数
<field>:(必填,对象)要搜索的对象。
参数说明
value:(必须,字符串)希望在提供<field>中找到术语。要返回文档,该术语必须与字段值完全匹配,包括空格和大小写字母。
默认情况下,ELasticsearch 将
terms查询限制为最多65536个词。可以使用index.max_terms_count设置更改此限制。
实例
GET /bank/_search
{
"query": {
"terms": {
"balance": ["39225", "16418"]
}
}
}
结果
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"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": "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": "elinorratliff@scentric.com",
"city": "Ribera",
"state": "WA"
}
}
]
}
}