Ruby: Faceted navigation in a Rails app


#1

Hello.

I have an online store built with Ruby on Rails, where I want to index my products' specs/attributes and then provide filtering options to my users.

I'm in the process of figuring out how I will build my queries, and I want some help.

I'm using the elasticsearch-model gem, with which I can do queries like this:

Product.search(filter: { bool: { filter: [{term: {category_id: 7}}, {range: {price: {gte:51.5, lte: 60}}}]} })

meaning "give me all products from category X that are in the following price range".

  1. The query above returns the proper results, but is it the way to go or I can write this in a more idiomatic/correct way?

  2. I'm thinking about how should I build compound queries like this in an imperative fashion, based on input that user has given me (through query string params in the URL or something).

Should I use the elasticsearch-dsl gem or I can do this by just constructing the hashes my own?

  1. I tried to convert that query using the elasticsearch-dsl gem but failed to do so:

    [66] pry(main)> search { filter { bool { filter {} }}}.to_hash
    NoMethodError: undefined method filter' for #<Elasticsearch::DSL::Search::Filters::Bool:0xbddab3cc> from (pry):68:inblock (3 levels) in '

Thanks in advance!


(system) #2