3

i tried to implement geo distance search with elasticsearch-rails but i am getting not a geopoint field error also triend with string version of latitude and longitude. i am using elasticsearch version 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:

the code

   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
surendar
  • 656
  • 1
  • 9
  • 27

1 Answers1

2

The issue was with index was not created index should be created at first before importing with bundle exec rake environment elasticsearch:import:model CLASS='Book' https://github.com/elasticsearch/elasticsearch-rails/tree/master/elasticsearch-model#index-configuration

surendar
  • 656
  • 1
  • 9
  • 27