2014-04-11 49 views
1

我是ElasticSearch的新手,試圖學習它。ElasticSearch:執行簡單的IN查詢

select * from results where mycal in ['abc', 'def', 'ghi'] and col2 in ['er', 'es', 'et'] 

如何在elasticsearch做到這一點:

在常規RDBM,如下我可以執行「在」聲明?

謝謝

回答

2

很多將取決於如何索引elasticsearch中的數據。但是你可以使用terms filter實現上述:

{ 
    "filter": { 
     "and": [ 
     { 
      "terms": { 
       "cal": [ 
        "abc", 
        "def", 
        "ghi" 
       ], 
       "execution": "or" 
      } 
     }, 
     { 
      "terms": { 
       "col2": [ 
        "er", 
        "es", 
        "et" 
       ], 
       "execution": "or" 
      } 
     } 
     ] 
    } 
} 
+0

+1的過濾器,他們比快的查詢,緩存快響應。記住Elasticsearch不會對過濾結果進行評分。 – Nate

2

根據您是否想filter or a query,你可以去請使用bool查詢/過濾器或快捷terms查詢/過濾器(首尾相連的查詢,因爲我預計這不會被緩存)。

當你與一個以上的領域去,那麼你要使用通過termsbool查詢中它:

{ 
    "query" : { 
    "bool" : { 
     "should" : [ 
     { "terms" : { "mycal" : [ "abc", "def", "ghi" ] } }, 
     { "terms" : { "col2" : [ "er", "es", "et" ] } } 
     ], 
     "minimum_should_match" : 2 
    } 
    } 
} 

默認情況下,"minimum_should_match"1,這將使上述查詢的OR

這是假設有像證件:

{ "mycal" : "abc", "col2" : "es" } 
{ "mycal" : "def", "col2" : "er" } 

如果你的視野進行嵌套在一個對象中(例如,{ "key" : { "mycal" : "abc" } }),那麼你只需訪問他們喜歡"key.mycal"

重要的部分是你如何做你的映射。默認情況下,您的字符串將被分析並以全部小寫形式存儲,因此您需要使用全部小寫的term(s)查詢進行搜索。如果你想找到"ABC",那麼你會仍然尋找​​。不過,如果你使用別的東西,像match代替term(S),那麼你就需要把它全部用小寫:

{ 
    "query" : { 
    "bool" : { 
     "should" : [ 
     { "match" : { "mycal" : "ABC" } }, 
     { "match" : { "mycal" : "DEF" } }, 
     { "match" : { "mycal" : "GHI" } }, 
     { "match" : { "col2" : "ER" } }, 
     { "match" : { "col2" : "ES" } }, 
     { "match" : { "col2" : "ET" } } 
     ], 
     "minimum_should_match" : 2 
    } 
    } 
}