【问题标题】:Indexing on column field is not working with ElasticSearch列字段的索引不适用于 ElasticSearch
【发布时间】:2015-10-30 22:53:55
【问题描述】:

我正在我的模型中实现 ElasticSearch 集成:

require 'elasticsearch/model'
class MissionDef < ActiveRecord::Base
  # field: name (String(40))
  # field: icon (String(2000))
  # field: definition (String)
  # field: public, boolean

  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  def as_indexed_json(options={})
    self.as_json(
     only: [:id, :name]
     )
  end
end

在我的 Rails 控制台中我做到了:

MissionDef.import

MissionDef.first

MissionDef id: 226, ade_channel_key: "d403d658313e6c35ce", name: "Test Mission", icon: "/app/assets/images/badges/Showedup.png", definition: "{}", deleted_at: nil, created_at: "2015-08-04 11:30:08", updated_at: "2015-08-04 11:30:08", container_id: 883, public: true

我的查询

1) 当我使用 name 和 id 以外的其他字段值进行搜索时,它会为我提供搜索结果。 例如:

result = MissionDef.search 'app' --- 有效 结果.records.count => 1

我猜应该不是这样。

2) Missiondef.first.as_indexed_json => 无法正常工作,它给了我 o/p 作为 JSON 的整个对象

{"id"=>226, "ade_channel_key"=>"d403d658313e6c35ce", "name"=>"测试任务", "icon"=>"/app/assets/images/badges/Showedup.png", “定义”=>“{}”,“created_at”=>2015 年 8 月 4 日星期二 11:30:08 UTC +00:00,“updated_at”=>2015 年 8 月 4 日星期二 11:30:08 UTC +00: 00, "container_id"=>883, "public"=>true}

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 elasticsearch elasticsearch-model


    【解决方案1】:

    我之前没有使用过elasticsearch-rails gem,但默认情况下我知道elasticsearch 会搜索整个文档,所以如果您只想在查询中指定特定字段。因此,在您给定的示例中,app 与图标字段匹配:/app/assets/images/badges/Showedup.png

    尝试指定您要搜索的字段 - 在此处阅读他们的文档中的更多信息:https://github.com/elastic/elasticsearch-rails/blob/master/elasticsearch-model/README.md#the-elasticsearch-dsl

    所以在这种情况下,它可能是这样的:

    result = MissionDef.search query: {match:  {name: "app" }}
    result.records.count #=> should be 0 this time
    

    【讨论】:

    • 谢谢,但是我的 2) 查询也无法正常工作 Missiondef.first.as_indexed_json 而不是以 json 格式提供 id 和 name 它以 json 格式提供整个对象
    • 对不起 Mangala,我不确定 (2) 的答案可能是什么,但它看起来确实很奇怪。我之前没有使用过elasticsearch-rails,所以我不确定应该如何使用as_indexed_json 方法。但是,from the example in the documentation,我可以看到的一个区别是它不包含 Callbacks 模块,这可能是它的一部分?
    • 谢谢我已经解决了这两个问题的问题。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多