首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >elasticsearch-rails不是地理点字段错误

elasticsearch-rails不是地理点字段错误
EN

Stack Overflow用户
提问于 2014-05-24 00:25:26
回答 1查看 1K关注 0票数 3

我尝试使用elasticsearch-rails实现地理距离搜索,但我得到的不是geopoint字段错误,还包括字符串版本的纬度和经度。我使用的是elasticsearch版本1.0.2

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

代码

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

回答 1

Stack Overflow用户

发布于 2014-05-26 14:22:33

问题是没有创建索引,应该在使用包执行rake环境导入之前首先创建索引elasticsearch:import:model CLASS='Book‘https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-model#index-configuration

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23834187

复制
相关文章

相似问题

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