gpt4 book ai didi

ruby-on-rails - 如何使用Elasticsearch Rails仅搜索教科书模型的标题?

转载 作者:行者123 更新时间:2023-12-03 01:56:04 25 4
gpt4 key购买 nike

编辑1:
我怎样才能使任何教科书:title具有“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”显示。
我只想查看搜索结果为“j​​ava”的书
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>

我的目标是从型号名称 :title中找到任何匹配的 :subjectTextbook属性

最佳答案

你这样子

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

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

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

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

关于ruby-on-rails - 如何使用Elasticsearch Rails仅搜索教科书模型的标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36565254/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com