Elastic Search 不在 Rails 中显示数据
Posted
技术标签:
【中文标题】Elastic Search 不在 Rails 中显示数据【英文标题】:Elastic Search not displaying data in rails 【发布时间】:2017-12-29 19:41:56 【问题描述】:我有一个文档模型,我正在使用弹性搜索
我的文档模型如下
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
我的搜索Controller如下:
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>
现在,当我在查询参数名称中以出租作为值运行查询时,即使存在带有姓名簿的记录,它也不会在我的搜索页面上显示数据。在为我的搜索控制器行运行调试器
@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=>>>
我需要显示搜索结果。我做错了什么?
【问题讨论】:
这个输出是什么:@documents.results.to_a
?
@zauzaj 这是我得到的 estamp 作为查询:[#<:model::response::result:0x007fc5f39c0c50 _id="3" _index="documents" _score="14.0215435" _source="#<Elasticsearch::Model::HashWrapper" automatic="true" created_at="2017-07-24T07:12:30.111Z" default_price="100" description="此文档为estamp" id="3" name="Estamp" priority="High" service="Estamp" stamp_required="true" status="Available" tat="dgfhj" template_url="vn" updated_at="2017-07-24T07:12:30.111 Z" user_id="1"> _type="document" highlight=#
=#<:model::hashwrapper name='["<em'>Estamp"] service=["Estamp"]>>>, #<:model::response::result:0x007fc5f39b3de8 _id="5" _index="documents" _score="9.678259" _source="#<Elasticsearch::Model::HashWrapper自动=true" created_at="2017-07-24T07:14:36.978Z" default_price="125" description="Franking and Estamp Services" id="5" name="Franking and Estamp" priority="High" service="Estamp" stamp_required="true" status="true" tat="asa" template_url="acac" updated_at="2017-07-24T07:14:36.978Z" user_id="1">
_type="document" highlight=#<:model::hashwrapper name='["Franking' and>Estamp"] service=["Estamp em>"]>>>]
抱歉,您能否使用这些数据更新您的问题,使其更加清晰和结构化,因为很难阐明这种方式的外观
【参考方案1】:
Document.__elasticsearch__.create_index! force: true
实际上是强制创建索引并清除那里的所有记录。
还有其他一些创建索引的方法,例如:
index_klass.__elasticsearch__.client.indices.create(index: 'index_name')
您可以传递更多选项,例如映射/设置,但您的数据将始终保持不变!
问候
【讨论】:
以上是关于Elastic Search 不在 Rails 中显示数据的主要内容,如果未能解决你的问题,请参考以下文章
我无法在 Elastic Beanstalk ec2 实例上找到生产日志文件
如何使用现有索引创建 Elastic App Search Engine(来自 GraphDB Elastic Search Connector 的数据)