mapper-attachments.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. [[mapper-attachments]]
  2. === Mapper Attachments Plugin
  3. deprecated[5.0.0,The `mapper-attachments` plugin has been replaced by the <<ingest-attachment, `ingest-attachment`>> plugin]
  4. The mapper attachments plugin lets Elasticsearch index file attachments in common formats (such as PPT, XLS, PDF)
  5. using the Apache text extraction library http://lucene.apache.org/tika/[Tika].
  6. In practice, the plugin adds the `attachment` type when mapping properties so that documents can be populated with
  7. file attachment contents (encoded as `base64`).
  8. [[mapper-attachments-install]]
  9. [float]
  10. ==== Installation
  11. This plugin can be installed using the plugin manager:
  12. [source,sh]
  13. ----------------------------------------------------------------
  14. sudo bin/elasticsearch-plugin install mapper-attachments
  15. ----------------------------------------------------------------
  16. The plugin must be installed on every node in the cluster, and each node must
  17. be restarted after installation.
  18. This plugin can be downloaded for offline install from
  19. {plugin_url}/mapper-attachments/{version}/mapper-attachments-{version}.zip[elastic download service].
  20. [[mapper-attachments-remove]]
  21. [float]
  22. ==== Removal
  23. The plugin can be removed with the following command:
  24. [source,sh]
  25. ----------------------------------------------------------------
  26. sudo bin/elasticsearch-plugin remove mapper-attachments
  27. ----------------------------------------------------------------
  28. The node must be stopped before removing the plugin.
  29. [[mapper-attachments-helloworld]]
  30. ==== Hello, world
  31. Create a property mapping using the new type `attachment`:
  32. [source,js]
  33. --------------------------
  34. PUT /trying-out-mapper-attachments
  35. {
  36. "mappings": {
  37. "person": {
  38. "properties": {
  39. "cv": { "type": "attachment" }
  40. }}}}
  41. --------------------------
  42. // CONSOLE
  43. Index a new document populated with a `base64`-encoded attachment:
  44. [source,js]
  45. --------------------------
  46. POST /trying-out-mapper-attachments/person/1?refresh
  47. {
  48. "cv": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
  49. }
  50. --------------------------
  51. // CONSOLE
  52. // TEST[continued]
  53. Search for the document using words in the attachment:
  54. [source,js]
  55. --------------------------
  56. POST /trying-out-mapper-attachments/person/_search
  57. {
  58. "query": {
  59. "query_string": {
  60. "query": "ipsum"
  61. }}}
  62. --------------------------
  63. // CONSOLE
  64. // TEST[continued]
  65. If you get a hit for your indexed document, the plugin should be installed and working. It'll look like:
  66. [source,js]
  67. --------------------------
  68. {
  69. "timed_out": false,
  70. "took": 53,
  71. "hits": {
  72. "total": 1,
  73. "max_score": 0.28582606,
  74. "hits": [
  75. {
  76. "_score": 0.28582606,
  77. "_index": "trying-out-mapper-attachments",
  78. "_type": "person",
  79. "_id": "1",
  80. "_source": {
  81. "cv": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
  82. }
  83. }
  84. ]
  85. },
  86. "_shards": ...
  87. }
  88. --------------------------
  89. // TESTRESPONSE[s/"took": 53/"took": "$body.took"/]
  90. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards"/]
  91. [[mapper-attachments-usage]]
  92. ==== Usage
  93. Using the attachment type is simple, in your mapping JSON, simply set a certain JSON element as attachment, for example:
  94. [source,js]
  95. --------------------------
  96. PUT /test
  97. {
  98. "mappings": {
  99. "person" : {
  100. "properties" : {
  101. "my_attachment" : { "type" : "attachment" }
  102. }
  103. }
  104. }
  105. }
  106. --------------------------
  107. // CONSOLE
  108. In this case, the JSON to index can be:
  109. [source,js]
  110. --------------------------
  111. PUT /test/person/1
  112. {
  113. "my_attachment" : "... base64 encoded attachment ..."
  114. }
  115. --------------------------
  116. // CONSOLE
  117. Or it is possible to use more elaborated JSON if content type, resource name or language need to be set explicitly:
  118. [source,js]
  119. --------------------------
  120. PUT /test/person/1
  121. {
  122. "my_attachment" : {
  123. "_content_type" : "application/pdf",
  124. "_name" : "resource/name/of/my.pdf",
  125. "_language" : "en",
  126. "_content" : "... base64 encoded attachment ..."
  127. }
  128. }
  129. --------------------------
  130. // CONSOLE
  131. The `attachment` type not only indexes the content of the doc in `content` sub field, but also automatically adds meta
  132. data on the attachment as well (when available).
  133. The metadata supported are:
  134. * `date`
  135. * `title`
  136. * `name` only available if you set `_name` see above
  137. * `author`
  138. * `keywords`
  139. * `content_type`
  140. * `content_length` is the original content_length before text extraction (aka file size)
  141. * `language`
  142. They can be queried using the "dot notation", for example: `my_attachment.author`.
  143. Both the meta data and the actual content are simple core type mappers (text, date, …), thus, they can be controlled
  144. in the mappings. For example:
  145. [source,js]
  146. --------------------------
  147. PUT /test
  148. {
  149. "settings": {
  150. "index": {
  151. "analysis": {
  152. "analyzer": {
  153. "my_analyzer": {
  154. "type": "custom",
  155. "tokenizer": "standard",
  156. "filter": ["standard"]
  157. }
  158. }
  159. }
  160. }
  161. },
  162. "mappings": {
  163. "person" : {
  164. "properties" : {
  165. "file" : {
  166. "type" : "attachment",
  167. "fields" : {
  168. "content" : {"index" : true},
  169. "title" : {"store" : true},
  170. "date" : {"store" : true},
  171. "author" : {"analyzer" : "my_analyzer"},
  172. "keywords" : {"store" : true},
  173. "content_type" : {"store" : true},
  174. "content_length" : {"store" : true},
  175. "language" : {"store" : true}
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. --------------------------
  183. // CONSOLE
  184. In the above example, the actual content indexed is mapped under `fields` name `content`, and we decide not to index it, so
  185. it will only be available in the `_all` field. The other fields map to their respective metadata names, but there is no
  186. need to specify the `type` (like `text` or `date`) since it is already known.
  187. ==== Querying or accessing metadata
  188. If you need to query on metadata fields, use the attachment field name dot the metadata field. For example:
  189. [source,js]
  190. --------------------------
  191. PUT /test
  192. PUT /test/person/_mapping
  193. {
  194. "person": {
  195. "properties": {
  196. "file": {
  197. "type": "attachment",
  198. "fields": {
  199. "content_type": {
  200. "type": "text",
  201. "store": true
  202. }
  203. }
  204. }
  205. }
  206. }
  207. }
  208. PUT /test/person/1?refresh=true
  209. {
  210. "file": "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg=="
  211. }
  212. GET /test/person/_search
  213. {
  214. "stored_fields": [ "file.content_type" ],
  215. "query": {
  216. "match": {
  217. "file.content_type": "text plain"
  218. }
  219. }
  220. }
  221. --------------------------
  222. // CONSOLE
  223. Will give you:
  224. [source,js]
  225. --------------------------
  226. {
  227. "took": 2,
  228. "timed_out": false,
  229. "_shards": {
  230. "total": 5,
  231. "successful": 5,
  232. "failed": 0
  233. },
  234. "hits": {
  235. "total": 1,
  236. "max_score": 0.53484553,
  237. "hits": [
  238. {
  239. "_index": "test",
  240. "_type": "person",
  241. "_id": "1",
  242. "_score": 0.53484553,
  243. "fields": {
  244. "file.content_type": [
  245. "text/plain; charset=ISO-8859-1"
  246. ]
  247. }
  248. }
  249. ]
  250. }
  251. }
  252. --------------------------
  253. // TESTRESPONSE[s/"took": 2,/"took": $body.took,/]
  254. [[mapper-attachments-indexed-characters]]
  255. ==== Indexed Characters
  256. By default, `100000` characters are extracted when indexing the content. This default value can be changed by setting
  257. the `index.mapping.attachment.indexed_chars` setting. It can also be provided on a per document indexed using the
  258. `_indexed_chars` parameter. `-1` can be set to extract all text, but note that all the text needs to be allowed to be
  259. represented in memory:
  260. [source,js]
  261. --------------------------
  262. PUT /test/person/1
  263. {
  264. "my_attachment" : {
  265. "_indexed_chars" : -1,
  266. "_content" : "... base64 encoded attachment ..."
  267. }
  268. }
  269. --------------------------
  270. // CONSOLE
  271. [[mapper-attachments-error-handling]]
  272. ==== Metadata parsing error handling
  273. While extracting metadata content, errors could happen for example when parsing dates.
  274. Parsing errors are ignored so your document is indexed.
  275. You can disable this feature by setting the `index.mapping.attachment.ignore_errors` setting to `false`.
  276. [[mapper-attachments-language-detection]]
  277. ==== Language Detection
  278. By default, language detection is disabled (`false`) as it could come with a cost.
  279. This default value can be changed by setting the `index.mapping.attachment.detect_language` setting.
  280. It can also be provided on a per document indexed using the `_detect_language` parameter.
  281. Note that you can force language using `_language` field when sending your actual document:
  282. [source,js]
  283. --------------------------
  284. PUT /test/person/1
  285. {
  286. "my_attachment" : {
  287. "_language" : "en",
  288. "_content" : "... base64 encoded attachment ..."
  289. }
  290. }
  291. --------------------------
  292. // CONSOLE
  293. [[mapper-attachments-highlighting]]
  294. ==== Highlighting attachments
  295. If you want to highlight your attachment content, you will need to set `"store": true` and
  296. `"term_vector":"with_positions_offsets"` for your attachment field. Here is a full script which does it:
  297. [source,js]
  298. --------------------------
  299. PUT /test
  300. PUT /test/person/_mapping
  301. {
  302. "person": {
  303. "properties": {
  304. "file": {
  305. "type": "attachment",
  306. "fields": {
  307. "content": {
  308. "type": "text",
  309. "term_vector":"with_positions_offsets",
  310. "store": true
  311. }
  312. }
  313. }
  314. }
  315. }
  316. }
  317. PUT /test/person/1?refresh=true
  318. {
  319. "file": "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg=="
  320. }
  321. GET /test/person/_search
  322. {
  323. "stored_fields": [],
  324. "query": {
  325. "match": {
  326. "file.content": "king queen"
  327. }
  328. },
  329. "highlight": {
  330. "fields": {
  331. "file.content": {
  332. }
  333. }
  334. }
  335. }
  336. --------------------------
  337. // CONSOLE
  338. It gives back:
  339. [source,js]
  340. --------------------------
  341. {
  342. "took": 9,
  343. "timed_out": false,
  344. "_shards": {
  345. "total": 5,
  346. "successful": 5,
  347. "failed": 0
  348. },
  349. "hits": {
  350. "total": 1,
  351. "max_score": 0.5446649,
  352. "hits": [
  353. {
  354. "_index": "test",
  355. "_type": "person",
  356. "_id": "1",
  357. "_score": 0.5446649,
  358. "highlight": {
  359. "file.content": [
  360. "\"God Save the <em>Queen</em>\" (alternatively \"God Save the <em>King</em>\"\n"
  361. ]
  362. }
  363. }
  364. ]
  365. }
  366. }
  367. --------------------------
  368. // TESTRESPONSE[s/"took": 9,/"took": $body.took,/]