Search code examples
ruby-on-railsfilterelasticsearchtire

Filtering Tire results by status


I'm trying to filter my Tire search results by a 'status_id'. I do not want to show results with a 'status_id' of 2.

Here is my current search code:

query = params[:query]
page = params[:page] || 1
per_page = params[:per_page] || 5
offset = page.to_i - 1 < 0 ? 0 : (page.to_i - 1) * per_page

@results = Tire.search INDEX_NAME do
  query { string query }
  from offset
  size per_page
end

I have tried using:

filter :terms, status_id: '1'
filter :string, status_id: '1'
filter :integer, status_id: '1'

but none of them work. However, ideally I would like to get all that aren't '2', not just select results for '1'.

status_id is being properly returned by elasticsearch in the json.

Any help would be great.

Thank you.


Solution

  • filter :term, :status_id => 1
    

    Should work. The terms filter expects an array of possible terms, which is why your attempts have not worked.

    If you want to use the not filter, something like

    filter :not, {:term => {:status_id => 2}}
    

    Should work.