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

标签 ruby-on-rails elasticsearch tire

我尝试使用 elasticsearch-rails 实现地理距离搜索,但我得到的不是地理点字段错误,也不是字符串版本的纬度和经度。我正在使用 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

最佳答案

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

关于ruby-on-rails - elasticsearch-rails 不是地理点字段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23834187/

相关文章:

elasticsearch - Elastic Search 5.1 将使用哪个版本的 Kibana?

elasticsearch - 多个并使用子请求进行查询。 Elasticsearch

elasticsearch - 如何使Elasticsearch忽略某些查询之间的空格?

ruby-on-rails - 载波,Rails 4; Errno::ENOENT(无此文件或目录-标识)

ruby-on-rails - 在 Windows 上安装 json gem 时出错

ruby-on-rails - 通过Tire gem进行Elasticsearch版本控制

elasticsearch - Elasticsearch 中的分析器

ruby-on-rails - Elasticsearch和Tire排除Deleted_at对象

ruby-on-rails - LinkedIn Oauth 错误 : 'The partner did did not properly implement the authentication protocol' on browser back after signin

ruby-on-rails - 在 rails 应用程序中安装 Sinatra 应用程序并共享布局