繁体   English   中英

Elasticsearch按索引类型和字段搜索

[英]Elasticsearch Search by Index Type and Field

我想搜索单个索引类型,然后通过匹配单个字段进一步对其进行过滤。 假设我有书籍,电影和玩具类型。 现在,我想搜索惊悚类型的电影,我该怎么做? 我有此代码无法正常工作并产生一些错误:

{
            "from" : 0, 
            "size" : 10,
            "query": {
                "filtered" : {
                    "filter" : {
                        "type" : { 
                            "value" : "movies"
                        },
                        "term" : { 
                            "genre" : "Thriller"
                        }
                    }
                }
            },
            "sort": [
                {
                    "date_entered" : {
                        "order": "desc"
                    }
                }
            ]
        }

错误是这样的:

{"error":"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed;

您的语法错误; 您不能以这种方式指定两个过滤器。 尝试使用bool must过滤器

我设置了一个简单的索引,然后添加了三个不同类型但具有相同“类型”的文档:

PUT /test_index

POST /test_index/_bulk
{"index":{"_type":"books"}}
{"genre":"Thriller"}
{"index":{"_type":"movies"}}
{"genre":"Thriller"}
{"index":{"_type":"toys"}}
{"genre":"Thriller"}

然后运行此查询:

POST /test_index/_search
{
   "from": 0,
   "size": 10,
   "query": {
      "filtered": {
         "query": {
            "match_all": {}
         },
         "filter": {
            "bool": {
               "must": [
                  {
                     "type": {
                        "value": "movies"
                     }
                  },
                  {
                     "term": {
                        "genre": "thriller"
                     }
                  }
               ]
            }
         }
      }
   }
}

并返回正确的文档:

{
   "took": 10,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "movies",
            "_id": "uReb0g9KSLKS18sTATdr3A",
            "_score": 1,
            "_source": {
               "genre": "Thriller"
            }
         }
      ]
   }
}

注意,我必须在术语过滤器中使用小写的“惊悚片”。 由于我未指定分析器,因此将使用标准分析器 ,这会将文本“ Thriller”转换为术语“ thriller”,因此,如果我要使用term过滤器,则需要使用下限-case版本。 如果我改用匹配查询 ,则可以使用“ Thriller”作为查询文本。

这是我用来测试的代码:

http://sense.qbox.io/gist/2f5dc5f15f0ecb50fb9772bbf4d52849031be41d

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM