【发布时间】:2012-08-17 19:53:55
【问题描述】:
我将Tire 用于弹性搜索。在我的应用程序中,我有 2 个模型;价格和产品。
我正在尝试搜索我的 Price 类并将其所属的 Product 的 :name 属性用于搜索字段。现在,如果我有一个名为 Product 1 的产品并输入“pro”、“prod”或“duct”,则不会出现任何结果。但是输入“产品”或“产品”会显示结果。我相信问题在于我的映射。我查看了查询及其:
...localhost:3000/search/results?utf8=%E2%9C%93&query=product
当我认为它应该是:
...localhost:3000/search/results?utf8=%E2%9C%93&query:product=product
从这个问题来看:ElasticSearch mapping doesn't work
我不知道如何让我的params[:query] 只接受product.name。我尝试使用:string params[:query], default_field: "product.name",但没有成功。
我不想使用_all 字段。
这是我的代码:
价格.rb
include Tire::Model::Search
include Tire::Model::Callbacks
def self.search(params)
tire.search(load: true, page: params[:page], per_page: 20) do
query do
boolean do
must { string params[:query] } if params[:query].present?
must { term :private, false }
end
end
sort do
by :date, "desc"
by :amount, "asc"
end
end
end
def to_indexed_json
to_json( include: { product: { only: [:name] } } )
end
mapping do
indexes :id, type: "integer"
indexes :amount, type: "string", index: "not_analyzed"
indexes :date, type: "date", index: "not_analyzed"
indexes :private, type: "boolean"
indexes :product do
indexes :name, type: "string", analyzer: "custom_analyzer"
end
end
settings analysis: {
analyzer: {
custom_analyzer: {
tokenizer: [ "whitespace", "lowercase" ],
filter: [ "ngram_filter", "word_delimiter_filter" ],
type: "custom"
}
},
filter: {
ngram_filter: {
type: "nGram",
min_gram: 2,
max_gram: 15
}
},
filter: {
word_delimiter_filter: {
type: "word_delimiter",
generate_word_parts: true,
generate_number_parts: true,
preserve_original: true,
stem_english_possessive: true
}
}
}
那么有没有人有任何建议或知道如何将查询字段设置为仅使用产品名称?
谢谢。
【问题讨论】:
-
我将不得不在不久的将来解决同样的问题。所以我会更新我的进度,除非你打败我!
-
@Alain 我还在做这个,所以如果你知道如何让它工作,请告诉我。你的进步会很有帮助。
标签: ruby-on-rails ruby ruby-on-rails-3.1 elasticsearch tire