Elasticsearch复合查询格式



我有一个JSON,如下所示:

[
{
"tenant_id": "012345",
"status": "Active"
},
{
"tenant_id": "012345",
"status": "Not Applicable"
},
{
"tenant_id": "012345",
"status": "Inactive"
}
]

我想从Elasticsearch获取所有文档,这些文档可以显示在以下表示中:

  1. tenant_id必须与某个ID匹配,并且
  2. status可以是ActiveNot Applicable

如何为此编写查询?我尝试过以下查询,但没有成功:

{
"size": 2,
"track_total_hits": true,
"query": {
"bool": {
"should": [
{
"term": {
"status": "Active"
}
},
{
"term": {
"status": "Not Applicable"
}
}
],
"must": {
"term": {
"tenant_id": "012345"
}
},
"minimum_should_match" : 1
}
}
}

您不需要多个最小值应匹配的should子句。你可以为此使用术语。Terms采用一个数组,其中一个应该匹配。

由于这两个条件都是强制性的,您可以将它们封装在must:中


"query": {
"bool": {
"must": [
{
"terms": {
"status": ["Active","Not Applicable"]
}
},
{
"term": {
"tenant_id": "012345"
}
}
]
}
}

注意:术语查询是针对关键字字段的,不进行分析。