【问题标题】:Mapping of a geo_point field in ElasticSearch with Tire使用轮胎映射 ElasticSearch 中的 geo_point 字段
【发布时间】:2012-10-26 23:14:24
【问题描述】:

我正在尝试使用 Tire gem 索引 Elasticsearch 中的 geo_point 字段。这是我的 ActiveRecord 模型的轮胎映射:

class Availability < ActiveRecord::Base
  belongs_to :user
  attr_accessible :date, :latitude, :longitude

  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire do
    mapping do
      indexes :id,                  type: 'integer',  index: 'not_analysed'
      indexes :user_id,                 type: 'integer',  index: 'not_analysed'
      indexes :user_firstname,      type: 'string',   as: 'user_firstname' 
      indexes :user_lastname,       type: 'string',   as: 'user_lastname' 
      indexes :user_level,          type: 'integer',  as: 'user_level' 
      indexes :date,                type: 'date'
      indexes :location,            type: 'geo_type', as: 'location'
    end
  end

  # def location
  #   "#{latitude},#{longitude}"
  # end

  def location
    [longitude.to_f, latitude.to_f]
  end

  def user_firstname
    user.firstname
  end

  def user_lastname
    user.lastname
  end

  def user_level
    user.level
  end
end

当我创建映射 (bundle exec rake environment tire:import CLASS=Availability FORCE=true) 时,Elasticsearch 似乎忽略了 location 字段的 geo_point 类型。

这是http://localhost:9200/availabilities/_mapping 调用的结果:

{
  availabilities: {
    availability: {
      properties: {
        date: {...},
        id: {...},
        location: {
          type: "double"
        },
        user_firstname: {...},
        user_id: {...},
        user_lastname: {...},
        user_level: {...}
      }
    }
  }
}

位置字段被索引为文档上的双精度数组(http://localhost:9200/availabilities/_search 的结果):

{
  id: 8,
  ...
  location: [
    2.301643,
    48.780651
  ]
}

当我将location 方法更改为:

  def location
    "#{latitude},#{longitude}"
  end

根据文档 (http://www.elasticsearch.org/guide/reference/mapping/geo-point-type.html) 索引 geo_point 字段的另一种解决方案是位置映射的结果是:

  location: {
    type: "string"
  },

当然,位置字段被索引为字符串:

{
  id: 4,
  ...
  location: "48.780651,2.301643"
}

知道为什么geo_point 在我的映射中被忽略了吗?

谢谢!

【问题讨论】:

  • 这很奇怪:你能生成一个带有位置支持的空白轮胎应用程序(参见自述文件中的命令)吗?我已经尝试过,并且映射已正确存储。另外,您可以尝试在您的 Gemfile 中使用来自 Github 的版本吗?
  • 啊!!!我打错类型了!这不是geo_type,而是geo_point...奇怪的是我没有从elasticsearch中得到任何错误,但是在更新(到0.19.10)之后,elasticsearch转储了一个错误...感谢您的帮助,并对这个“错字问题”表示抱歉:)跨度>
  • 酷!很高兴它已经解决了。
  • 也许将您的解决方案添加为答案,因此问题被正确标记为“已回答”:)

标签: ruby-on-rails geolocation elasticsearch tire


【解决方案1】:

location 索引类型输入错误。

您使用了geo_type 而不是geo_point

indexes :location, type: 'geo_point', as: 'location'

【讨论】:

    猜你喜欢
    • 2013-07-21
    • 2013-02-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    相关资源
    最近更新 更多