简体   繁体   English

如何使用Elasticsearch红宝石gem创建Ruby on Rails 4.1应用程序

[英]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. 我对Ruby on Rails和Elasticsearch都不熟悉。 I am looking for some useful information to help me implement the gems for Elasticsearch in to an application I am developing. 我正在寻找一些有用的信息,以帮助我在要开发的应用程序中实现Elasticsearch的精髓。

The application needs to be able to search and filter results, using Ajax ideally. 应用程序需要能够理想地使用Ajax搜索和过滤结果。 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. 我已经阅读了关于gem的README指南,文档和示例,但是有些零碎的内容我无法弄清,或者无法确定我是否采用了正确的方法。

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: 我要实现的一件事是,有一个名为date date的选择框其中有两个选项:

  • anytime 任何时候
  • Today (20) 今天(20)
  • Last Three days (50) 最近三天(50)
  • Last Week (100) 上周(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) models / concerns / searchable.rb(这包含在我的模型中)

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 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: (re)tire自述文件中:

NOTICE: This library has been renamed and retired in September 2013 ( read the explanation ). 注意:此库已在2013年9月重命名和淘汰(请阅读说明 )。 It is not considered compatible with Elasticsearch 1.x. 它不被认为与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. 看看http://github.com/elasticsearch/elasticsearch-rails gems套件,其中包含与Tyre相似的ActiveModel / Record和Rails集成功能集。

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. 从这里可以很容易地看到karmi(轮胎宝石的创建者建议您使用elasticsearch -rails宝石, karmi自己是活跃的并且维护着该宝石。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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