【问题标题】:Elasticsearch mapping not updated after inserting new document via tire (mongoid4, rails4)通过轮胎(mongoid4,rails4)插入新文档后,Elasticsearch 映射未更新
【发布时间】:2013-08-02 13:55:15
【问题描述】:

最近我遇到了一个关于使用 rails4/mongoid4/tire 的弹性搜索的奇怪行为。我设法进行了临时修复,但我想知道是否有更清洁的解决方案以及问题的确切位置(是弹性搜索问题吗?)

我的 Gemfile 的相关部分

gem 'rails', '4.0.0'
gem "mongoid", github: 'mongoid/mongoid'
gem 'tire'

Elasticsearch 版本:

  "version" : {
    "number" : "0.90.2",
    "snapshot_build" : false,
    "lucene_version" : "4.3.1"
  }

我的模特:

我的模型的相关部分由 Ad 类组成:

class Ad
  include Mongoid::Document
  field :title, type: String
  [... other stuff...]
end

和 Ad 子类,其中之一是:

class AdInAutomotiveAutomobile < Ad
  field :make
  field :model
  field :body_type
  tire.index_name 'ads'
  [... other stuff ...]
end

使用继承似乎没有任何重要性,但我只是为了记录而提及它

问题

插入新广告不会更新“广告”索引的映射

{
  "ads": {
    "ad_in_automotive_automobile": {
      "properties": {
        "$oid": {
          "type": "string"
        }
      }
    }
  }
}

日志输出,精简:

# 2013-08-02 15:40:58:387 [ad_in_automotive_automobile/51fbb6b26f87e9ab1d000001] ("ads")
#
curl -X POST "http://localhost:9200/ads/ad_in_automotive_automobile/51fbb6b26f87e9ab1d000001" -d '{
  "_id": {
    "$oid": "51fbb6b26f87e9ab1d000001"
  },
  "active": null,
  "body_type": "hatchback",
  "c_at": "2013-08-02T13:40:57.647Z",
  "category_id": {
    "$oid": "51e8020c6f87e9b8e0000001"
  },
  "color": null,
  "description": null,
  "engine_displacement": null,
  "expire_at": null,
  "fuel_type": null,
  "locale": null,
  "make": "ford",
  "meta": {},
  "mileage": null,
  "model": "focus",
  "power": null,
  "price": null,
  "title": "foo",
  "transmission": null,
  "u_at": "2013-08-02T13:40:57.647Z",
  "year": null,
  "category_slug": "automotive-automobile"
}'

# 2013-08-02 15:40:58:388 [201]
#
# 
{
  "ok": true,
  "_index": "ads",
  "_type": "ad_in_automotive_automobile",
  "_id": "51fbb6b26f87e9ab1d000001",
  "_version": 1
}

解决方案

不知何故,这是:

"_id":{"$oid":"51fbb6b26f87e9ab1d000001"}

正在阻止 elasticsearch 更新映射 所以我在#to_indexed_json 方法中“修复”了这个问题:

  def to_indexed_json
    to_json(methods: [:category_slug]).gsub( /\{\"\$oid\"\:(\".{24}\")\}/ ) { $1 }
  end

结果:

# 2013-08-02 15:50:08:689 [ad_in_automotive_automobile/51fbb8fb6f87e9ab1d000002] ("ads")
#
curl -X POST "http://localhost:9200/ads/ad_in_automotive_automobile/51fbb8fb6f87e9ab1d000002" -d '{
  "_id": "51fbb8fb6f87e9ab1d000002",
  "active": null,
  "body_type": "hatchback",
  "c_at": "2013-08-02T13:50:08.593Z",
  "category_id": "51e8020c6f87e9b8e0000001",
  "color": null,
  "description": null,
  "engine_displacement": null,
  "expire_at": null,
  "fuel_type": null,
  "locale": null,
  "make": "ford",
  "meta": {},
  "mileage": null,
  "model": "focus",
  "power": null,
  "price": null,
  "title": "foo",
  "transmission": null,
  "u_at": "2013-08-02T13:50:08.593Z",
  "year": null,
  "category_slug": "automotive-automobile"
}'

# 2013-08-02 15:50:08:690 [201]
#
# 
{
  "ok": true,
  "_index": "ads",
  "_type": "ad_in_automotive_automobile",
  "_id": "51fbb8fb6f87e9ab1d000002",
  "_version": 1
}

现在映射就OK了:

{
  "ads": {
    "ad_in_automotive_automobile": {
      "properties": {
        "$oid": {
          "type": "string"
        },
        "body_type": {
          "type": "string"
        },
        "c_at": {
          "type": "date",
          "format": "dateOptionalTime"
        },
        "category_id": {
          "type": "string"
        },
        "category_slug": {
          "type": "string"
        },
        "make": {
          "type": "string"
        },
        "meta": {
          "type": "object"
        },
        "model": {
          "type": "string"
        },
        "title": {
          "type": "string"
        },
        "u_at": {
          "type": "date",
          "format": "dateOptionalTime"
        }
      }
    }
  }
}

问题再一次

为什么会这样?

堆栈的哪一部分对此负责?

可以用更干净的方式修复吗?

【问题讨论】:

  • 我遇到了同样的问题,这让我在墙上撞了一会儿,直到我查看了弹性搜索日志,因为轮胎默默地失败了。

标签: mongoid elasticsearch ruby-on-rails-4 tire


【解决方案1】:

我是评论中的那个人,看起来这是在轮胎 HEAD 中修复的,看这个问题https://github.com/karmi/tire/issues/775。自从我猴子修补课程以来,我还没有验证修复。如果你想这样做,这是补丁:

require "tire"
module Tire
  class Index
    def get_id_from_document(document)
     case
        when document.is_a?(Hash)
          document[:_id] || document['_id'] || document[:id] || document['id']
        when document.respond_to?(:id) && document.id != document.object_id
          document.id.to_s   # was document.id.as_json
      end
    end
  end
end

【讨论】:

  • 我很熟悉这个问题,我正在使用这个初始化程序,但是它对我不起作用 - 在 #to_indexed_json 中清理 json 可以。
猜你喜欢
  • 2013-02-28
  • 2013-07-21
  • 2015-02-02
  • 2012-10-26
  • 1970-01-01
  • 2012-02-06
  • 1970-01-01
  • 1970-01-01
  • 2012-09-24
相关资源
最近更新 更多