【问题标题】:Elasticsearch Query Filter for Word Count用于字数统计的 Elasticsearch 查询过滤器
【发布时间】:2016-08-05 19:28:32
【问题描述】:

我目前正在寻找一种方法来返回某个字段中最多包含 n 个单词的文档。

对于包含“名称”字段中少于三个单词的文档的结果集,查询可能看起来像这样,但据我所知,没有像 word_count 这样的东西。

有谁知道如何处理这个问题,甚至可能以不同的方式处理?

GET myindex/myobject/_search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "word_count": {
                "name": {
                  "lte": 3
                }
              }
            }
          ]
        }
      },
      "query": {
        "match_all" : { }
      }
    }
  }
}

【问题讨论】:

    标签: elasticsearch elasticsearch-query


    【解决方案1】:

    您可以使用token_count 数据类型来索引给定字段中的标记数量,然后在该字段上进行搜索。

    # 1. create the index/mapping with a token_count field
    PUT myindex
    {
      "mappings": {
        "myobject": {
          "properties": {
            "name": { 
              "type": "string",
              "fields": {
                "word_count": { 
                  "type":     "token_count",
                  "analyzer": "standard"
                }
              }
            }
          }
        }
      }
    }
    
    # 2. index some documents
    
    PUT index/myobject/1
    {
       "name": "The quick brown fox"
    }
    PUT index/myobject/2
    {
       "name": "brown fox"
    }
    
    # 3. the following query will only return document 2
    POST myindex/_search
    {
      "query": {
        "range": {
          "name.word_count": { 
            "lt": 3  
          }
        }
      }
    }
    

    【讨论】:

    • 这听起来像是一个可能的解决方案。我可能必须重新索引所有文档,但这对我来说没问题。感谢您的帮助。
    • 是的,您需要创建一个新索引并重新索引您的数据以填充name.word_count 字段。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多