最近,我遇到了一个关于使用rails4/mongoid4/tire进行elasticsearch的奇怪行为。我设法做了一个临时修复,但我想知道是否有更干净的解决方案,以及问题到底出在哪里(是elasticsearch问题吗?)
我的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组成:
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‘索引的映射
{
"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"
}
}
}
}
}问题又一次出现了
为什么会发生这种情况?
堆栈的哪个部分对此负责?
它能以更干净的方式修复吗?
发布于 2013-08-04 11:23:29
我是评论中的那个人,看起来这个问题已经在轮胎头上解决了,看看这个问题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
endhttps://stackoverflow.com/questions/18018682
复制相似问题