【发布时间】:2016-08-02 14:13:23
【问题描述】:
编辑1: 我怎么能做任何教科书:标题有“java”?它只匹配一本完全“java”的教科书。如果书名是“java2”或“java-algo”,则不会显示..提前致谢!
您好,我安装了 elasticsearch,它正在工作。 我有教科书模型:
require 'elasticsearch/model'
class Textbook < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
belongs_to :user
#below is for sunspot
#searchable do
#text :title, :default_boost => 2
#text :subject
#end
#include ActiveModel::Validations
validates :title, :presence => true
validates :subject, :presence => true
validates :price, :presence => true
validates :offer, :presence => false
validates :user_email, :presence => true
validates :description, :presence => true
end
Textbook.import
但是当我搜索诸如“java”之类的东西时 不仅书名“java”而且所有属性都有“java”显示.. 我只想查看标题为“java”的书的搜索结果
class SearchController < ApplicationController
def search
if params[:search].nil?
#@textbooks = []
@textbooks = Textbook.all.order(created_at: :desc)
else
@textbooks = Textbook.search params[:search]
end
end
end
下面是views/textbooks/index.html.erb里面的搜索表单
<div>
<%= form_tag search_path, :method => :get do %>
<p>
<%= text_field_tag :search, params[:search], placeholder: "Search textbooks" %>
<%= submit_tag "Search", :title => nil, class: "btn btn-success btn-sm" %>
<% end %>
</div>
更新!#!#!#!#!#!#!
当我尝试如下建议时:
@textbooks = Textbook.search index: 'INDEXNAME', body: {query: { constant_score: { filter: {term: { title: params[:search]} } }}}
我很生气:
[400] {"error":{"root_cause":[{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [index]","line":1,"col":2}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"textbooks","node":"RPfuoi8FQLWkC8OIj5uUNQ","reason":{"type":"search_parse_exception","reason":"failed to parse search source. unknown search element [index]","line":1,"col":2}}]},"status":400}
Extracted source (around line #18):
<tbody>
<% @textbooks.each do |textbook| %>
<tr>
<!--<td><%= textbook.title %></td>-->
<td><%=link_to "#{textbook.title}", textbook_path(textbook.id) %></td>
我的目标是从模型名称 Textbook 中找到任何匹配的 :title 和 :subject 属性
【问题讨论】:
-
阅读本文,可能对您有所帮助。 github.com/elastic/elasticsearch-rails/tree/master/…
标签: ruby-on-rails search elasticsearch