如何合并elasticsearch rails gem的两个或多个结果?

时间:2017-07-04 10:15:19

标签: elasticsearch merge ruby-on-rails-5 elasticsearch-rails

我正在尝试将两个elasticsearch结果合并为一个变量,这里我的代码尝试...

class SearchController < ApplicationController
    def index

    end

    def advanced
        @results = {}

        if !params[:fast_search].empty?
                @results[:features] = TestCases.search(params[:fast_search]).results
                @results[:steps] = Steps.search(params[:fast_search]).results
                @results[:examples] = Examples.search(params[:fast_search]).results
        else
            unless params[:feature].blank?
                features = TestCases.search(query: { match: { function: params[:feature] } }).results
                features_tag = Steps.search(query: { match: { tags: params[:tags] } }).results
                @results[:features] = features + features_tag
            end

            unless params[:steps].blank? || params[:scenario].blank?
                @results[:steps] = Steps.search(query: { match: { scenario: params[:scenario] } }).results
                params[:steps].each do |step|
                    @results[:steps] += Steps.search(query: { match: { steps: step } }).results
                end
                @results[:steps] += Steps.search(query: { match: { tags: params[:tags] } }).results
            end

            unless params[:examples].blank?
                params[:examples].each do |example|
                    @results[:examples] += Examples.search(query: { match: { examples: example } }).results
                end
                @results[:examples] += Examples.search(query: { match: { tags: params[:tags] } }).results
            end

            unless params[:bug].blank?
                @results[:miscs] = StepsMiscs.search(query: { match: { bug: true } }).results
            end
        end

        render "search/index"
    end
end

我也尝试features.merge(features_tag),但也没有成功。

这很简单,我只需要合并来自elasticsearch的一个或多个结果,但我根本不知道如何。

这是我的Gemfile:

source 'https://rubygems.org'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# jQuery-Turbolinks
gem 'jquery-turbolinks'

# Mysql
gem 'mysql2'

# Safe Attributes
gem 'safe_attributes'

# Elastic Search
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'elasticsearch-persistence'
gem 'pry'

#rake
gem 'rake'

#sidekiq
gem 'sidekiq'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console'
end

group :production do
  #passenger
  gem "passenger", require: "phusion_passenger/rack_handler"
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

这显然是我得到的错误:

undefined method `+' for #<Elasticsearch::Model::Response::Results:0x0000000d2719f8>

谢谢!

1 个答案:

答案 0 :(得分:0)

经过大量搜索,我发现了这个:

    unless params[:steps].blank? || params[:scenario].blank? || params[:tags].blank?
            query = Jbuilder.encode do |json|
              json.query do
                json.match do
                  json.scenario do
                    json.query params[:scenario]
                  end
                end
                params[:steps].each do |step|
                    json.match do
                      json.tags do
                        json.query step
                      end
                    end
                end
                json.match do
                  json.tags do
                    json.query params[:tags]
                  end
                end
              end
            end
            @results[:steps] = Steps.search(query).results
        end

这有助于我在所有模特上搜索多个匹配项。 我知道只抓住基础知识,但此刻是我需要的部分,我希望这可以帮助周围所有其他人同样的问题!

谢谢大家!