elasticsearch - 使用多个应该查询

标签 elasticsearch

我想获取类似于多个“组”但单独存在的文档。每个组都有自己的规则(术语)。
当我尝试在一个“ bool(boolean) ”内使用多个“应”查询时,我会得到同时包含两个“应”项的项目。
我想总共使用1个查询,而不是msearch。

有人可以帮我吗?

{
 "explain": true,
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "term": {
                "p_id": "123"
              }
            },
            {
              "term": {
                "p_id": "124"
              }
            }
          ]
        }
      },
      "query": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "term": {
                      "cat": "1"
                    }
                  },
                  {
                    "term": {
                      "cat": "2"
                    }
                  },

                  {
                    "term": {
                      "keys": "a"
                    }
                  },
                  {
                    "term": {
                      "keys": "b"
                    }
                  }
                ]
              }
            },
            {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "term": {
                      "cat": "6"
                    }
                  },
                  {
                    "term": {
                      "cat": "7"
                    }
                  },
                  {
                    "term": {
                      "keys": "r"
                    }
                  },

                  {
                    "term": {
                      "keys": "u"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  },
  "from": 0,
  "size": 3
}

最佳答案

您可以尝试通过脚本在多个字段上使用术语聚合,然后将热门匹配聚合添加为子聚合。请注意,这将非常缓慢。在查询/过滤器后面添加此代码,并根据需要调整size参数

 "aggs": {
    "Cat_and_Keys": {
      "terms": {
        "script": "doc['cat'].values + doc['keys'].values"
      }, 
      "aggs":{ "separate_docs": {"top_hits":{"size":1 }} }
   }
 }

关于elasticsearch - 使用多个应该查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50201761/

相关文章:

python - 如何读取S3中的ElasticSearch快照文件?

python - Django haystack 索引多对多字段,但如果有多个词则搜索失败

elasticsearch - 如何在 elasticsearch 中只存储有限数量的文档。

elasticsearch - 查询 Elasticsearch 基于地址的索引

Elasticsearch : map date as text?

elasticsearch - Elasticsearch,对交叉日期字段进行排序

elasticsearch - Wire定制插件与elasticsearch

elasticsearch - {"error":"Content-Type header [] is not supported","status":406} When Inserting Data to Elasticsearch with Golang

elasticsearch - 是否有用于索引/搜索序数作为数值的过滤器?

node.js - Elasticsearch批量API发布请求中的NewLine错误