elasticsearch中的术语和范围过滤器无法工作

时间:2015-01-12 23:37:30

标签: filter elasticsearch

我有记录

观看次数:3亿 作者:psy

但我无法搜索此记录

如何修复我的代码?

{ "query":{
 "filtered" :{
     "filter" :{ 
        "and":[{"term" :{"author": "psy"}},
               {"range" :{"viewCount" :{ "gte" : 3000000 }}}] },
      "query":{ 
         "multi_match" : { "query" : "psy", "fields" : ["title","content"] } } } },
               "size": 20 }

1 个答案:

答案 0 :(得分:0)

很难确定您发布的内容,但问题似乎出现在您发布的代码的"query"部分。当我保存您发布的文档(至少我认为您的意思),并运行您的搜索查询时,它没有返回。但是当我删除"query"部分并运行"filter"部分时,文档被返回。更确切地说,我创建了一个索引并添加了文档:

DELETE /test_index

PUT /test_index

PUT /test_index/doc/1
{
   "viewCount": 300000000,
   "author": "psy"
}

然后运行过滤后的查询:

POST /test_index/_search
{
   "query": {
      "filtered": {
         "filter": {
            "and": [
               {
                  "term": {
                     "author": "psy"
                  }
               },
               {
                  "range": {
                     "viewCount": {
                        "gte": 3000000
                     }
                  }
               }
            ]
         },
         "query": {
            "multi_match": {
               "query": "psy",
               "fields": [
                  "title",
                  "content"
               ]
            }
         }
      }
   },
   "size": 20
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

一无所获。当我只运行"query"部分时:

POST /test_index/_search
{
   "query": {
      "multi_match": {
         "query": "psy",
         "fields": [
            "title",
            "content"
         ]
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 0,
      "max_score": null,
      "hits": []
   }
}

我什么也没得到。但当我只运行"filter"部分时:

POST /test_index/_search
{
   "filter": {
      "and": [
         {
            "term": {
               "author": "psy"
            }
         },
         {
            "range": {
               "viewCount": {
                  "gte": 3000000
               }
            }
         }
      ]
   }
}
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "viewCount": 300000000,
               "author": "psy"
            }
         }
      ]
   }
}

该文件已被退回。所以这里出了点问题:

"query": {
    "multi_match": {
       "query": "psy",
       "fields": [
          "title",
          "content"
       ]
    }
}

由于我所拥有的文档不包含这些字段,因此它与查询不匹配且不会返回。也许你的文档有这些领域,但如果没有关于你的设置的更多信息,很难告诉你出了什么问题。也许如果您发布了映射和一些示例文档,它会有所帮助。