具有动态字段的嵌套聚合 - elasticsearch

Nest Aggregation with dynamic fields - elasticsearch

是否可以使用 nest 创建 keywords/fields 非强类型的存储桶?

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

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

是的,类似的事情是可能的。使用嵌套字段查找 here 我的解决方案。它允许在 "dynamic" 字段上执行所有操作,但需要付出更大的努力(嵌套字段更难操作)。要点有一些搜索证据,但我也实现了聚合。

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

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

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