【问题标题】:Elasticsearch query to find documents with number of values of a term equal to a specified numberElasticsearch查询以查找术语值的数量等于指定数量的文档
【发布时间】:2023-03-16 01:21:01
【问题描述】:

我有一个带有以下映射的 Elasticsearch 索引“库”:

{
  "mappings": {
    "book": { 
      "properties": { 
        "title":    { "type": "text", "index": "not_analyzed" }, 
        "author":   { "type": "text", "index": "not_analyzed" }, 
        "price":    { "type": "integer" },  
    }
  }
}

现在我想进行查询以查找作者数量等于 3 的所有文档(书籍)。即我想进行匹配的查询

curl -XGET "http://localhost:9200/library/_search?pretty=true" -d '{
    "query": {
        "match": {
            Number of values of term "author" = 3.
        }
    }
}'

有没有办法在不添加额外术语的情况下进行这样的查询?

[我知道在搜索结果中查找某个词的所有可能值的聚合,但无法根据上述条件转换该聚合。]

【问题讨论】:

    标签: elasticsearch elasticsearch-2.0


    【解决方案1】:

    无法找到准确获取每个作者的 3 个文档的方法。 聚合将为您提供所有可能的值。但是,它也会向您显示doc_count - 我们可以在那里找到我们的方式:

    {
      "size": 0,
      "aggregations": {
        "authors": {
          "terms": {
            "field": "author",
            "min_doc_count": 3,
            "size": 5
          }
        }
      }
    }
    

    min_doc_count - 将仅过滤包含至少 3 个文档的存储桶。

    size - 只会给你前 5 个文档(请记住,默认情况下,桶按 'doc_count' 升序排序)。

    现在您可以调整 size 以准确获取具有 3 个文档的作者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      相关资源
      最近更新 更多