Elastic Search equivalent of "Scoping (Scalar Fields)" in sunspot/solr
Asked Answered
A

0

0

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
Aswan answered 29/1, 2013 at 9:19 Comment(4)
I can't speak authoritatively to Tire's syntax, but the with operator in Sunspot is a filter query. I would think your filter on the Tire query comes close. Let us know if some experimentation gets you to the right answer.Infeasible
The sunspot dsl makes it easy to flatten associations. The tire examples I've seen so far use a dotted notation for filtering ('categories.id') by association attributes.Aswan
Did you ever find a clear answer for this?Charlie
nope. we flattened association attributes and indexed them instead.Aswan

© 2022 - 2024 — McMap. All rights reserved.