如何在ElasticSearch中添加字段,以便在ruby gem SearchKick中对其进行排序

问题描述 投票:0回答:2

我有一个项目,在这里我使用Searchkick gem,以便首先从ElasticSearch中获取记录,然后对其进行进一步处理。最初实现它的家伙在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在应用程序数据库中不存在,因此它必须是他们添加的ElasticSearch中的一个字段。

现在,我要更改此设置,而不是当前顺序,我想告诉SearchKick告诉ElasticSearch根据我添加到应用程序数据库中的字段对记录进行排序:images_countparameters_count。所以可能我需要将这些新字段添加到ElasticSearch中,以便它知道如何对记录进行排序。我将订单选项更改为

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

但是现在我得到了错误:

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"}]

这可能还会涉及ElasticSearch上的一些额外工作,以将数据添加到这些新字段中,但是我对ElasticSearch知之甚少。您能否给我一些有关如何解决问题的指示或提示?

ruby elasticsearch searchkick
2个回答
0
投票

您需要向每个要订购/可搜索的新字段添加elasticsearch索引。

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

他们给出的示例是:

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

您将在搜索的任何型号上看到相似的部分。

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

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

0
投票

我在searchkick文档中找到了解决方案。

更新search_data定义

def search_data
  {
    ...,
    images_count:         images_count,
    parameters_count:     parameters_count
  }
end

更新搜索提示选项:

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

重新索引模型

Part.reindex

它将自动添加索引。

© www.soinside.com 2019 - 2024. All rights reserved.