gpt4 book ai didi

ruby-on-rails - 轮胎/Elasticsearch 搜索关联

转载 作者:行者123 更新时间:2023-11-29 02:55:57 25 4
gpt4 key购买 nike

我有以下代码,我正在尝试使用 ElasticSearch 来查询它。

当我执行 Book.search(:q=>'Foo') 时它可以工作,但当我执行 Book.search(:author=>'Doctor') 时它不起作用。在我的数据库中,我有一个名为“Barz, Foo, Doctor”的条目

我不确定在我的查询中是否应该使用 terms 或 term,因为我正在使用滚雪球破坏名称。我尝试使用术语,然后出现错误。我没有得到任何结果。

class Author < ActiveRecord::Base
has_many :books
end

class Book < ActiveRecord::Base
belongs_to :author
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :title,
indexes :description
indexes :author,type: 'object', properties: {
name: { type: 'multi_field',
fields: { name: { type: 'string', analyzer: 'snowball' },
exact: { type: 'string', index: 'not_analyzed' }
}
} }
end

def to_indexed_json
to_json(:include=>{:author=>{:only=>[:name]}} )
end

def self.search(params = {})
tire.search(load:true) do
query do
boolean do
should { string params[:q] } if params[:q].present?
should { term params[:author] } if params[:author].present?
end
end
filter :term, :active=>true
end
end
end

最佳答案

你可以这样做

should { terms :author, [params[:author]]} if params[:author].present?

should { term :author, params[:author]} if params[:author].present?

should { string "author:#{params[:author]}"} if params[:author].present?

关于ruby-on-rails - 轮胎/Elasticsearch 搜索关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10071634/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com