【问题标题】:Elasticsearch rails not geting proper responseElasticsearch rails 没有得到适当的响应
【发布时间】:2017-01-09 08:30:33
【问题描述】:

我正在使用 elasticsearch-rails 和 elasticsearch-model gem 在我的 rails 应用中搜索单词。

我想让我的搜索不区分大小写,并且必须独立于复数形式。我在谷歌上进行了很多研究,但对如何使用分析器进行了预感,但没有成功。所以我不得不发布一个新问题。

这是我要搜索的模型

require 'elasticsearch/model'

class Article < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :title, analyzer: 'english', index_options: 'offsets'
      indexes :text, analyzer: 'english'
    end
  end

  def self.search(query)
    __elasticsearch__.search(
      {
        query: {
          multi_match: {
            query: query,
            fields: ['title^10', 'text']
          }
        },
        highlight: {
          pre_tags: ['<em>'],
          post_tags: ['</em>'],
          fields: {
            title: {},
            text: {}
          }
        }
      }
    )
  end
end

# Delete the previous articles index in Elasticsearch
Article.__elasticsearch__.client.indices.delete index: Article.index_name rescue nil

# Create the new index with the new mapping
Article.__elasticsearch__.client.indices.create \
  index: Article.index_name,
  body: { settings: Article.settings.to_hash, mappings: Article.mappings.to_hash }

# Index all article records from the DB to Elasticsearch
Article.import

#Article.import force: true

我的问题是如何搜索词? T恤,T恤,T恤都应该匹配。 任何进一步研究的链接也很有帮助。 谢谢

【问题讨论】:

    标签: ruby-on-rails elasticsearch-rails


    【解决方案1】:

    在表中搜索的一种方法是:

    User.where("firstname ILIKE ? OR lastname ILIKE ?", "%#{params[q]}%", "%#{params[q]}%" );
    

    在哪里

    User is Model
    
    params[q] is search query parameter.
    

    【讨论】:

    • 抱歉,我没听懂?你能解释一下吗。因为我是 ruby​​ 新手?
    • 打开 Rails 控制台进行测试。 User.where("firstname ILIKE ? OR lastname ILIKE ?", "%#{params[q]}%", "%#{params[q]}%" ) 其中 User 是模型名称 firstname 和 lastname 是属性或列名 params[:q] 为查询参数
    • Model.where("column_name ILIKE ?", "%#{query_string}%")
    • 要查找名称包含 Google 的公司运行:$ Company.where("name ilike '%Google%'")
    • 但我想在我的 Rails 应用程序中集成弹性搜索。因为它只是示例,我有很多数据,我需要使用 elasticsearch 搜索这些数据
    猜你喜欢
    • 2013-11-08
    • 2020-02-09
    • 1970-01-01
    • 2011-03-21
    • 1970-01-01
    • 2017-07-18
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多