1

我的环境是:

gem 'rails', '~> 5.2'
gem 'searchkick', '~> 3.1', '>= 3.1.2'

Elasticsearch6.1.2 elasticsearch-analysis-ik 6.1.2

现在ik包已导入到 /usr/local/var/elasticsearch/plugins 这个里,启动elasticsearch 可以看到 ik已经运行了。

[2018-11-18T11:55:03,154][INFO ][o.e.p.PluginsService     ] [FYGMHBZ] loaded plugin [analysis-ik]

Article Model

class Article < ApplicationRecord
  searchkick word_middle: [:title, :content],language: "chinese"

  def search_data
    {
        title: title,
        content: content
    }
  end
Article.reindex
end

search controller

class SearchController < ApplicationController
  before_action :logged_in_user

  search_params = { page: params[:page],
                      per_page: 50,
                      highlight: { tag: "<strong class='key'>" }
    }
    
    @result = Article.search(params[:q], search_params)

end

search index.html.erb

<div class="container mbt20">
  <div class="col-sm-12 mbt20 text-center">
    <h1 class="mbt20"><span class="mr10"><strong class="text-blue">U</strong><strong class="text-yellow">D</strong><strong class="text-red">A</strong>SK</span>搜索</h1>
  </div>
  <div class="col-sm-10 col-sm-offset-1 mt15">
    <%= form_for search_path, method: :get do |f| %>
      <div class="col-sm-10 plr5">
        <%= text_field_tag :q, params[:q], class: "input-lg form-control", placeholder: "输入关键字搜索" %>
      </div>
      <div class="col-sm-2 plr5">
        <%= f.submit "立即搜索", class: "btn btn-primary btn-lg" %>
      </div>
    <% end %>
  </div>
  <div class="col-sm-12 mbt20 search-result">
    <% if @articles.nil? %>
    <span>没有任何结果</span>
      <% else %>
      <% @articles.with_highlights.each do |article, highlights| %>
        <div class="groups-list">
          <p>
          <%= link_to highlights[:title],article %>
          </p>
          <%= highlights[:content] %>
        </div>
      <% end %>
      <div class="paginationBox">
        <%= paginate @articles,left:2,right:1,window: 1 %>
      </div>
      <% end %>

  </div>
</div>

搜索后,控制台出现的提示:

 Article Search (26.8ms)  curl http://localhost:9200/articles_development/_search?pretty -H 'Content-Type: application/json' -d '{"aggs":{"category_name":{"terms":{"field":"category_name","size":1000}}},"query":{"dis_max":{"queries":[{"multi_match":{"query":"2","boost":10,"operator":"and","analyzer":"searchkick_search","fields":["*.analyzed"],"type":"best_fields"}},{"multi_match":{"query":"2","boost":10,"operator":"and","analyzer":"searchkick_search2","fields":["*.analyzed"],"type":"best_fields"}},{"multi_match":{"query":"2","boost":1,"operator":"and","analyzer":"searchkick_search","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true,"fields":["*.analyzed"],"type":"best_fields"}},{"multi_match":{"query":"2","boost":1,"operator":"and","analyzer":"searchkick_search2","fuzziness":1,"prefix_length":0,"max_expansions":3,"fuzzy_transpositions":true,"fields":["*.analyzed"],"type":"best_fields"}}]}},"highlight":{"fields":{"*.analyzed":{}},"fragment_size":0,"pre_tags":["\u003cstrong class='key'\u003e"],"post_tags":["\u003c/strong\u003e"]},"timeout":"11s","_source":false,"size":50,"from":0}'

现在我的出现的错误是:

1、如果输入英文的话,当标题和内容全为英文时,可以正确匹配;

但是输入中文的时候,不能准确匹配,很多非相关的字也会被添加标签。如图:

图片描述

2、 如果标题含有字符串,不能全部搜索,只能搜索一部分 如图:

图片描述

3、如果搜索数字的话,搜索结果显示不准确,不是输入的关键字,也会显示。 如图:

图片描述

请问下大家,该怎么设置,该怎么做?非常感谢。

2018-11-18 提问
0 个回答

撰写答案

推广链接