简体   繁体   English

如何在Elasticsearch中将关键字更改为数字?

[英]How To Change Keyword to Numeric in Elasticsearch?

Hello I have an error about Rails Elasticsearch with searchkick gem 您好,我有关于带有searchkick gem Rails Elasticsearch的错误

the error 错误

Searchkick::InvalidQueryError ([400] {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Expected numeric type on field [item_final_price], but got [keyword]"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":"items_development_20180507023923980","node":"w7LqCILTTK-gndnoQB1cOQ","reason":{"type":"illegal_argument_exception","reason":"Expected numeric type on field [item_final_price], but got [keyword]"}}]},"status":400}):

Basically telling me the error is expecting numeric type but the type is keywords 基本上告诉我错误是期望数字类型,但类型是关键字

heres my search params 这是我的搜索参数

in controller.rb 在controller.rb中

def results
    price_ranges = [{to: params[:max_price]}, {from: params[:min_price], to: params[:max_price]}, {from: params[:min_price]}]
    @results = Item.search(params[:q], aggs: {item_final_price: {ranges: price_ranges}}, page: params[:page], per_page: 10) if params[:q].present?
end

my views 我的看法

<%= form_tag results_path, method: :get, enforce_utf8: false, id: "q_filter" do %>
<section class="widget widget-categories">
    <%= hidden_field_tag :q, params[:q] %>
    <h3 class="widget-title">Price Range</h3>
    <div class="form-group">
        <label>Price Between</label>
        <%= number_field_tag :min_price, params[:min_price].to_f, class: "form-control form-control-sm", placeholder: "Min Price" %>
    </div>
    <div class="form-group">
        <label>And</label>
        <%= number_field_tag :max_price, params[:max_price].to_f, class: "form-control form-control-sm", placeholder: "Max Price" %>
    </div>
    <%= button_tag(type: "submit", name: nil, class: "btn btn-outline-primary btn-sm btn-block") do %>
        Filter Search
    <% end %>
</section>

and my model 和我的模特

include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
Item.import

searchkick callbacks: :async

def search_data
    {
        item_name: item_name,
        item_details: item_details,
        item_final_price: item_final_price
    }
end

I found my Answer. 我找到了答案。 What I did is change the column datatype from string to float. 我所做的是将列数据类型从字符串更改为浮点型。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM