termvectors.asciidoc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. [[docs-termvectors]]
  2. === Term vectors API
  3. ++++
  4. <titleabbrev>Term vectors</titleabbrev>
  5. ++++
  6. Retrieves information and statistics for terms in the fields of a particular document.
  7. [source,console]
  8. --------------------------------------------------
  9. GET /my-index-000001/_termvectors/1
  10. --------------------------------------------------
  11. // TEST[setup:my_index]
  12. [[docs-termvectors-api-request]]
  13. ==== {api-request-title}
  14. `GET /<index>/_termvectors/<_id>`
  15. [[docs-termvectors-api-prereqs]]
  16. ==== {api-prereq-title}
  17. * If the {es} {security-features} are enabled, you must have the `read`
  18. <<privileges-list-indices,index privilege>> for the target index or index alias.
  19. [[docs-termvectors-api-desc]]
  20. ==== {api-description-title}
  21. You can retrieve term vectors for documents stored in the index or
  22. for _artificial_ documents passed in the body of the request.
  23. You can specify the fields you are interested in through the `fields` parameter,
  24. or by adding the fields to the request body.
  25. [source,console]
  26. --------------------------------------------------
  27. GET /my-index-000001/_termvectors/1?fields=message
  28. --------------------------------------------------
  29. // TEST[setup:my_index]
  30. Fields can be specified using wildcards, similar to the <<query-dsl-multi-match-query,multi match query>>.
  31. Term vectors are <<realtime,real-time>> by default, not near real-time.
  32. This can be changed by setting `realtime` parameter to `false`.
  33. You can request three types of values: _term information_, _term statistics_
  34. and _field statistics_. By default, all term information and field
  35. statistics are returned for all fields but term statistics are excluded.
  36. [[docs-termvectors-api-term-info]]
  37. ===== Term information
  38. * term frequency in the field (always returned)
  39. * term positions (`positions` : true)
  40. * start and end offsets (`offsets` : true)
  41. * term payloads (`payloads` : true), as base64 encoded bytes
  42. If the requested information wasn't stored in the index, it will be
  43. computed on the fly if possible. Additionally, term vectors could be computed
  44. for documents not even existing in the index, but instead provided by the user.
  45. [WARNING]
  46. ======
  47. Start and end offsets assume UTF-16 encoding is being used. If you want to use
  48. these offsets in order to get the original text that produced this token, you
  49. should make sure that the string you are taking a sub-string of is also encoded
  50. using UTF-16.
  51. ======
  52. [[docs-termvectors-api-term-stats]]
  53. ===== Term statistics
  54. Setting `term_statistics` to `true` (default is `false`) will
  55. return
  56. * total term frequency (how often a term occurs in all documents) +
  57. * document frequency (the number of documents containing the current
  58. term)
  59. By default these values are not returned since term statistics can
  60. have a serious performance impact.
  61. [[docs-termvectors-api-field-stats]]
  62. ===== Field statistics
  63. Setting `field_statistics` to `false` (default is `true`) will
  64. omit :
  65. * document count (how many documents contain this field)
  66. * sum of document frequencies (the sum of document frequencies for all
  67. terms in this field)
  68. * sum of total term frequencies (the sum of total term frequencies of
  69. each term in this field)
  70. [[docs-termvectors-api-terms-filtering]]
  71. ===== Terms filtering
  72. With the parameter `filter`, the terms returned could also be filtered based
  73. on their tf-idf scores. This could be useful in order find out a good
  74. characteristic vector of a document. This feature works in a similar manner to
  75. the <<mlt-query-term-selection,second phase>> of the
  76. <<query-dsl-mlt-query,More Like This Query>>. See <<docs-termvectors-terms-filtering,example 5>>
  77. for usage.
  78. The following sub-parameters are supported:
  79. [horizontal]
  80. `max_num_terms`::
  81. Maximum number of terms that must be returned per field. Defaults to `25`.
  82. `min_term_freq`::
  83. Ignore words with less than this frequency in the source doc. Defaults to `1`.
  84. `max_term_freq`::
  85. Ignore words with more than this frequency in the source doc. Defaults to unbounded.
  86. `min_doc_freq`::
  87. Ignore terms which do not occur in at least this many docs. Defaults to `1`.
  88. `max_doc_freq`::
  89. Ignore words which occur in more than this many docs. Defaults to unbounded.
  90. `min_word_length`::
  91. The minimum word length below which words will be ignored. Defaults to `0`.
  92. `max_word_length`::
  93. The maximum word length above which words will be ignored. Defaults to unbounded (`0`).
  94. [[docs-termvectors-api-behavior]]
  95. ==== Behaviour
  96. The term and field statistics are not accurate. Deleted documents
  97. are not taken into account. The information is only retrieved for the
  98. shard the requested document resides in.
  99. The term and field statistics are therefore only useful as relative measures
  100. whereas the absolute numbers have no meaning in this context. By default,
  101. when requesting term vectors of artificial documents, a shard to get the statistics
  102. from is randomly selected. Use `routing` only to hit a particular shard.
  103. [[docs-termvectors-api-path-params]]
  104. ==== {api-path-parms-title}
  105. `<index>`::
  106. (Required, string) Name of the index that contains the document.
  107. `<_id>`::
  108. (Optional, string) Unique identifier of the document.
  109. [[docs-termvectors-api-query-params]]
  110. ==== {api-query-parms-title}
  111. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=fields]
  112. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=field_statistics]
  113. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=offsets]
  114. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=payloads]
  115. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=positions]
  116. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=preference]
  117. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=routing]
  118. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=realtime]
  119. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=term_statistics]
  120. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=version]
  121. include::{es-repo-dir}/rest-api/common-parms.asciidoc[tag=version_type]
  122. [[docs-termvectors-api-example]]
  123. ==== {api-examples-title}
  124. [[docs-termvectors-api-stored-termvectors]]
  125. ===== Returning stored term vectors
  126. First, we create an index that stores term vectors, payloads etc. :
  127. [source,console]
  128. --------------------------------------------------
  129. PUT /my-index-000001
  130. { "mappings": {
  131. "properties": {
  132. "text": {
  133. "type": "text",
  134. "term_vector": "with_positions_offsets_payloads",
  135. "store" : true,
  136. "analyzer" : "fulltext_analyzer"
  137. },
  138. "fullname": {
  139. "type": "text",
  140. "term_vector": "with_positions_offsets_payloads",
  141. "analyzer" : "fulltext_analyzer"
  142. }
  143. }
  144. },
  145. "settings" : {
  146. "index" : {
  147. "number_of_shards" : 1,
  148. "number_of_replicas" : 0
  149. },
  150. "analysis": {
  151. "analyzer": {
  152. "fulltext_analyzer": {
  153. "type": "custom",
  154. "tokenizer": "whitespace",
  155. "filter": [
  156. "lowercase",
  157. "type_as_payload"
  158. ]
  159. }
  160. }
  161. }
  162. }
  163. }
  164. --------------------------------------------------
  165. Second, we add some documents:
  166. [source,console]
  167. --------------------------------------------------
  168. PUT /my-index-000001/_doc/1
  169. {
  170. "fullname" : "John Doe",
  171. "text" : "test test test "
  172. }
  173. PUT /my-index-000001/_doc/2?refresh=wait_for
  174. {
  175. "fullname" : "Jane Doe",
  176. "text" : "Another test ..."
  177. }
  178. --------------------------------------------------
  179. // TEST[continued]
  180. The following request returns all information and statistics for field
  181. `text` in document `1` (John Doe):
  182. [source,console]
  183. --------------------------------------------------
  184. GET /my-index-000001/_termvectors/1
  185. {
  186. "fields" : ["text"],
  187. "offsets" : true,
  188. "payloads" : true,
  189. "positions" : true,
  190. "term_statistics" : true,
  191. "field_statistics" : true
  192. }
  193. --------------------------------------------------
  194. // TEST[continued]
  195. Response:
  196. [source,console-result]
  197. --------------------------------------------------
  198. {
  199. "_index": "my-index-000001",
  200. "_id": "1",
  201. "_version": 1,
  202. "found": true,
  203. "took": 6,
  204. "term_vectors": {
  205. "text": {
  206. "field_statistics": {
  207. "sum_doc_freq": 4,
  208. "doc_count": 2,
  209. "sum_ttf": 6
  210. },
  211. "terms": {
  212. "test": {
  213. "doc_freq": 2,
  214. "ttf": 4,
  215. "term_freq": 3,
  216. "tokens": [
  217. {
  218. "position": 0,
  219. "start_offset": 0,
  220. "end_offset": 4,
  221. "payload": "d29yZA=="
  222. },
  223. {
  224. "position": 1,
  225. "start_offset": 5,
  226. "end_offset": 9,
  227. "payload": "d29yZA=="
  228. },
  229. {
  230. "position": 2,
  231. "start_offset": 10,
  232. "end_offset": 14,
  233. "payload": "d29yZA=="
  234. }
  235. ]
  236. }
  237. }
  238. }
  239. }
  240. }
  241. --------------------------------------------------
  242. // TEST[continued]
  243. // TESTRESPONSE[s/"took": 6/"took": "$body.took"/]
  244. [[docs-termvectors-api-generate-termvectors]]
  245. ===== Generating term vectors on the fly
  246. Term vectors which are not explicitly stored in the index are automatically
  247. computed on the fly. The following request returns all information and statistics for the
  248. fields in document `1`, even though the terms haven't been explicitly stored in the index.
  249. Note that for the field `text`, the terms are not re-generated.
  250. [source,console]
  251. --------------------------------------------------
  252. GET /my-index-000001/_termvectors/1
  253. {
  254. "fields" : ["text", "some_field_without_term_vectors"],
  255. "offsets" : true,
  256. "positions" : true,
  257. "term_statistics" : true,
  258. "field_statistics" : true
  259. }
  260. --------------------------------------------------
  261. // TEST[continued]
  262. [[docs-termvectors-artificial-doc]]
  263. ===== Artificial documents
  264. Term vectors can also be generated for artificial documents,
  265. that is for documents not present in the index. For example, the following request would
  266. return the same results as in example 1. The mapping used is determined by the `index`.
  267. *If dynamic mapping is turned on (default), the document fields not in the original
  268. mapping will be dynamically created.*
  269. [source,console]
  270. --------------------------------------------------
  271. GET /my-index-000001/_termvectors
  272. {
  273. "doc" : {
  274. "fullname" : "John Doe",
  275. "text" : "test test test"
  276. }
  277. }
  278. --------------------------------------------------
  279. // TEST[continued]
  280. [[docs-termvectors-per-field-analyzer]]
  281. ====== Per-field analyzer
  282. Additionally, a different analyzer than the one at the field may be provided
  283. by using the `per_field_analyzer` parameter. This is useful in order to
  284. generate term vectors in any fashion, especially when using artificial
  285. documents. When providing an analyzer for a field that already stores term
  286. vectors, the term vectors will be re-generated.
  287. [source,console]
  288. --------------------------------------------------
  289. GET /my-index-000001/_termvectors
  290. {
  291. "doc" : {
  292. "fullname" : "John Doe",
  293. "text" : "test test test"
  294. },
  295. "fields": ["fullname"],
  296. "per_field_analyzer" : {
  297. "fullname": "keyword"
  298. }
  299. }
  300. --------------------------------------------------
  301. // TEST[continued]
  302. Response:
  303. [source,console-result]
  304. --------------------------------------------------
  305. {
  306. "_index": "my-index-000001",
  307. "_version": 0,
  308. "found": true,
  309. "took": 6,
  310. "term_vectors": {
  311. "fullname": {
  312. "field_statistics": {
  313. "sum_doc_freq": 2,
  314. "doc_count": 4,
  315. "sum_ttf": 4
  316. },
  317. "terms": {
  318. "John Doe": {
  319. "term_freq": 1,
  320. "tokens": [
  321. {
  322. "position": 0,
  323. "start_offset": 0,
  324. "end_offset": 8
  325. }
  326. ]
  327. }
  328. }
  329. }
  330. }
  331. }
  332. --------------------------------------------------
  333. // TEST[continued]
  334. // TESTRESPONSE[s/"took": 6/"took": "$body.took"/]
  335. // TESTRESPONSE[s/"sum_doc_freq": 2/"sum_doc_freq": "$body.term_vectors.fullname.field_statistics.sum_doc_freq"/]
  336. // TESTRESPONSE[s/"doc_count": 4/"doc_count": "$body.term_vectors.fullname.field_statistics.doc_count"/]
  337. // TESTRESPONSE[s/"sum_ttf": 4/"sum_ttf": "$body.term_vectors.fullname.field_statistics.sum_ttf"/]
  338. [[docs-termvectors-terms-filtering]]
  339. ===== Terms filtering
  340. Finally, the terms returned could be filtered based on their tf-idf scores. In
  341. the example below we obtain the three most "interesting" keywords from the
  342. artificial document having the given "plot" field value. Notice
  343. that the keyword "Tony" or any stop words are not part of the response, as
  344. their tf-idf must be too low.
  345. [source,console]
  346. --------------------------------------------------
  347. GET /imdb/_termvectors
  348. {
  349. "doc": {
  350. "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."
  351. },
  352. "term_statistics": true,
  353. "field_statistics": true,
  354. "positions": false,
  355. "offsets": false,
  356. "filter": {
  357. "max_num_terms": 3,
  358. "min_term_freq": 1,
  359. "min_doc_freq": 1
  360. }
  361. }
  362. --------------------------------------------------
  363. // TEST[skip:no imdb test index]
  364. Response:
  365. [source,console-result]
  366. --------------------------------------------------
  367. {
  368. "_index": "imdb",
  369. "_version": 0,
  370. "found": true,
  371. "term_vectors": {
  372. "plot": {
  373. "field_statistics": {
  374. "sum_doc_freq": 3384269,
  375. "doc_count": 176214,
  376. "sum_ttf": 3753460
  377. },
  378. "terms": {
  379. "armored": {
  380. "doc_freq": 27,
  381. "ttf": 27,
  382. "term_freq": 1,
  383. "score": 9.74725
  384. },
  385. "industrialist": {
  386. "doc_freq": 88,
  387. "ttf": 88,
  388. "term_freq": 1,
  389. "score": 8.590818
  390. },
  391. "stark": {
  392. "doc_freq": 44,
  393. "ttf": 47,
  394. "term_freq": 1,
  395. "score": 9.272792
  396. }
  397. }
  398. }
  399. }
  400. }
  401. --------------------------------------------------