在elasticsearch中搜索列表的各个部分

时间:2013-03-18 18:01:02

标签: elasticsearch

说我有一个书名列表,我想找到我的索引中存在哪些书。

映射是:

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

我可以用

迭代并检查每一个
curl -XPOST 'localhost:9200/myindex/book/_search' 
-d '{"query":{"match":{"title":"my title"}}}

但是假设我的标题列表很长,我怎样才能批量处理并获得哪些标题的列表?

1 个答案:

答案 0 :(得分: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"
               }
            }
         ]
      }
   }
}
'

这将搜索确切的短语:相同的单词以相同的顺序。