Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

ruby - 如何在ruby gem SearchKick中将字段添加到ElasticSearch以便按顺序排序(How to add a field to ElasticSearch in order to order by it in ruby gem SearchKick)

I have a project, where I use Searchkick gem in order to fetch records from ElasticSearch first and then do further processing on them.

(我有一个项目,在这里我使用Searchkick gem,以便首先从ElasticSearch中获取记录,然后对其进行进一步处理。)

The guys that initially implemented it added a sort option by two fields in base options in SearchKick:

(最初实现它的家伙在SearchKick的基本选项中通过两个字段添加了一个排序选项:)

def base options
{
   fields: %i[catalogue_number oe_numbers],
   match: :word_start,
   misspellings: false,
   where: @conditions,
   load: false,
   order: { sale_rotation_group: { order: :desc } }, { lauber_id_integer: { order: :desc } },
   highlight: @params[:highlight]
}
end

lauber_it_integer does not exist in the application database so it must be a field in ElasticSearch that they added.

(lauber_it_integer在应用程序数据库中不存在,因此它必须是他们添加的ElasticSearch中的一个字段。)

Now I want to change this and instead of current order I want to tell SearchKick to tell ElasticSearch to order the records by the fields I added to the application database: images_count and parameters_count .

(现在,我想更改它,而不是当前顺序,我想告诉SearchKick告诉ElasticSearch通过我添加到应用程序数据库中的字段images_countparameters_count来对记录进行排序。)

So probably I need to add those new fields to ElasticSearch so it knows how to order the records.

(所以可能我需要将这些新字段添加到ElasticSearch中,以便它知道如何对记录进行排序。)

I changed the order option to

(我将订单选项更改为)

order: { images_count: { order: :desc } }

but now I am getting the error:

(但是现在我得到了错误:)

Searchkick::InvalidQueryError ([400] {"error":{"root_cause":[{"type":"query_shard_exception","reason":"No mapping found for [images_count] in order to sort on","index_uuid":"WhC4XK8IRnmmfkPJMNmV1g","index":"parts_development_20191124205133405"}]

This will probably involve also some extra work on ElasticSearch to add data to those new fields, however I know very little about ElasticSearch.

(这可能还涉及在ElasticSearch上进行一些额外的工作,以将数据添加到这些新字段中,但是我对ElasticSearch知之甚少。)

Could you give me some indications or hints on how I could solve my problem?

(您能给我一些有关如何解决问题的指示或提示吗?)

  ask by jedi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to add elasticsearch indexes to each new field you want to be orderable / searchable.

(您需要将弹性搜索索引添加到要可排序/可搜索的每个新字段。)

Here is a decent guide to setting one up from scratch which explains all the basic concepts, elasticsearch has its own schema so you need to get that to match your new one.

(是一个不错的指南,从头开始进行设置,解释了所有基本概念,elasticsearch具有自己的架构,因此您需要获取它以匹配新的架构。)

The example they give is:

(他们给出的示例是:)

class Post < ApplicationRecord
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings do
    mappings dynamic: false do
      indexes :author, type: :text
      indexes :title, type: :text, analyzer: :english
      indexes :body, type: :text, analyzer: :english
      indexes :tags, type: :text, analyzer: :english
      indexes :published, type: :boolean
    end
  end
end

You should see a similar section on whatever model you're searching for.

(您将在所搜索的任何模型上看到类似的部分。)

Just add the fields you want to have indexed to the model file, and then from console call (replacing Post with your model name):

(只需将要索引的字段添加到模型文件中,然后从控制台调用中添加(将Post替换为您的模型名称):)

Post.__elasticsearch__.delete_index!
Post.import force: true

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...