简体   繁体   中英

Rails: Elasticsearch::Transport::Transport::Errors::BadRequest in Search#search

I'm using the elasticsearch gem on my rails app. I'm trying to implement a basic search and sort the results by the distance from the current location. But I'm getting the following error when trying to search:

[400] {"error":{"root_cause":[{"type":"parse_exception","reason":"illegal latitude value [269.99999983236194] for [GeoDistanceSort] for field [distance_type]."}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"items","node":"AfyW4Pa4S-qKhua-3lY4rg","reason":{"type":"parse_exception","reason":"illegal latitude value [269.99999983236194] for [GeoDistanceSort] for field [distance_type]."}}]},"status":400}

I have created a search_controller with the following methods:

class SearchController < ApplicationController

  def show
    @items = Item.search(query).records.to_a
  end

  def search
    if params[:q].nil?
      @items = []
    else
      @items = Item.search params[:q]
    end
  end
end

and I also add the elasticsearch mapping and indexes in the item.rb model:

require 'elasticsearch/model'

class Item < ApplicationRecord

  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
  include Elasticsearch::Model::Indexing

   settings index: { number_of_shards: 1 } do
     mappings dynamic: 'false' do
      indexes :title, analyzer: 'english', index_options: 'offsets'
      indexes :description, analyzer: 'english'
      indexes :location, type: 'geo_point'
    end
  end

  def location
    [longitude.to_f, latitude.to_f]
  end

  def current_location
    location = request.location
  end


  def self.search(query)
    __elasticsearch__.search(
        {
            query: {
                multi_match: {
                    query: query,
                    fields: ['title^10', 'description', 'distance']
                }
            },
            sort: [
                {
                    _geo_distance: {
                        "pin.location": ["current_location"],
                        distance: ["radius"],
                        unit: ["km"],
                        mode: ["min"],
                        order: ["asc"],
                        distance_type: ["arc"],

                     }
                }
            ],

            highlight: {
                pre_tags: ['<em>'],
                post_tags: ['</em>'],
                fields: {
                    title: {},
                    description: {},
                 }
             }
        }
    )
  end
 end
Item.__elasticsearch__.client.indices.delete index: Item.index_name rescue nil

Item.__elasticsearch__.client.indices.create \
index: Item.index_name,
body: { settings: Item.settings.to_hash, mappings: Item.mappings.to_hash }

Item.import(force: true)

and finally , this is my view search.html.erb

<%= form_for search_path, method: :get do |f| %>
    <p>
      <%= f.label "Search for" %>
      <%= text_field_tag :q, params[:q] %>
      <%= submit_tag "Nearby", name: nil %>
    </p>
<% end %>

<ul>
  <% @items.each do |item| %>
      <li>
        <h3>
          <%= link_to item.try(:highlight).try(:title) ? item.highlight.title[0].html_safe : item.title,
                  controller: "search",
                  action: "show",
                  id: item._id%>
        </h3>
        <% if item.try(:highlight).try(:description) %>
            <% item.highlight.description.each do |snippet| %>
                <p><%= snippet.html_safe %>...</p>
            <% end %>
        <% end %>
      </li>
  <% end %>
</ul>

try on rails console ModelName.reindex

Model from which you are filtering out the result . like Book

so Book.reindex

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM