1

我正在尝试使用 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 似乎忽略了该字段的geo_point类型。location

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

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

location 字段被索引为文档上的 double 数组(结果http://localhost:9200/availabilities/_search):

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

当我将location方法更改为:

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

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

  location: {
    type: "string"
  },

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

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

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

谢谢 !

4

1 回答 1

2

location索引类型输入错误。

您使用geo_type而不是geo_point

indexes :location, type: 'geo_point', as: 'location'
于 2013-07-04T15:17:29.110 回答