如何使用 Elasticsearch 编写条件查询?
How to write a conditional query with Elasticsearch?
我有一个简单的 JSON Elasticsearch 查询,如下所示:
"query": {
"bool": {
"must": { "match": { "id": "1" }} ,
"must": { "match": { "tags.name": "a1"}}
}
}
如果值(在本例中为 'a1')不为空,我如何才能执行第二个 'must' 条件?
您可以使用以下方法实现它 -
{
"query": {
"bool": {
"must": [
{
"match": {
"id": "1"
}
},
{
"bool": {
"should": [
{
"missing": {
"field": "tags.name"
}
},
{
"match": {
"tags.name": "a1"
}
}
]
}
}
]
}
}
}
我认为"missing"不能用于最新版本的elasticsearch。
但是可以改用条件从句。
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-template.html#_conditional_clauses
我有一个简单的 JSON Elasticsearch 查询,如下所示:
"query": {
"bool": {
"must": { "match": { "id": "1" }} ,
"must": { "match": { "tags.name": "a1"}}
}
}
如果值(在本例中为 'a1')不为空,我如何才能执行第二个 'must' 条件?
您可以使用以下方法实现它 -
{
"query": {
"bool": {
"must": [
{
"match": {
"id": "1"
}
},
{
"bool": {
"should": [
{
"missing": {
"field": "tags.name"
}
},
{
"match": {
"tags.name": "a1"
}
}
]
}
}
]
}
}
}
我认为"missing"不能用于最新版本的elasticsearch。 但是可以改用条件从句。 https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-template.html#_conditional_clauses