termvectors.asciidoc 14 KB

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