2013-03-18 46 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" 
       } 
      } 
     ] 
     } 
    } 
} 
' 

这将搜索精确的短语:按相同的顺序相同的话。