2

我只是在浏览器插件(奇迹)中运行聚合,如下图所示,只有一个文档与查询匹配,但聚合用空格分隔,但我想为不同的文档聚合是没有意义的。在这种情况下应该只有一组计数为 1 和键:“卓尔游侠”。在elasticsearch中这样做的真正方法是什么.. 在此处输入图像描述

4

1 回答 1

1

这可能是因为您的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"
                }
            }
        }
    }
} 

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

{
    "aggs": {
        "asd": {
            "terms": {
                "field": "heroname.raw",    <--- use the raw field here
                "size": 0
            }
        }
    }
}
于 2015-08-24T13:30:33.957 回答