2013-03-18 44 views
0

假設我有一個書名的列表,我想找到它們中的哪些存在於我的索引中。搜索彈性搜索的部分列表

的映射是:

"book": { 
    "properties": { 
        "title":{"type":"string"}, 
        "author":{"type":"string"}}} 

我可以迭代,並檢查每一個與

curl -XPOST 'localhost:9200/myindex/book/_search' 
-d '{"query":{"match":{"title":"my title"}}} 

不過,假設我渴望冠軍的名單,我怎麼可以批量做到這一點,並得到一個列表哪些受到了打擊?

回答

0

運行幾個match查詢對你的標題字段,它們與bool查詢相結合:

curl -XGET 'http://127.0.0.1:9200/myindex/book/_search?pretty=1' -d ' 
{ 
    "query" : { 
     "bool" : { 
     "should" : [ 
      { 
       "match" : { 
        "title" : "Title one" 
       } 
      }, 
      { 
       "match" : { 
        "title" : "Title two" 
       } 
      }, 
      { 
       "match" : { 
        "title" : "Title three" 
       } 
      } 
     ] 
     } 
    } 
} 
' 

當然,一個match查詢將匹配任何書籍,其title字段包含查詢字符串一個字,所以您可能需要使用match_phrase代替:

curl -XGET 'http://127.0.0.1:9200/myindex/book/_search?pretty=1' -d ' 
{ 
    "query" : { 
     "bool" : { 
     "should" : [ 
      { 
       "match_phrase" : { 
        "title" : "Title one" 
       } 
      }, 
      { 
       "match_phrase" : { 
        "title" : "Title two" 
       } 
      }, 
      { 
       "match_phrase" : { 
        "title" : "Title three" 
       } 
      } 
     ] 
     } 
    } 
} 
' 

這將搜索精確的短語:按相同的順序相同的話。