因此,假设我输入文本“电子书”进行搜索并得到以下结果:
Ebook for Rails
Elastic Ebook
Booking for ebook.
现在我从列表中选择“Elastic Ebook”。假设其他几个用户输入 Ebook 进行搜索,其中大多数从列表中选择“Elastic Ebook”。这清楚地表明了 Elastic Ebook 对关键字“Ebook”的流行程度。因此,在进一步的搜索中,我希望 Elastic Ebook 具有更高的优先级并出现在顶部。
Elastic Ebook
Ebook for Rails
Booking for an ebook.
有什么办法可以做到这一点。
我的代码 document.rb :
require 'elasticsearch/model'
class Document < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :user
Document.import force: true
def self.search(query)
__elasticsearch__.search({
query: {
multi_match: {
query: query,
fields: ['name^10', 'service'],
fuzziness: "AUTO"
}
}
})
end
settings index: {
"number_of_shards": 1,
analysis: {
analyzer: {
edge_ngram_analyzer: { type: "custom", tokenizer: "standard", filter:
["lowercase", "edge_ngram_filter", "stop", "kstem" ] },
}
},
filter: {
edge_ngram_filter: { type: "edgeNGram", min_gram: "3",
max_gram: "20" }
}
} do
mapping do
indexes :name, type: "string", analyzer: "edge_ngram_analyzer"
indexes :service, type: "string", analyzer: "edge_ngram_analyzer"
end
end
end
搜索控制器代码:
def search
if params[:query].nil?
@documents = []
else
@documents = Document.search params[:query]
end
end