10

有没有办法根据特定字段的长度过滤 ElasticSearch 文档?

例如,我有一堆带有“body”字段的文档,我只想返回body中字符数> 1000的结果。有没有办法在ES中做到这一点而不必添加额外的列索引中的长度?

4

2 回答 2

8

使用脚本过滤器,如下所示:

"filtered" : {
    "query" : {
        ...
    }, 
    "filter" : {
        "script" : {
            "script" : "doc['body'].length > 1000"
        }
    }
}

编辑 对不起,意在参考关于脚本过滤器的查询 DSL 指南

于 2013-07-28T20:11:08.940 回答
0

您还可以创建自定义标记器并在多字段属性中使用它,如下所示:

PUT test_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "character_analyzer": {
          "type": "custom",
          "tokenizer": "character_tokenizer"
        }
      },
      "tokenizer": {
        "character_tokenizer": {
          "type": "nGram",
          "min_gram": 1,
          "max_gram": 1
        }
      }
    }
  }, 
  "mappings": {
    "person": {
      "properties": {
        "name": { 
          "type": "text",
          "fields": {
            "keyword": { 
              "type": "keyword"
            },
            "words_count": { 
              "type": "token_count",
              "analyzer": "standard"
            },
            "length": { 
              "type": "token_count",
              "analyzer": "character_analyzer"
            }
          }
        }
      }
    }
  }
}

PUT test_index/person/1
{
  "name": "John Smith"
}

PUT test_index/person/2
{
  "name": "Rachel Alice Williams"
}

GET test_index/person/_search
{
  "query": {
    "term": {
      "name.length": 10
    }
  }
}
于 2017-11-29T15:23:59.240 回答