elasticsearch - 参数的 Elasticsearch 对象关联查询

标签 elasticsearch tire

我在使用Elastic Search和Tire不返回任何结果时遇到了一些困难。我正在使用Ruby 1.9.3和Rails 3.2.11。

在我的 Controller 中,我正在打电话:

@location_id = 1
@listings = Listing.search(params.merge!(location_id: @location_id))

在我的列表模型中,我有
mapping do
    indexes :id, type: 'integer'
    ...
    indexes :author do
      indexes :location_id,     :type => 'integer', :index => :not_analyzed
      ...
end

def self.search(params={})
      tire.search(load: true, page: params[:page], per_page: 20) do |search|

      search.query  { string params[:query], :default_operator => "AND" } if params[:query].present?
      search.filter :range, posted_at: {lte: DateTime.now}

      search.filter :term, "author.location_id"       => params[:location_id]
end

我有300个结果,在数据库中所有的location_id都为1,所以我似乎无法弄清楚为什么它返回的是nil集?如果我注释掉author.location_id搜索过滤器行,它是否按预期返回所有其他结果?

最佳答案

在您这样的情况下,有几件事需要解决。让我们从一个完整的代码开始:

require 'active_record'
require 'tire'
require 'logger'

# Tire.configure { logger STDERR }
# ActiveRecord::Base.logger = Logger.new(STDERR)

Tire.index('articles').delete

ActiveRecord::Base.establish_connection( adapter: 'sqlite3', database: ":memory:" )
ActiveRecord::Schema.define(version: 1) do
  create_table :articles do |t|
    t.string :title
    t.integer :author_id
    t.date    :posted_at
    t.timestamps
  end
  create_table :authors do |t|
    t.string  :name
    t.integer :number, :location_id
    t.timestamps
  end
  add_index(:articles, :author_id)
  add_index(:authors, :location_id)
end

class Article < ActiveRecord::Base
  belongs_to :author, touch: true
  self.include_root_in_json = false

  include Tire::Model::Search
  include Tire::Model::Callbacks

  mapping do
    indexes :title

    indexes :author do
      indexes :location_id, type: 'integer'
    end
  end

  def self.search(params={})
    tire.search load: {include: 'author'} do |search|
      search.query do |query|
        query.filtered do |f|
          f.query { params[:query].present? ? match([:title], params[:query], operator: 'and') : match_all }
          f.filter :range, 'posted_at' => { lte: DateTime.now }
          f.filter :term,  'author.location_id' => params[:location_id]
        end
      end
    end
  end

  def to_indexed_json
    to_json( only: ['title', 'posted_at'], include: { author: { only: [:location_id] } } )
  end
end

class Author < ActiveRecord::Base
  has_many :articles

  after_touch do
    articles.each { |a| a.tire.update_index }
  end
end

# -----

Author.create id: 1, name: 'John', location_id: 1
Author.create id: 2, name: 'Mary', location_id: 1
Author.create id: 3, name: 'Abby', location_id: 2

Article.create title: 'Test A', author: Author.find(1), posted_at: 2.days.ago
Article.create title: 'Test B', author: Author.find(2), posted_at: 1.day.ago
Article.create title: 'Test C', author: Author.find(3), posted_at: 1.day.ago
Article.create title: 'Test D', author: Author.find(3), posted_at: 1.day.from_now

Article.index.refresh

# -----

articles = Article.search query: 'test', location_id: 1
puts "", "Documents with location:1", '-'*80
articles.results.each { |a| puts "* TITLE: #{a.title}, LOCATION: #{a.author.location_id}, DATE: #{a.posted_at}" }

articles = Article.search query: 'test', location_id: 2
puts "", "Documents with location:2", '-'*80
articles.results.each { |a| puts "* TITLE: #{a.title}, LOCATION: #{a.author.location_id}, DATE: #{a.posted_at}" }
puts "(NOTE: 'D' is missing, because is not yet posted)"

articles = Article.search query: 'test b', location_id: 1
puts "", "Documents with query:B and location:1", '-'*80
articles.results.each { |a| puts "* TITLE: #{a.title}, LOCATION: #{a.author.location_id}, DATE: #{a.posted_at}" }

首先,创建一个像这样的隔离的提取案例通常是一个好主意。

在您的示例代码中,我假设您有一个Listing belongs_to :author关系。您需要正确定义映射和序列化,我再次假设您已完成。

至于查询本身:
  • 除非您使用多面导航,否则请像我的示例代码中那样,使用filtered查询而不是顶级过滤器。
  • 请勿使用string查询,除非您确实希望向用户展示Lucene查询字符串查询的所有功能(以及脆弱性!)。
  • 使用match查询作为您的“通用”查询-轮胎在其上撒一些糖,从而可以轻松创建multi_match查询,等等
  • 您的示例中的过滤器语法正确。当在Tire中多次调用filter方法时,它将创建和and过滤器。

  • 取消对Tire日志记录配置(可能还有ActiveRecord日志记录)的注释,以查看代码在做什么。

    关于elasticsearch - 参数的 Elasticsearch 对象关联查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14729115/

    相关文章:

    elasticsearch - 在Elasticsearch中生成URL字符串的匹配项

    ruby-on-rails - 通过has_many关联搜索Rails轮胎(ElasticSearch)

    ruby-on-rails - 轮胎/elasticsearch 远程服务器连接错误

    ruby-on-rails - 退出嵌套过滤器错误的参数

    python - 如何获取所有文档类型的列表

    elasticsearch - 如何在Elasticsearch中实现自动完成

    search - Elasticsearch -如何增加 Elasticsearch 文档中的_score

    elasticsearch - 如何使用Logstash解析日志

    ruby - Elasticsearch 字段提升未显示在映射中

    elasticsearch - 使用Elasticsearch自定义脚本字段检查参数数组中是否存在ID