如何在Elasticsearch查询中结合“必须”和“应该”?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在Elasticsearch查询中结合“必须”和“应该”?相关的知识,希望对你有一定的参考价值。
我需要在Elasticsearch查询DSL中“翻译”这个伪SQL查询:
从invoiceType中选择invoiceType ='REGULAR'和receiver ='CUSTOMER'和(invoiceStatus ='DISPATCHED'或invoiceStatus ='PAYED')
我有这个:
{
"query": {
"bool": {
"must": [
{ "match": { "invoiceType": "REGULAR" }},
{ "match": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should": [
{"match" : {"invoiceStatus": "DISPATCHED"}},
{"match" : {"invoiceStatus": "PAYED"}}
]
}
}
]
}
}
}
该查询返回0结果,但我知道有很多匹配我正在搜索的内容。 AFAIK,must
就像'AND'和should
就像'OR'。我错过了什么?
答案
不确定它是否适合你,但你可以尝试一下,看看你得到了什么?虽然我用match
对term
做了一些改变。希望这会帮助你。
GET /invoice/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"invoiceType" : "REGULAR"}},
{ "term": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should" : [
{"terms": {"invoiceStatus": ["DISPATCHED","PAYED"]}}
]
}}
]
}
}
}
}
}
要么
GET /invoice/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"invoiceType" : "REGULAR"}},
{ "term": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should" : [
{"term": {"invoiceStatus": "DISPATCHED"}},
{"term": {"invoiceStatus": "PAYED"}}
]
}}
]
}
}
}
}
}
以上是关于如何在Elasticsearch查询中结合“必须”和“应该”?的主要内容,如果未能解决你的问题,请参考以下文章
Elasticsearch:深入理解 Elasticsearch 查询:过滤器查询 vs 全文搜索
Elasticsearch:深入理解 Elasticsearch 查询:过滤器查询 vs 全文搜索