簡體   English   中英

Elasticsearch不應用NOT過濾器

[英]Elasticsearch doesn't apply the NOT filter

今天,我一直在用Elasticsearch撞牆,試圖修復一個失敗的測試用例。

我正在使用Rails 3.2.14,Ruby 1.9.3,Tire gem和ElasticSearch 0.90.2

目的是讓查詢返回匹配結果不包括的項目,其中vid == "ABC123xyz"

Video模型中的Ruby代碼如下所示:

def related_videos(count)
  Video.search load: true do
    size(count)
    filter :term, :category_id => self.category_id
    filter :term, :live => true
    filter :term, :public => true
    filter :not, {:term => {:vid => self.vid}}
    query do
      boolean do
        should { text(:_all, self.title, boost: 2) }
        should { text(:_all, self.description) }
        should { terms(:tags, self.tag_list, minimum_match: 1) }
      end  
   end
  end
end

Tire生成的結果搜索查詢如下所示:

{
  "query":{
    "bool":{
      "should":[
        {
          "text":{
            "_all":{
              "query":"Top Gun","boost":2
            }
          }
        },
        {
          "text":{
            "_all":{
              "query":"The macho students of an elite US Flying school for advanced fighter pilots compete to be best in the class, and one romances the teacher."
            }
          }
        },
        {
          "terms":{
            "tags":["top-gun","80s"],
            "minimum_match":1
          }
        }
      ]
    }
  },
  "filter":{
    "and":[
      {
        "term":{
          "category_id":1
        }
      },
      {
        "term":{
          "live":true
        }
      },
      {
        "term":{
          "public":true
        }
      },
      {
        "not":{
          "term":{
            "vid":"ABC123xyz"
          }
        }
      }
    ]
  },
  "size":10
}

從ElasticSearch得到的JSON:

{
  "took": 7,
  "timed_out": false,
  "_shards":{
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total":1,
    "max_score":0.2667512,
    "hits":[
      {
        "_index":"test_videos",
        "_type":"video",
        "_id":"8",
        "_score":0.2667512,
        "_source":{
          "vid":"ABC123xyz",
          "title":"Top Gun",
          "description":"The macho students of an elite US Flying school for advanced fighter pilots compete to be best in the class, and one romances the teacher.",
          "tags":["top-gun","80s"],
          "category_id":1,
          "live":true,
          "public":true,
          "featured":false,
          "last_video_view_count":0,
          "boost_factor":0.583013698630137,
          "created_at":"2013-08-28T14:24:47Z"
        }
      }
    ]
  }
}

有人可以幫忙! Elasticsearch的文檔很少涉及該主題,而我的想法已經用完了。

謝謝

使用頂級過濾器不會過濾查詢結果-只會過濾掉方面計數之類的結果。 Elasticsearch文檔中有關於filter的完整描述。

您需要執行一個稍有不同的過濾查詢 ,並過濾查詢子句的結果:

Video.search load: true do
  query do
    filtered do
      boolean do
        should { text(:_all, self.title, boost: 2) }
        should { text(:_all, self.description) }
        should { terms(:tags, self.tag_list, minimum_match: 1) }
      end  
      filter :term, :category_id => self.category_id
      filter :term, :live => true
      filter :term, :public => true
      filter :not, {:term => {:vid => self.vid}}
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM