【问题标题】:Elastic search term aggregation弹性搜索词聚合
【发布时间】:2021-02-19 03:42:45
【问题描述】:

我正在编写一个 python 脚本来获取弹性搜索索引中的唯一值。我正在使用术语聚合来获取唯一值及其计数。但是,当我将字段列表传递给脚本时,我意识到某些字段存储为
"abc" : {
            "type" : "keyword"
        }

有些存储为

"xyz" : {
            "type" : "text",
            "fields" : {
              "keyword" : {
                "type" : "keyword"
              }
            }
          }

在术语聚合期间,我使用查询

{
    "aggs" : {
        "abc" : {
            "terms" : {
                "field" : "abc"
            }
        }
    }, "size":0
}

但是当这个查询用于“xyz”时,它会给出错误Fielddata is disabled on text fields by default. Set fielddata=true on [description] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.

要运行“xyz”查询,我需要向其中添加“.keyword”,但“abc”将不会运行。

有什么方法可以检查哪个字段属于哪个类型,然后使用 if/else 相应地更新查询?

【问题讨论】:

    标签: python sql elasticsearch kibana elk


    【解决方案1】:

    您可以同时拥有 - 字段可聚合和可搜索,无需 .keyword 表示法。只需按照错误消息的提示调整您的映射:

    "xyz" : {
       "type" : "text",
       "fielddata": true
    }
    

    然后重新索引,你就可以开始了。

    至于是否有查询时检查来确定哪些字段是哪些——没有。 ElasticSearch 的核心原则之一是预先确定和定义字段类型,以便对它们进行适当的索引,从而优化搜索/聚合。因此,假设在查询时您知道哪些字段属于哪种类型。

    【讨论】:

    • 嘿@Joe,感谢您的回复。但是我不能改变索引的结构。这就是为什么我正在寻找可以比较的解决方案。
    • 对。然后查看GET your_index_name/_mapping,所有这些字段的定义都存储在其中。相应地构建您的查询。
    • 当然。告诉我进展如何。
    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-03-10
    • 1970-01-01
    • 2019-11-08
    • 2023-03-18
    • 2021-10-11
    • 1970-01-01
    相关资源
    最近更新 更多