2

私は今日、失敗したテスト ケースを修正しようとして、Elasticsearch で頭を壁にぶつけていました。

Rails 3.2.14、Ruby 1.9.3、Tire gem、ElasticSearch 0.90.2 を使用しています。

目的は、クエリが一致する結果を返すようにすることです。 vid == "ABC123xyz"

モデルの Ruby コードはVideo次のようになります。

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 のドキュメントはこのトピックに関してまばらであり、私はアイデアを使い果たしています。

ありがとう

4

1 に答える 1

0

トップレベル フィルターをこのように使用しても、クエリの結果はフィルター処理されません。ファセット カウントなどから結果がフィルター処理されるだけです。filterについては、elasticsearch のドキュメントに詳しい説明があります。

少し異なり、クエリ句の結果をフィルター処理するフィルター処理されたクエリを実行する必要があります。

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
于 2013-08-28T17:12:00.377 に答える