-->

Elastic Search equivalent of “Scoping (Scalar Fiel

2019-08-29 14:01发布

问题:

I'm exploring various options for a search engine for our rails-app/data. I was able to make sunspot/solr work and am currently exploring ElasticSearch as an alternative but couldn't get the same thing(scoping/filtering) to work under ES.

I would like to filter/scope objects as described in the "Scoping" section in 'https://github.com/sunspot/sunspot'.

I have an active record class 'Product'.

class Product < ActiveRecord::Base
...

  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire.mapping do
    indexes :name, :type => :string
    indexes :categories do
      indexes :id, :type => :integer
    end
  end
end

Once I imported products into ES,

the following query works and gives results

Product.tire.search { query { string '*', default_operator: "AND", default_field: "name" } }.results

How do I get products of a particular category given a category id ?

Product.tire.search { query { string '*', default_operator: "AND", default_field: "name" }; filter(:term, 'categories.id' => 38) }.results

I'm basically looking for the tire equivalent for the following sunspot call:

Product.search do
  with(:category_ids, 38)
end

with the following in the Product class

searchable :auto_index => true, :auto_remove => true do
  text :name

  integer :category_ids, :multiple => true do
     self.categories.map &:id
  end

end