Williamf
Williamf

Reputation: 575

ElasticSearch / Tire - Facets are not filtered by the query?

I have an ElasticSearch / Tire query that has facets attached. It seems the facets a using the global scope, instead of the query scope and filters.

Here is my definition. I'm not sure how to scope the it to use the filters.

 def self.facets_for(keyword_array, fasit, opts = {})
    keyword_array = [keyword_array] if keyword_array.is_a?(String)
    tire.search(per_page: opts[:per_page], page: opts[:page] ) do 
      query do 
        boolean { should { string '*' }}
      end
      filter :terms, :keyword => keyword_array

      facet "phrases" do terms :phrases, :size => 20 end if fasit.include?("phrases")
      facet "sentiment" do terms :sentiment end if fasit.include?("sentiment")
      facet "lang" do terms :lang end if fasit.include?("lang")
      facet "provider" do terms :provider end if fasit.include?("provider")

    end # tire search
  end 

Right now, this returns facets that are "global" in scope, i.e. they aren't filtered by the filters (or the query if there is a more sophisticated query).

What am I missing?

Upvotes: 0

Views: 1185

Answers (1)

karmi
karmi

Reputation: 14419

Facets are by default bound (restricted) by the query issued: see the Tire integration test case.

Filters, on the contrary, do not restrict the facets, making "faceted navigation" possible (again, see the integration test case).

Note that in your example, your query is effectively a match_all query, and thus cannot restrict facets.


On the Ruby side of things, a safer construct for keyword_array would be:

keyword_array = Array(keyword_array)

Upvotes: 4

Related Questions