有没有办法根据特定字段的长度过滤ElasticSearch文档?
例如,我有一堆带有“body”字段的文档,我只想返回body中字符数为>的结果。 1000.有没有办法在ES中执行此操作而无需在索引中添加长度为额外的列?
答案 0 :(得分:7)
使用脚本过滤器,如下所示:
"filtered" : {
"query" : {
...
},
"filter" : {
"script" : {
"script" : "doc['body'].length > 1000"
}
}
}
修改强> 对不起,打算参考the query DSL guide on script filters
答案 1 :(得分:0)
您还可以创建自定义标记生成器并在multifields属性中使用它,如下所示:
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
}
}
}