弹性搜索未在导轨

时间:2017-07-24 09:25:56

标签: ruby-on-rails elasticsearch elasticsearch-rails elasticsearch-ruby

我有一个文档模型,我正在使用弹性搜索

我的文档模型如下

    require 'elasticsearch/model'

    class Document < ApplicationRecord
    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
    belongs_to :user

    def self.search(query)
    __elasticsearch__.search(
    {
      query: {
      multi_match: {
      query: query,
      fields: ['name', 'service', 'description', 'tat', 'status']
    }
   },
   highlight: {
    pre_tags: ['<em>'],
    post_tags: ['</em>'],
    fields: {
      name: {},
      service: {},
      description: {},
      tat: {},
      status: {}
    }
   }
   }

  ) 
    end

  settings index: { number_of_shards: 1 } do
  mappings dynamic: 'false' do
  indexes :name, analyzer: 'english', index_options: 'offsets'
  indexes :service, analyzer: 'english'
  indexes :description, analyzer: 'english'
  indexes :tat, analyzer: 'english'
  indexes :status, analyzer: 'english'
  end
  end


 end

 Document.import force: true
 Document.__elasticsearch__.create_index! force: true 

我的搜索控制器如下:

 def search
 if params[:q].nil?
  @documents = []
 else
  @documents = Document.search(params[:q])
 end
 end

我的搜索视图如下:

  <h1>Document 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>
  <% @documents.each do |document| %>
  <li>
  <h3>
    <%= link_to document.try(:highlight).try(:name) ? 
  document.highlight.name[0].html_safe : document.name,
    controller: "documents", action: "show", id: document._id %>
  </h3>
  <% if document.try(:highlight).try(:description) %>
    <% document.highlight.description.each do |snippet| %>
      <p><%= snippet.html_safe %>...</p>
    <% end %>
  <% end %>
  <% if document.try(:highlight).try(:service) %>
    <% document.highlight.service.each do |snippet| %>
      <p><%= snippet.html_safe %>...</p>
    <% end %>
  <% end %>
  <% if document.try(:highlight).try(:tat) %>
    <% document.highlight.tat.each do |snippet| %>
      <p><%= snippet.html_safe %>...</p>
    <% end %>
  <% end %>
  <% if document.try(:highlight).try(:status) %>
    <% document.highlight.status.each do |snippet| %>
      <p><%= snippet.html_safe %>...</p>
    <% end %>
  <% end %>


 </li>

  <% end %>
 </ul>

现在,当我使用rental作为查询参数名称中的值运行查询时,即使具有名称簿的记录存在,它也不会在我的搜索页面上显示数据。在我的搜索控制器行上运行调试器

  @documents = Document.search(params[:q])

这就是我得到的

 <Elasticsearch::Model::Response::Response:0x007fcdde617430 @klass=[PROXY] 
 Document(id: integer, name: string, service: string, stamp_required: boolean, 
 default_price: string, tat: string, automated: boolean, template_url: string, 
 status: string, description: string, priorty: string, user_id: integer, 
 created_at: datetime, updated_at: datetime), @search=#

<Elasticsearch::Model::Searching::SearchRequest:0x007fcdde6175e8 @klass=
[PROXY] Document(id: integer, name: string, service: string, stamp_required:    
boolean, default_price: string, tat: string, automated: boolean, template_url: 
string, status: string, description: string, priorty: string, user_id: 
integer, created_at: datetime, updated_at: datetime), @options={}, 
@definition={:index=>"documents", :type=>"document", :body=>{:query=>
{:multi_match=>{:query=>"Rental Agreement", :fields=>["name", "service", 
"description", "tat", "status"]}}, :highlight=>{:pre_tags=>["<em>"], 
:post_tags=>["</em>"], :fields=>{:name=>{}, :service=>{}, :description=>{}, 
:tat=>{}, :status=>{}}}}}>>

我需要显示搜索结果。我做错了什么?

1 个答案:

答案 0 :(得分:1)

Document.__elasticsearch__.create_index! force: true实际上被迫创建索引并清除那里的所有记录。

还有一些其他方法可以创建索引,如:

index_klass.__elasticsearch__.client.indices.create(index: 'index_name')

您可以传递更多选项,例如映射/设置,但您的数据将始终保持不被触及!

此致