簡體   English   中英

搜索#search中的Rails:Elasticsearch :: Transport :: Transport :: Errors :: BadRequest

[英]Rails: Elasticsearch::Transport::Transport::Errors::BadRequest in Search#search

我在Rails應用程序上使用了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 mappingindexesitem.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>

嘗試在Rails控制台ModelName.reindex

從中篩選出結果的模型。 像書

所以Book.reindex

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM