2017-06-16 106 views
0

我們對如何存儲以下數據以便以後輕鬆檢索它感到有些困惑。我們將有一百萬條記錄的格式如下,:ElasticSearch嵌套對象 - 使用各自的值獲取唯一字段

{ 
    "width" : 720 
    "height" : 200 
    "coords" : [{ 
       "el" : "xyz", 
       "x-y" : "100-50" 
       }, 
       { 
       "el" : "abc", 
       "x-y" : "200-150" 
       }] 
}, 
{ 
    "width" : 1280 
    "height" : 1000 
    "coords" : [{ 
       "el" : "lmn", 
       "x-y" : "400-50" 
       }, 
       { 
       "el" : "abc", 
       "x-y" : "500-250" 
       }] 
} 

將我們需要什麼類型的聚集查詢的運行得到下面的表單中的數據?到目前爲止,我只能搜索返回唯一值及其各自計數(即發生次數)的查詢。

{ 
"el" : "xyz", 
"x-y" : ["100-50"] 
}, 
{ 
"el" : "abc", 
"x-y" : ["200-150","500-250"] 
}, 
{ 
"el" : "lmn", 
"x-y" : ["400-50"] 
} 

我們創建了一個映射,其中「coords」的類型爲nested。 由於我們仍處於開發階段,我們仍然可以更改映射以獲得更好的性能和結果。

回答

0

我建議你扁平化你的el和x-y的,如果你有大數據並且需要像coords這樣的嵌套對象的聚合。您可以將聚合字符串添加到您的映射中;

{ 
    "width" : 1280 
    "height" : 1000 
    "coords" : [{ 
       "el" : "lmn", 
       "x-y" : "400-50" 
       }, 
       { 
       "el" : "abc", 
       "x-y" : "500-250" 
       }] 
    "coords-foraggregation" : "lmn,400-500;abc,500-200;nnn,xxx..." 
} 

而且你可以在你的映射模型的「座標 - foraggregation」字段中添加分析儀(https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html)這種方式能比這兩個聚集和coords.el更快coords.xy ...

+0

我們想要的是:獨特的「el」與所有「xy」值相對應的每個「el」。分析器分別爲每個單詞編制索引。我們如何能夠實現我們想要的結果? –

+0

您可以使用自定義分析器實現該功能,該分析器按指定的字符串分割字符串。 – eyildiz