Skip to content

Commit

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

# Return information and statistics about terms in the fields of a particular document
#
# @example Get statistics for a specific document
#
# client.indices.create index: 'my_index',
# body: {
# mappings: {
# my_type: {
# properties: {
# text: {
# type: 'string',
# term_vector: 'with_positions_offsets_payloads'
# }
# }
# }
# }
# }
#
# client.index index: 'my_index', type: 'my_type', id: '1', body: { text: 'Foo Bar Fox' }
#
# client.termvector index: 'my_index', type: 'my_type', id: '1'
# # => { ..., "term_vectors" => { "text" => { "field_statistics" => { ... }, "terms" => { "bar" => ... } } }
#
# @option arguments [String] :index The name of the index (*Required*)
# @option arguments [String] :type The type of the document (*Required*)
# @option arguments [String] :id The document ID (*Required*)
# @option arguments [Hash] :body The request definition
# @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-termvectors.html
#
def termvector(arguments={})
raise ArgumentError, "Required argument 'index' missing" unless arguments[:index]
raise ArgumentError, "Required argument 'type' missing" unless arguments[:type]
raise ArgumentError, "Required argument 'id' missing" unless arguments[:id]

valid_params = [
:term_statistics,
:field_statistics,
:fields,
:offsets,
:positions,
:payloads,
:preference,
:routing,
:parent ]

method = 'GET'

path = Utils.__pathify Utils.__escape(arguments[:index]),
Utils.__escape(arguments[:type]),
arguments[:id],
'_termvector'

params = Utils.__validate_and_extract_params arguments, valid_params
body = arguments[:body]

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

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

context "Termvector" do
subject { FakeClient.new }

should "require the :index argument" do
assert_raise ArgumentError do
subject.termvector :type => 'bar', :id => '1'
end
end

should "require the :type argument" do
assert_raise ArgumentError do
subject.termvector :index => 'foo', :id => '1'
end
end

should "require the :id argument" do
assert_raise ArgumentError do
subject.termvector :index => 'foo', :type => 'bar'
end
end

should "perform correct request" do
subject.expects(:perform_request).with do |method, url, params, body|
assert_equal 'GET', method
assert_equal 'foo/bar/123/_termvector', url
assert_equal Hash.new, params
assert_equal Hash.new, body
true
end.returns(FakeResponse.new)

subject.termvector :index => 'foo', :type => 'bar', :id => '123', :body => {}
end

end

end
end
end

0 comments on commit 5e9dfac

Please sign in to comment.