Ruby on rails Elasticsearch rails如何提高结果的准确性?
我正在使用ElasticSearchRails和ElasticSearchModelGem在我的rails应用程序中搜索单词 这是我的模型文章.rb我想搜索的地方:Ruby on rails Elasticsearch rails如何提高结果的准确性?,ruby-on-rails,
elasticsearch-rails,Ruby On Rails,
elasticsearch Rails,我正在使用ElasticSearchRails和ElasticSearchModelGem在我的rails应用程序中搜索单词 这是我的模型文章.rb我想搜索的地方: require 'elasticsearch/model' class Article < ActiveRecord::Base include Elasticsearch::Model include Elasticsearch::Model::Callbacks settings index: { numb
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
这是我的搜索控制器。rb:
<h1>Articles Search</h1>
<%= form_for search_path, method: :get do |f| %>
<p>
<%= f.label "Search for" %>
<%= text_field_tag :q, params[:q] %>
<%= submit_tag "Go", name: nil %>
</p>
<% end %>
<ul>
<% @articles.each do |article| %>
<li>
<h3>
<%= link_to article.try(:highlight).try(:title) ? article.highlight.title[0].html_safe : article.title,
controller: "articles",
action: "show",
id: article._id%>
</h3>
<% if article.try(:highlight).try(:text) %>
<% article.highlight.text.each do |snippet| %>
<p><%= snippet.html_safe %>...</p>
<% end %>
<% end %>
</li>
<% end %>
</ul>
Rails.application.routes.draw do
resources :articles
get 'search', to: 'search#search'
end
class SearchController < ApplicationController
def search
if params[:q].nil?
@articles = []
else
@articles = Article.search params[:q]
end
end
end
class SearchController
现在,我只得到匹配单词的结果。
我的问题是:
如何进行复合匹配?
我不明白这个功能是如何工作的-
如何更改查询
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
def self.search(查询)
__弹性搜索(
{
查询:{
多重匹配:{
查询:查询,
字段:['title^10','text']
}
},
亮点:{
前标签:[''],
张贴标签:[''],
字段:{
标题:{},
正文:{}
}
}
}
)
结束
结束
还有,我如何提高合成匹配、拼写检查、部分匹配等准确性?
多谢各位