【发布时间】:2017-02-06 12:40:48
【问题描述】:
我正在使用 elasticsearch-rails 和 elasticsearch-model gem 在我的 rails 应用中搜索单词。
这是我要搜索的模型 article.rb:
require 'elasticsearch/model'
class Article < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def self.search(query)
__elasticsearch__.search(
{
query: {
multi_match: {
query: query,
fuzziness: 2,
fields: ['title^10', 'text']
}
},
highlight: {
pre_tags: ['<em>'],
post_tags: ['</em>'],
fields: {
title: {},
}
}
}
)
end
end
这是我的模型控制器 search_controller.rb
class SearchController < ApplicationController
def search
if params[:q].nil?
@articles = []
else
@articles = Article.search params[:q]
logger.info "LocationController.add_locations called with params: #{@articles.records.each_with_hit { |record, hit| puts "* #{record.title}: #{hit._score}" }}"
end
end
end
我正在获取搜索结果。 但我的问题是: 如果我搜索“John team”。
Articles.search('John team').records.records
我获得了多个完美匹配“john team”的记录以及一些与“john”或“team”相关的匹配项。
但我想要,如果“john team”在我的数据库中完全匹配,结果应该只有 john team。我不想要其他记录。但是如果“john team”不存在,我想要另一个与“john”相关的结果' 或 'team' 两个关键字。
示例:
Article.search('John team').records.records
responce: ('john team', 'team joy', 'John cena')
但我想要
Article.search('John team').records.records
responce: ('john team')
【问题讨论】:
标签: ruby-on-rails elasticsearch-rails