Sha256: 269333c6be9610693961700b2018bab92dad24a087a0ee9ae2c69f8b9d2b4e5d

Contents?: true

Size: 1.57 KB

Versions: 13

Compression:

Stored size: 1.57 KB

Contents

require "elasticity/search"
require "elasticity/multi_search"

RSpec.describe Elasticity::MultiSearch do
  let :client do
    double(:client)
  end

  let :klass do
    Class.new do
      include ActiveModel::Model
      attr_accessor :_id, :name

      def self.from_hit(hit)
        new(_id: hit["_id"], name: hit["_source"]["name"])
      end

      def ==(other)
        self._id == other._id && self.name == other.name
      end
    end
  end

  let :response do
    {
      "responses" => [
        { "hits" => { "total" => 2, "hits" => [{ "_id" => 1, "_source" => { "name" => "foo" }}, { "_id" => 2, "_source" => { "name" => "bar" }}]}},
        { "hits" => { "total" => 1, "hits" => [{ "_id" => 3, "_source" => { "name" => "baz" }}]}},
      ]
    }
  end

  it "performs multi search" do
    subject.add(:first, Elasticity::Search::Facade.new(client, Elasticity::Search::Definition.new("index_first", "document_first", { search: :first })), documents: klass)
    subject.add(:second, Elasticity::Search::Facade.new(client, Elasticity::Search::Definition.new("index_second", "document_second", { search: :second })), documents: klass)

    expect(Elasticity.config.client).to receive(:msearch).with(body: [
      { index: "index_first", type: "document_first", search: { search: :first } },
      { index: "index_second", type: "document_second", search: { search: :second } },
    ]).and_return(response)

    expect(Array(subject[:first])). to eq [klass.new(_id: 1, name: "foo"), klass.new(_id: 2, name: "bar")]
    expect(Array(subject[:second])). to eq [klass.new(_id: 3, name: "baz")]
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
es-elasticity-0.3.10 spec/units/multi_search_spec.rb
es-elasticity-0.4.1 spec/units/multi_search_spec.rb
es-elasticity-0.4.0 spec/units/multi_search_spec.rb
es-elasticity-0.3.9 spec/units/multi_search_spec.rb
es-elasticity-0.3.8 spec/units/multi_search_spec.rb
es-elasticity-0.3.7 spec/units/multi_search_spec.rb
es-elasticity-0.3.6 spec/units/multi_search_spec.rb
es-elasticity-0.3.5 spec/units/multi_search_spec.rb
es-elasticity-0.3.4 spec/units/multi_search_spec.rb
es-elasticity-0.3.3 spec/units/multi_search_spec.rb
es-elasticity-0.3.2 spec/units/multi_search_spec.rb
es-elasticity-0.3.1 spec/units/multi_search_spec.rb
es-elasticity-0.3.0 spec/units/multi_search_spec.rb