首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails: Search#search中的Elasticsearch::Transport::Transport::Errors::BadRequest

Rails: Search#search中的Elasticsearch::Transport::Transport::Errors::BadRequest
EN

Stack Overflow用户
提问于 2017-09-07 01:50:39
回答 1查看 1.2K关注 0票数 1

我正在我的rails应用上使用elasticsearch gem。我正在尝试实现一个基本的搜索,并根据距离当前位置的距离对结果进行排序。但我在尝试搜索时遇到以下错误:

代码语言:javascript
复制
[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

代码语言:javascript
复制
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

我还在item.rb模型中添加了elasticsearch mappingindexes

代码语言:javascript
复制
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

代码语言:javascript
复制
<%= 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>
EN

回答 1

Stack Overflow用户

发布于 2019-10-07 17:33:58

在rails控制台ModelName.reindex上试用

从中筛选出结果的模型。像书一样

所以Book.reindex

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46081467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档