首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过轮胎插入新文档后,Elasticsearch映射未更新(mongoid4、rails4)

通过轮胎插入新文档后,Elasticsearch映射未更新(mongoid4、rails4)
EN

Stack Overflow用户
提问于 2013-08-02 21:55:15
回答 1查看 362关注 0票数 1

最近,我遇到了一个关于使用rails4/mongoid4/tire进行elasticsearch的奇怪行为。我设法做了一个临时修复,但我想知道是否有更干净的解决方案,以及问题到底出在哪里(是elasticsearch问题吗?)

我的Gemfile的相关部分

代码语言:javascript
复制
gem 'rails', '4.0.0'
gem "mongoid", github: 'mongoid/mongoid'
gem 'tire'

Elasticsearch版本:

代码语言:javascript
复制
  "version" : {
    "number" : "0.90.2",
    "snapshot_build" : false,
    "lucene_version" : "4.3.1"
  }

我的模型:

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

代码语言:javascript
复制
class Ad
  include Mongoid::Document
  field :title, type: String
  [... other stuff...]
end

和Ad子类,其中之一是:

代码语言:javascript
复制
class AdInAutomotiveAutomobile < Ad
  field :make
  field :model
  field :body_type
  tire.index_name 'ads'
  [... other stuff ...]
end

使用继承似乎并不重要,但我只是为了记录而提及它

问题所在

插入新广告不会更新'ads‘索引的映射

代码语言:javascript
复制
{
  "ads": {
    "ad_in_automotive_automobile": {
      "properties": {
        "$oid": {
          "type": "string"
        }
      }
    }
  }
}

日志输出,已修剪:

代码语言:javascript
复制
# 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
}

解决方案

不知何故,这就是:

代码语言:javascript
复制
"_id":{"$oid":"51fbb6b26f87e9ab1d000001"}

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

代码语言:javascript
复制
  def to_indexed_json
    to_json(methods: [:category_slug]).gsub( /\{\"\$oid\"\:(\".{24}\")\}/ ) { $1 }
  end

这会导致:

代码语言:javascript
复制
# 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的:

代码语言:javascript
复制
{
  "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"
        }
      }
    }
  }
}

问题又一次出现了

为什么会发生这种情况?

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

它能以更干净的方式修复吗?

EN

回答 1

Stack Overflow用户

发布于 2013-08-04 11:23:29

我是评论中的那个人,看起来这个问题已经在轮胎头上解决了,看看这个问题https://github.com/karmi/tire/issues/775。自从我用猴子修补了这个类之后,我还没有验证过这个修复。这是补丁,如果你想这样做的话:

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18018682

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档