2016-07-26 93 views
0

是否可以創建索引,限制索引父屬性?ElasticSearch創建具有動態屬性的索引

例如,

$ curl -XPOST 'http://localhost:9200/actions/action/' -d '{ 
    "user": "kimchy", 
    "message": "trying out Elasticsearch", 
    "actionHistory": [ 
    { "timestamp": 123456789, "action": "foo" }, 
    { "timestamp": 123456790, "action": "bar" }, 
    { "timestamp": 123456791, "action": "buz" }, 
    ... 
    ] 
}' 

我不想actionHistory在所有索引。如何才能做到這一點?

對於上述文件,我相信該指數將作爲

$ curl -XPOST localhost:9200/actions -d '{ 
    "settings": { 
    "number_of_shards": 1 
    }, 
    "mappings": { 
    "action": { 
     "properties" : { 
     "user": { "type": "string", "index" : "analyzed" }, 
     "message": { "type": "string": "index": "analyzed" }, 
     "actionHistory": { 
      "properties": { 
      "timestamp": { 
       "type": "date", 
       "format": "strict_date_optional_time||epoch_millis" 
      }, 
      "action": { "type": "string", "index": "analyzed" } 
      } 
     } 
     } 
    } 
    } 
}' 

會去除actionHistoryproperties"index": "no"替換它是妥善解決產生的呢?

這是一個例子,但是我的實際情況是具有動態屬性的文檔(即actionHistory包含各種自定義的,非重複的屬性),我的這個特定類型的映射定義有超過2000個不同的屬性,慢(即比數據庫中的全文搜索最差)。

+0

嗯,你的搜索查詢是如何構建的,因爲'actionHistory'如此不同使得它們表現不佳?你在搜索或字段名稱通配符中使用了'_all'嗎? –

+0

是的,我讓用戶使用'query_string'形式。 –

+0

這就是允許用戶用'query_string'完成他們想做的任何事情的缺點。同樣,允許用戶在所有文檔中創建_various自定義非重複屬性。您可以從這些任意的query_strings中獲得各種問題,並在文檔中擁有任何屬性。我建議限制這些自由以獲得更穩定和可預測的羣集。 –

回答

1

您可能可以通過使用dynamic templates,匹配所有actionHistory子字段並將"index": "no"設置爲全部匹配。

PUT actions 
{ 
    "mappings": { 
    "action": { 
     "dynamic_templates": [ 
     { 
      "actionHistoryRule": { 
      "path_match": "actionHistory.*", 
      "mapping": { 
       "type": "{dynamic_type}", 
       "index": "no" 
      } 
      } 
     } 
     ] 
    } 
    } 
} 
+0

這是什麼運氣? – Val