Skip to content

Commit

Permalink
[API] [1.0] Added the "Multi Term Vectors" API
Browse files Browse the repository at this point in the history
  • Loading branch information
karmi committed Jan 24, 2014
1 parent 58b2c11 commit d4f564f
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
67 changes: 67 additions & 0 deletions elasticsearch-api/lib/elasticsearch/api/actions/mtermvectors.rb
@@ -0,0 +1,67 @@
module Elasticsearch
module API
module Actions

# Returns information and statistics about terms in the fields of multiple documents
# in a single request/response. The semantics are similar to the {#mget} API.
#
# @example Return information about multiple documents in a specific index
#
# subject.mtermvectors index: 'my-index', type: 'my-type', body: { ids: [1, 2, 3] }
#
# @option arguments [String] :index The name of the index
# @option arguments [String] :type The type of the document
# @option arguments [Hash] :body Document identifiers; can be either `docs` (containing full document information)
# or `ids` (when index and type is provided in the URL (*Required*)
# @option arguments [List] :ids A comma-separated list of documents ids (alternative to `:body`)
# @option arguments [Boolean] :term_statistics Whether total term frequency and
# document frequency should be returned.
# @option arguments [Boolean] :field_statistics Whether document count, sum of document frequencies
# and sum of total term frequencies should be returned.
# @option arguments [List] :fields A comma-separated list of fields to return
# @option arguments [Boolean] :offsets Whether term offsets should be returned
# @option arguments [Boolean] :positions Whether term positions should be returned
# @option arguments [Boolean] :payloads Whether term payloads should be returned
# @option arguments [String] :preference Specify the node or shard the operation should be performed on
# (default: random)
# @option arguments [String] :routing Specific routing value
# @option arguments [String] :parent Parent ID of documents
#
# @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/master/docs-multi-termvectors.html
#
# @see #mget
# @see #termvector
#
def mtermvectors(arguments={})
valid_params = [
:ids,
:term_statistics,
:field_statistics,
:fields,
:offsets,
:positions,
:payloads,
:preference,
:routing,
:parent ]

ids = arguments.delete(:ids)

method = 'GET'
path = Utils.__pathify Utils.__escape(arguments[:index]),
Utils.__escape(arguments[:type]),
'_mtermvectors'

params = Utils.__validate_and_extract_params arguments, valid_params

if ids
body = { :ids => ids }
else
body = arguments[:body]
end

perform_request(method, path, params, body).body
end
end
end
end
38 changes: 38 additions & 0 deletions elasticsearch-api/test/unit/mtermvectors_test.rb
@@ -0,0 +1,38 @@
require 'test_helper'

module Elasticsearch
module Test
class MtermvectorsTest < ::Test::Unit::TestCase

context "Mtermvectors" do
subject { FakeClient.new }

should "perform correct request" do
subject.expects(:perform_request).with do |method, url, params, body|
assert_equal 'GET', method
assert_equal 'my-index/my-type/_mtermvectors', url
assert_equal Hash.new, params
assert_equal [1, 2, 3], body[:ids]
true
end.returns(FakeResponse.new)

subject.mtermvectors :index => 'my-index', :type => 'my-type', :body => { :ids => [1, 2, 3] }
end

should "allow passing a list of IDs instead of the body" do
subject.expects(:perform_request).with do |method, url, params, body|
assert_equal 'GET', method
assert_equal 'my-index/my-type/_mtermvectors', url
assert_equal Hash.new, params
assert_equal [1, 2, 3], body[:ids]
true
end.returns(FakeResponse.new)

subject.mtermvectors :index => 'my-index', :type => 'my-type', :ids => [1, 2, 3]
end

end

end
end
end

0 comments on commit d4f564f

Please sign in to comment.