[英]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
mapping
和indexes
在item.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.