Term vectors API

Retrieves information and statistics for terms in the fields of a particular document.

  1. GET /my-index-000001/_termvectors/1

Request

GET /<index>/_termvectors/<_id>

Description

You can retrieve term vectors for documents stored in the index or for artificial documents passed in the body of the request.

You can specify the fields you are interested in through the fields parameter, or by adding the fields to the request body.

  1. GET /my-index-000001/_termvectors/1?fields=message

Fields can be specified using wildcards, similar to the multi match query.

Term vectors are real-time by default, not near real-time. This can be changed by setting realtime parameter to false.

You can request three types of values: term information, term statistics and field statistics. By default, all term information and field statistics are returned for all fields but term statistics are excluded.

Term information

  • term frequency in the field (always returned)
  • term positions (positions : true)
  • start and end offsets (offsets : true)
  • term payloads (payloads : true), as base64 encoded bytes

If the requested information wasn’t stored in the index, it will be computed on the fly if possible. Additionally, term vectors could be computed for documents not even existing in the index, but instead provided by the user.

Start and end offsets assume UTF-16 encoding is being used. If you want to use these offsets in order to get the original text that produced this token, you should make sure that the string you are taking a sub-string of is also encoded using UTF-16.

Term statistics

Setting term_statistics to true (default is false) will return

  • total term frequency (how often a term occurs in all documents)

  • document frequency (the number of documents containing the current term)

By default these values are not returned since term statistics can have a serious performance impact.

Field statistics

Setting field_statistics to false (default is true) will omit :

  • document count (how many documents contain this field)
  • sum of document frequencies (the sum of document frequencies for all terms in this field)
  • sum of total term frequencies (the sum of total term frequencies of each term in this field)

Terms filtering

With the parameter filter, the terms returned could also be filtered based on their tf-idf scores. This could be useful in order find out a good characteristic vector of a document. This feature works in a similar manner to the second phase of the More Like This Query. See example 5 for usage.

The following sub-parameters are supported:

max_num_terms

Maximum number of terms that must be returned per field. Defaults to 25.

min_term_freq

Ignore words with less than this frequency in the source doc. Defaults to 1.

max_term_freq

Ignore words with more than this frequency in the source doc. Defaults to unbounded.

min_doc_freq

Ignore terms which do not occur in at least this many docs. Defaults to 1.

max_doc_freq

Ignore words which occur in more than this many docs. Defaults to unbounded.

min_word_length

The minimum word length below which words will be ignored. Defaults to 0.

max_word_length

The maximum word length above which words will be ignored. Defaults to unbounded (0).

Behaviour

The term and field statistics are not accurate. Deleted documents are not taken into account. The information is only retrieved for the shard the requested document resides in. The term and field statistics are therefore only useful as relative measures whereas the absolute numbers have no meaning in this context. By default, when requesting term vectors of artificial documents, a shard to get the statistics from is randomly selected. Use routing only to hit a particular shard.

Path parameters

<index>

(Required, string) Name of the index that contains the document.

<_id>

(Optional, string) Unique identifier of the document.

Query parameters

fields

(Optional, string) Comma-separated list or wildcard expressions of fields to include in the statistics.

Used as the default list unless a specific field list is provided in the completion_fields or fielddata_fields parameters.

field_statistics

(Optional, boolean) If true, the response includes the document count, sum of document frequencies, and sum of total term frequencies. Defaults to true.

<offsets>

(Optional, boolean) If true, the response includes term offsets. Defaults to true.

payloads

(Optional, boolean) If true, the response includes term payloads. Defaults to true.

positions

(Optional, boolean) If true, the response includes term positions. Defaults to true.

preference

(Optional, string) Specifies the node or shard the operation should be performed on. Random by default.

routing

(Optional, string) Target the specified primary shard.

realtime

(Optional, boolean) If true, the request is real-time as opposed to near-real-time. Defaults to true. See Realtime.

term_statistics

(Optional, boolean) If true, the response includes term frequency and document frequency. Defaults to false.

version

(Optional, boolean) If true, returns the document version as part of a hit.

version_type

(Optional, enum) Specific version type: internal, external, external_gte.

Examples

Returning stored term vectors

First, we create an index that stores term vectors, payloads etc. :

  1. PUT /my-index-000001
  2. { "mappings": {
  3. "properties": {
  4. "text": {
  5. "type": "text",
  6. "term_vector": "with_positions_offsets_payloads",
  7. "store" : true,
  8. "analyzer" : "fulltext_analyzer"
  9. },
  10. "fullname": {
  11. "type": "text",
  12. "term_vector": "with_positions_offsets_payloads",
  13. "analyzer" : "fulltext_analyzer"
  14. }
  15. }
  16. },
  17. "settings" : {
  18. "index" : {
  19. "number_of_shards" : 1,
  20. "number_of_replicas" : 0
  21. },
  22. "analysis": {
  23. "analyzer": {
  24. "fulltext_analyzer": {
  25. "type": "custom",
  26. "tokenizer": "whitespace",
  27. "filter": [
  28. "lowercase",
  29. "type_as_payload"
  30. ]
  31. }
  32. }
  33. }
  34. }
  35. }

Second, we add some documents:

  1. PUT /my-index-000001/_doc/1
  2. {
  3. "fullname" : "John Doe",
  4. "text" : "test test test "
  5. }
  6. PUT /my-index-000001/_doc/2?refresh=wait_for
  7. {
  8. "fullname" : "Jane Doe",
  9. "text" : "Another test ..."
  10. }

The following request returns all information and statistics for field text in document 1 (John Doe):

  1. GET /my-index-000001/_termvectors/1
  2. {
  3. "fields" : ["text"],
  4. "offsets" : true,
  5. "payloads" : true,
  6. "positions" : true,
  7. "term_statistics" : true,
  8. "field_statistics" : true
  9. }

Response:

  1. {
  2. "_index": "my-index-000001",
  3. "_type": "_doc",
  4. "_id": "1",
  5. "_version": 1,
  6. "found": true,
  7. "took": 6,
  8. "term_vectors": {
  9. "text": {
  10. "field_statistics": {
  11. "sum_doc_freq": 4,
  12. "doc_count": 2,
  13. "sum_ttf": 6
  14. },
  15. "terms": {
  16. "test": {
  17. "doc_freq": 2,
  18. "ttf": 4,
  19. "term_freq": 3,
  20. "tokens": [
  21. {
  22. "position": 0,
  23. "start_offset": 0,
  24. "end_offset": 4,
  25. "payload": "d29yZA=="
  26. },
  27. {
  28. "position": 1,
  29. "start_offset": 5,
  30. "end_offset": 9,
  31. "payload": "d29yZA=="
  32. },
  33. {
  34. "position": 2,
  35. "start_offset": 10,
  36. "end_offset": 14,
  37. "payload": "d29yZA=="
  38. }
  39. ]
  40. }
  41. }
  42. }
  43. }
  44. }

Generating term vectors on the fly

Term vectors which are not explicitly stored in the index are automatically computed on the fly. The following request returns all information and statistics for the fields in document 1, even though the terms haven’t been explicitly stored in the index. Note that for the field text, the terms are not re-generated.

  1. GET /my-index-000001/_termvectors/1
  2. {
  3. "fields" : ["text", "some_field_without_term_vectors"],
  4. "offsets" : true,
  5. "positions" : true,
  6. "term_statistics" : true,
  7. "field_statistics" : true
  8. }

Artificial documents

Term vectors can also be generated for artificial documents, that is for documents not present in the index. For example, the following request would return the same results as in example 1. The mapping used is determined by the index.

If dynamic mapping is turned on (default), the document fields not in the original mapping will be dynamically created.

  1. GET /my-index-000001/_termvectors
  2. {
  3. "doc" : {
  4. "fullname" : "John Doe",
  5. "text" : "test test test"
  6. }
  7. }
Per-field analyzer

Additionally, a different analyzer than the one at the field may be provided by using the per_field_analyzer parameter. This is useful in order to generate term vectors in any fashion, especially when using artificial documents. When providing an analyzer for a field that already stores term vectors, the term vectors will be re-generated.

  1. GET /my-index-000001/_termvectors
  2. {
  3. "doc" : {
  4. "fullname" : "John Doe",
  5. "text" : "test test test"
  6. },
  7. "fields": ["fullname"],
  8. "per_field_analyzer" : {
  9. "fullname": "keyword"
  10. }
  11. }

Response:

  1. {
  2. "_index": "my-index-000001",
  3. "_type": "_doc",
  4. "_version": 0,
  5. "found": true,
  6. "took": 6,
  7. "term_vectors": {
  8. "fullname": {
  9. "field_statistics": {
  10. "sum_doc_freq": 2,
  11. "doc_count": 4,
  12. "sum_ttf": 4
  13. },
  14. "terms": {
  15. "John Doe": {
  16. "term_freq": 1,
  17. "tokens": [
  18. {
  19. "position": 0,
  20. "start_offset": 0,
  21. "end_offset": 8
  22. }
  23. ]
  24. }
  25. }
  26. }
  27. }
  28. }

Terms filtering

Finally, the terms returned could be filtered based on their tf-idf scores. In the example below we obtain the three most “interesting” keywords from the artificial document having the given “plot” field value. Notice that the keyword “Tony” or any stop words are not part of the response, as their tf-idf must be too low.

  1. GET /imdb/_termvectors
  2. {
  3. "doc": {
  4. "plot": "When wealthy industrialist Tony Stark is forced to build an armored suit after a life-threatening incident, he ultimately decides to use its technology to fight against evil."
  5. },
  6. "term_statistics": true,
  7. "field_statistics": true,
  8. "positions": false,
  9. "offsets": false,
  10. "filter": {
  11. "max_num_terms": 3,
  12. "min_term_freq": 1,
  13. "min_doc_freq": 1
  14. }
  15. }

Response:

  1. {
  2. "_index": "imdb",
  3. "_type": "_doc",
  4. "_version": 0,
  5. "found": true,
  6. "term_vectors": {
  7. "plot": {
  8. "field_statistics": {
  9. "sum_doc_freq": 3384269,
  10. "doc_count": 176214,
  11. "sum_ttf": 3753460
  12. },
  13. "terms": {
  14. "armored": {
  15. "doc_freq": 27,
  16. "ttf": 27,
  17. "term_freq": 1,
  18. "score": 9.74725
  19. },
  20. "industrialist": {
  21. "doc_freq": 88,
  22. "ttf": 88,
  23. "term_freq": 1,
  24. "score": 8.590818
  25. },
  26. "stark": {
  27. "doc_freq": 44,
  28. "ttf": 47,
  29. "term_freq": 1,
  30. "score": 9.272792
  31. }
  32. }
  33. }
  34. }
  35. }