简体   繁体   中英

How to create a Ruby on Rails 4.1 application with Elasticsearch ruby gems

I am new-ish to Ruby on Rails, as well to Elasticsearch. I am looking for some useful information to help me implement the gems for Elasticsearch in to an application I am developing.

The application needs to be able to search and filter results, using Ajax ideally. I have read the README guides, documentation and examples, for the gems, however there are bits and pieces that I can't figure out, or confirm if I am taking the correct approach.

I have the results being created within the index, I also have the results coming out. My main focus is getting aggregations, filters and facets working, what are the standard conventions for these?

I'm looking for some information on taking this from the view right the way through.

One particular thing I am trying to achieve, is having a select box called date posted, with a couple of options:

  • anytime
  • Today (20)
  • Last Three days (50)
  • Last Week (100)

The numbers representing the number of documents that fall within that facet? or aggregation or filter? - Not sure which is the best to use.

I also want the numbers to update based on other filtered options that may be present ie the query.

Here is some code:

models/concerns/searchable.rb (This is included in my model)

def self.search(query, options={})

    __set_filters = lambda do |key, f|

    @search_definition[:filter][:and] ||= []
    @search_definition[:filter][:and]  |= [f]

    @search_definition[:facets][key.to_sym][:facet_filter][:and] ||= []
    @search_definition[:facets][key.to_sym][:facet_filter][:and]  |= [f]
  end

    @search_definition = {
        query: {},
        filter: {}
    }

    unless query.blank?
        @search_definition[:query] = {
            bool: {
            should: [
              { 
                multi_match: {
                    query: query,
                    fields: ['title^10', 'content'],
                    operator: 'and'
                }
              }
            ]
          }
        }
     else
        @search_definition[:query] = { match_all: {} }
        @search_definition[:sort]  = { created_at: 'desc' }
    end


    if options[:offset]
        f = {
            numeric_range: {
                created_at: {
                    gte: "now-5d"
                }
            }
        }
    end

        __elasticsearch__.search(@search_definition)
    end

Controller.rb

options = {
        offset: params[:datecreatedoffset],
    }
    @jobs = Job.search(params[:q], options).results
    respond_with @jobs

Any help is really appreciated. Thank you.

From the (re)tire readme:

NOTICE: This library has been renamed and retired in September 2013 ( read the explanation ). It is not considered compatible with Elasticsearch 1.x.

Have a look at the http://github.com/elasticsearch/elasticsearch-rails suite of gems, which contain similar set of features for ActiveModel/Record and Rails integration as Tire.

From this it is plain to see the karmi (the creator of the tire gem suggests that you use the elasticsearch-rails gem, where karmi himself is active and maintaining the gem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM