【问题标题】:elasticsearch aggregations separated wordselasticsearch聚合分隔单词
【发布时间】:2015-11-17 19:37:23
【问题描述】:

我只是在浏览器插件(奇迹)中运行一个聚合,如下图所示,只有一个文档与查询匹配,但聚合后用空格分隔,但我想为不同的文档聚合是没有意义的.. ın this场景应该只有一组计数为 1 和键:“Drow Ranger”。 在elasticsearch中这样做的真正方法是什么..

【问题讨论】:

    标签: elasticsearch elasticsearch-aggregation


    【解决方案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"
                    }
                }
            }
        }
    } 
    

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

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

    【讨论】:

    • 我知道它是因为我分析了我的字段索引。但如果我这次这样做,我可能无法进行“不区分大小写的查询”。我的意思是我已经这样做了,我运行查询“...wildcard:*d*”...它不起作用,但 D 有效.. 那么这两者如何工作?
    • 不区分大小写的通配符搜索将针对heroname 字段起作用,并且聚合将针对heroname.raw 字段起作用。我们所做的只是添加一个新字段 (heroname.raw),而不是更改已经有效的字段 (heroname)。试试看。
    • 我完全理解你的意思,而且效果很好!,谢谢 :) 亲切的问候,
    猜你喜欢
    • 2020-07-21
    • 2017-08-28
    • 2016-03-20
    • 2018-06-17
    • 2017-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多