簡體   English   中英

具有動態字段的嵌套聚合-Elasticsearch

[英]Nest Aggregation with dynamic fields - elasticsearch

是否可以使用nest來創建帶有非強類型關鍵字/字段的存儲桶?

由於該項目的性質。 我沒有任何要傳遞的根對象。下面是一個示例。

            var result = client.Search<PortalDoc>(s => s
                        .Aggregations(a => a
                            .Terms("agg_objecttype", t => t.Field(l => "CUSTOM_FIELD_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在同一嵌套查詢中完成,而聚合則應在嵌套聚合中完成。 我是針對動態字段制作的,但可能可以針對其他內容進行調整。 我懷疑由於索引的工作原理,可搜索/可聚合字段是否會具有更大的靈活性。

string隱式轉換為Field ,因此您可以為任何字段名稱傳遞字符串

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM