Elasticsearch Query Filter for Word Count

≯℡__Kan透↙ 提交于 2020-01-04 05:14:09

问题


I am currently looking for a way to return documents with a maximum of n words in a certain field.

The query could look like this for a resultset that contains documents with less than three words in the "name" field but there is nothing like word_count as far as I know.

Does anyone know how to handle this, maybe even in a different way?

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

回答1:


You can use the token_count data type in order to index the number of tokens in a given field and then search on that field.

# 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  
      }
    }
  }
}


来源:https://stackoverflow.com/questions/38786713/elasticsearch-query-filter-for-word-count

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!