简体   繁体   中英

tire elastic search filter by association ids

I have a Post model that has voters. in Elastic Search i have indexed Post so that i have following example data: name: 'My first post', voter_ids: [1,2,3] . I am trying to search posts so that I only get results for given voter_ids. For example if search says 1,2 , i want to get My first post in results since it has voter_ids 1 and 2, but if search is 1,5 , I should not see that post in my result since voter 5 never voted on that post. following code returns all the posts that have any of the voter_ids specified. so its like OR-ing. What i am looking is AND.

@posts = Post.search(load: true) do |tire|
  tire.filter :terms, :voter_ids => voter_ids
end

Create the equivalent of this:

"filter" : {
    "terms" : {
        "voter_ids" : ["1", "2"],
        "execution" : "and"
    }
}

From reading the elasticsearch docs there is an execution parameter on a terms filter which you can set to be and .

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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