【问题标题】:How can I search only title of Textbook model using elasticsearch Rails?如何使用 elasticsearch Rails 仅搜索教科书模型的标题?
【发布时间】: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 属性

【问题讨论】:

标签: ruby-on-rails search elasticsearch


【解决方案1】:

你这样做

Textbook.search index: 'INDEXNAME', body: { query: { match: { title: params[:search] } } }

语法:

    Textbook.search index: 'INDEXNAME', body: { query: { match: { COLUMN_NAME: STRING } } }

对于不复杂和简单的用法: 你可以试试searchkick gem

使用简单直接的前言。它基本上建立在 elasticsearch gem 之上。

【讨论】:

  • 非常感谢!我怎么能做任何教科书:标题有“java”?您的建议仅与“java”完全匹配。如果书名是“java2”,则不会显示..提前致谢。
  • Textbook.search 索引:'INDEXNAME',正文:{query: { constant_score: { filter: {term: { COLUMN_NAME: STRING} } }}}
  • 为什么我收到Elasticsearch::Transport::Transport::Errors::BadRequest in Search#search_textbook 错误...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多