简体   繁体   English

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

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

I'm using the elasticsearch gem on my rails app. 我在Rails应用程序上使用了elasticsearch gem 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: 我使用以下方法创建了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

and I also add the elasticsearch mapping and indexes in the item.rb model: 我也加入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)

and finally , this is my view search.html.erb 最后,这是我的视图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 尝试在Rails控制台ModelName.reindex

Model from which you are filtering out the result . 从中筛选出结果的模型。 like Book 像书

so Book.reindex 所以Book.reindex

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Elasticsearch ::运输::运输::错误:: BadRequest错误heroku - Elasticsearch::Transport::Transport::Errors::BadRequest error heroku Elasticsearch::Transport::Transport::Errors::BadRequest [400] 创建索引时 - Elasticsearch::Transport::Transport::Errors::BadRequest [400] while creating index Elasticsearch :: Transport :: Transport :: Errors :: NotFound:[404] - Elasticsearch::Transport::Transport::Errors::NotFound: [404] Search#search中的ActionController :: UrlGenerationError - ActionController::UrlGenerationError in Search#search 使用ActiveAdmin,SearchKick和SearchBox在Heroku上出现Elasticsearch :: Transport :: Transport :: Errors - Elasticsearch::Transport::Transport::Errors on Heroku with ActiveAdmin, SearchKick and SearchBox Activeadmin破坏记录,并在管理中出现Elasticsearch :: Transport :: Transport :: Errors :: NotFound异常[404] - Activeadmin Destroy Record with Exception Elasticsearch::Transport::Transport::Errors::NotFound in Admin [404] Elasticsearch::Transport::Transport::Errors::Forbidden: [403] 发送超过 1200 个字符的文本字段时出错 - Elasticsearch::Transport::Transport::Errors::Forbidden: [403] error when sending text fields with more than 1200 characters Elasticsearch :: Transport :: Transport :: Errors :: NotAcceptable:[406] {“错误”:“不支持Content-Type标头[]”,“状态”:406} - Elasticsearch::Transport::Transport::Errors::NotAcceptable: [406] {“error”:“Content-Type header [] is not supported”,“status”:406} Elasticsearch :: Transport :: Transport :: Errors :: NotFound([404] {“错误”:{“ root_cause”:[{“类型”:“ index_not_found_exception” - Elasticsearch::Transport::Transport::Errors::NotFound ([404] {“error”:{“root_cause”:[{“type”:“index_not_found_exception” Elasticsearch Rails搜索集合 - Elasticsearch rails search in collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM