Elasticsearch(ES)多條件過濾實現案例
阿新 • • 發佈:2018-12-12
要完成上述圖片上的查詢條件,也即需要同時符合多種過濾條件。
由於公司的伺服器中安裝的elasticsearch版本很老,升級不是很方便,所以是基於老版本,新版本相應的語句做一些改變也可以。
實現:(具體條件需要更改)(elasticsearch版本:2.3.2)
潛在優化:
1. "should": [
{"match_phrase": {"termID": "00:23:A5:30:45:AD"}},
{"match_phrase": {"material_id": "1520"}},
{"match_phrase": {"material_id": "1521"}}
],
"minimum_should_match": 2,
這個匹配可能是比較耗時的,採用match_phrase主要是因為低版本string型別,且建立index時,沒有設定為not_anlyzed。
改成not_anlyzed(新版本用keyword型別,index=true),用filter - terms也許效率會提升;
2. 按專案而不是日期來定義索引,這樣搜尋範圍將會大大縮小;
3. 採用最新版本elasticsearch,採用painless語言。
補充:
version:6.3(改良優化後)的實現程式碼: