1

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

我的 Gemfile 的相关部分

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

弹性搜索版本:

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

正在阻止弹性搜索更新映射所以我在#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
}

现在映射就可以了:

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

问题,再一次

为什么会这样?

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

它可以以更清洁的方式修复吗?

4

1 回答 1

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
于 2013-08-04T03:23:28.577 回答