我尝试使用elasticsearch-rails实现地理距离搜索,但我得到的不是地理位置字段错误也是triend字符串版本的纬度和经度。我正在使用elasticsearch版本1.0.2
error
"error_code": "103",
"error_message": "[400] {\"error\":\"SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[IiAHsrYDQW-nDOBQYlVHpg][barter_li_application_development][3]: SearchParseException[[barter_li_application_development][3]: query[title:rty*],from[-1],size[-1]: Parse Failure [Failed to parse source [{\\\"query\\\":{\\\"prefix\\\":{\\\"title\\\":\\\"rty\\\"}},\\\"filter\\\":{\\\"geo_distance\\\":{\\\"distance\\\":50,\\\"lon_lat\\\":{\\\"lon\\\":\\\"12\\\",\\\"lat\\\":\\\"12\\\"}}}}]]]; nested: QueryParsingException[[barter_li_application_development] field [lon_lat] is not a geo_point field]; }{[IiAHsrYDQW-nDOBQYlVHpg][barter_li_application_development][4]: SearchParseException[[barter_li_application_development][4]: query[title:rty*],from[-1],size[-1]: Parse Failure [Failed to parse source [{\\\"query\\\":{\\\"prefix\\\":{\\\"title\\\":\\\"rty\\\"}},\\\"filter\\\":{\\\"geo_distance\\\":{\\\"distance\\\":50,\\\"lon_lat\\\":{\\\"lon\\\":\\\"12\\\",\\\"lat\\\":\\\"12\\\"}}}}]]]; nested: QueryParsingException[[barter_li_application_development] field [lon_lat] is not a geo_point field]; }{[IiAHsrYDQW-nDOBQYlVHpg][barter_li_application_development][1]: SearchParseException[[barter_li_application_development][1]: query[title:rty*],from[-1],size[-1]: Parse Failure [Failed to parse source [{\\\"query\\\":{\\\"prefix\\\":{\\\"title\\\":\\\"rty\\\"}},\\\"filter\\\":{\\\"geo_distance\\\":{\\\"distance\\\":50,\\\"lon_lat\\\":{\\\"lon\\\":\\\"12\\\",\\\"lat\\\":\\\"12\\\"}}}}]]]; nested: QueryParsingException[[barter_li_application_development] field [lon_lat] is not a geo_point field]; }{[IiAHsrYDQW-nDOBQYlVHpg][barter_li_application_development][2]: SearchParseException[[barter_li_application_development][2]: query[title:rty*],from[-1],size[-1]: Parse Failure [Failed to parse source [{\\\"query\\\":{\\\"prefix\\\":{\\\"title\\\":\\\"rty\\\"}},\\\"filter\\\":{\\\"geo_distance\\\":{\\\"distance\\\":50,\\\"lon_lat\\\":{\\\"lon\\\":\\\"12\\\",\\\"lat\\\":\\\"12\\\"}}}}]]]; nested:
代码
module Searchable
extend ActiveSupport::Concern
included do
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
# Customize the index name
#
index_name [Rails.application.engine_name, Rails.env].join('_')
# Set up index configuration and mapping
#
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mapping do
# indexes :title, type: 'multi_field' do
indexes :title, analyzer: 'not_analyzed'
# indexes :tokenized, analyzer: 'simple'
# end
indexes :lon_lat, type: 'geo_point'
end
end
# Set up callbacks for updating the index on model changes
#
# after_commit lambda { Indexer.perform_async(:index, self.class.to_s, self.id) }, on: :create
# after_commit lambda { Indexer.perform_async(:update, self.class.to_s, self.id) }, on: :update
# after_commit lambda { Indexer.perform_async(:delete, self.class.to_s, self.id) }, on: :destroy
# after_touch lambda { Indexer.perform_async(:update, self.class.to_s, self.id) }
# Customize the JSON serialization for Elasticsearch
#
def as_indexed_json(options={})
{
title: self.title,
lon_lat: lon_lat
}
end
def lon_lat
[self.location.longitude.to_f, self.location.latitude.to_f]
end
def self.search(query)
__elasticsearch__.search(
{
query: { prefix: { title: query[:title] } },
filter: {
geo_distance: {
distance: query[:radius],
lon_lat: {
lon: query[:longitude],
lat: query[:latitude]
}
}
}
}
)
end
end
end
答案 0 :(得分:2)
问题在于没有创建索引 在导入之前应首先创建索引 捆绑exec rake环境elasticsearch:import:model CLASS =' Book' https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-model#index-configuration