【问题标题】:Why rails + elasticsearch don't hit为什么 rails + elasticsearch 不成功
【发布时间】:2015-01-13 19:14:43
【问题描述】:

今天我开始阅读有关 elasticsearch 的内容,并尝试在我的 ruby​​ on rails 应用程序中使用它。

所以我做了一些调整,就像我在示例中看到的那样:

https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/01-basic.rb

我的模特:

class Service < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks
end

我的控制器:

class ServicesController < ApplicationController
  (...)  
  # GET /services/search
  def search
    @services = Service.search(params[:q]).records
    render action: "index"
  end
  (...)
end

我的看法:

(...)
<%= form_tag search_services_path, method: 'get' do %>
<%= text_field_tag :q, params[:q] %>
<%= button_tag :search %>
<% end %>
(...)

我的路线:

Newapp::Application.routes.draw do
  (...)
  resources :services do
    collection { get :search }
  end
  (...)
end

但是当我找到任何东西时,我总是收到 0(零)个结果。

其他人认为我试图这样做:

$ curl -XPOST 'localhost:9200/services/_search?pretty' -d '
 { "query": { "match_all": {} } }'

返回:

{
  "took" : 2396,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

我需要做什么才能通过我的 rails 模型进行弹性搜索查找保存在我的数据库中的数据

感谢您的帮助!

【问题讨论】:

    标签: ruby-on-rails database search elasticsearch


    【解决方案1】:

    这篇文章足以解决我所有的疑惑。

    http://www.sitepoint.com/full-text-search-rails-elasticsearch/

    我的问题解决了:

    require 'elasticsearch/model'
    

    在我的模型顶部,并添加:

    Article.import # for auto sync model with elastic search
    

    在我的模型底部。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-29
      相关资源
      最近更新 更多