弹性搜索聚合分隔词

elasticsearch aggregations separated words

我只是 运行 浏览器插件中的聚合(奇迹),如下图所示,只有一个文档匹配查询但聚合由空格分隔但没有意义我想要聚合不同的文档.. 在这种情况下,应该只有一组计数为 1 且键为:"Drow Ranger"。 在弹性搜索中这样做的真正方法是什么..

可能是因为您的 heroname 字段是 analyzed,因此 "Drow Ranger" 被标记化并索引为 "drow" 和 "ranger"。

解决此问题的一种方法是将您的 heroname 字段转换为具有分析部分(您使用通配符查询搜索的部分)和另一个 not_analyzed 部分(您可以汇总的那个)。

您应该像这样创建索引并为您的 heroname 字段指定正确的映射

curl -XPUT localhost:9200/dota2 -d '{
    "mappings": {
        "agust": {
            "properties": {
                "heroname": {
                    "type": "string",
                    "fields": {
                        "raw: {
                            "type": "string",
                            "index": "not_analyzed"
                        }
                    }
                },
                ... your other fields go here
            }
        }
    }
} 

然后您可以 运行 在 heroname.raw 字段而不是 heroname 字段上聚合。

更新

如果您只想在 heroname 字段上尝试,您可以只修改该字段而不是重新创建整个索引。如果您 运行 以下命令,它只会将新的 heroname.raw 子字段添加到您现有的 heroname 字段。请注意,您仍然需要重新索引您的数据

curl -XPUT localhost:9200/dota2/_mapping/agust -d '{
    "properties": {
        "heroname": {
            "type": "string",
            "fields": {
                "raw: {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
} 

然后您可以在 wildcard 查询中继续使用 heroname,但您的聚合将如下所示:

{
    "aggs": {
        "asd": {
            "terms": {
                "field": "heroname.raw",    <--- use the raw field here
                "size": 0
            }
        }
    }
}