【问题标题】:Nest Aggregation with dynamic fields - elasticsearch具有动态字段的嵌套聚合 - elasticsearch
【发布时间】:2018-10-27 10:23:21
【问题描述】:

是否可以使用嵌套来创建带有非强类型关键字/字段的存储桶?

由于这个项目的性质。我没有任何要传入的根对象。 下面是一个例子。

            var result = client.Search<PortalDoc>(s => s
                        .Aggregations(a => a
                            .Terms("agg_objecttype", t => t.Field(l => "CUSTOM_FIELD_HERE"))
                        )     
                    );

【问题讨论】:

    标签: elasticsearch nest elasticsearch-6


    【解决方案1】:

    string implicitly convert to Field,所以你可以为任何字段名传递一个字符串

    var result = client.Search<PortalDoc>(s => s
        .Aggregations(a => a
            .Terms("agg_objecttype", t => t
                .Field("CUSTOM_FIELD_HERE")
            )
        )     
    );
    

    【讨论】:

    • 这么简单吧?快速提问。我收到一条错误消息“默认情况下,文本字段上的字段数据已禁用”。我知道您通常可以将 .Suffix("Keyword") 放在字段的末尾,这样它就会消失。无论如何要对字符串执行此操作?
    • 使用"CUSTOM_FIELD_HERE.Keyword"(将Keyword替换为关键字子字段的名称)
    【解决方案2】:

    是的,这样的事情是可能的。看起来here 是我使用嵌套字段的解决方案。它允许对“动态”字段进行所有操作,但需要付出更大的努力(嵌套字段更难操作)。要点有一些搜索证明,但我也实现了聚合。

    curl -XPOST localhost:9200/something -d '{
        "mappings" : {
            "live" : {
                "_source" : { "enabled" : true },
                "dynamic" : false,
                "properties" : {
                    "customFields" : {
                        "type" : "nested",
                        "properties" : {
                            "fieldName" : { "type" : "string", "index" : "not_analyzed" },
                            "stringValue": {
                                "type" : "string",
                                "fields" : {
                                    "raw" : { "type" : "string", "index" : "not_analyzed" }
                                }
                            },
                            "integerValue": { "type" : "long" },
                            "floatValue": { "type" : "double" },
                            "datetimeValue": { "type" : "date", "format" : "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd" },
                            "booleanValue": { "type" : "boolean" }
                        }
                    }
                }
            }
        }
    }'
    

    搜索应该在同一个嵌套查询中使用 AND,聚合应该在嵌套聚合中完成。 我为动态字段制作了它,但它可能可以针对其他内容进行调整。由于索引的工作原理,我怀疑可搜索/可聚合字段是否具有更大的灵活性。

    【讨论】:

      猜你喜欢
      • 2015-07-30
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-13
      • 2018-07-22
      • 2021-05-29
      • 1970-01-01
      相关资源
      最近更新 更多