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

时间:2017-09-06 17:50:39

标签: ruby-on-rails ruby ruby-on-rails-4 elasticsearch

我正在使用我的rails app上的elasticsearch gem。我正在尝试实施基本搜索,并按当前位置的距离对结果进行排序。但是在尝试搜索时出现以下错误:

[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}

我使用以下方法创建了search_controller

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

我还在elasticsearch模型中添加了mapping indexesitem.rb

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)

最后,这是我的观点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>

1 个答案:

答案 0 :(得分:0)

尝试 在Rails Console上 ModelName.reindex

要从中过滤结果的模型。像书

所以 Book.reindex