mapper-attachments.asciidoc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. // NOTCONSOLE
  17. The plugin must be installed on every node in the cluster, and each node must
  18. be restarted after installation.
  19. [[mapper-attachments-remove]]
  20. [float]
  21. ==== Removal
  22. The plugin can be removed with the following command:
  23. [source,sh]
  24. ----------------------------------------------------------------
  25. sudo bin/elasticsearch-plugin remove mapper-attachments
  26. ----------------------------------------------------------------
  27. // NOTCONSOLE
  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.16273327,
  237. "hits": [
  238. {
  239. "_index": "test",
  240. "_type": "person",
  241. "_id": "1",
  242. "_score": 0.16273327,
  243. "fields": {
  244. "file.content_type": [
  245. "text/plain; charset=ISO-8859-1"
  246. ]
  247. }
  248. }
  249. ]
  250. }
  251. }
  252. --------------------------
  253. [[mapper-attachments-indexed-characters]]
  254. ==== Indexed Characters
  255. By default, `100000` characters are extracted when indexing the content. This default value can be changed by setting
  256. the `index.mapping.attachment.indexed_chars` setting. It can also be provided on a per document indexed using the
  257. `_indexed_chars` parameter. `-1` can be set to extract all text, but note that all the text needs to be allowed to be
  258. represented in memory:
  259. [source,js]
  260. --------------------------
  261. PUT /test/person/1
  262. {
  263. "my_attachment" : {
  264. "_indexed_chars" : -1,
  265. "_content" : "... base64 encoded attachment ..."
  266. }
  267. }
  268. --------------------------
  269. // CONSOLE
  270. [[mapper-attachments-error-handling]]
  271. ==== Metadata parsing error handling
  272. While extracting metadata content, errors could happen for example when parsing dates.
  273. Parsing errors are ignored so your document is indexed.
  274. You can disable this feature by setting the `index.mapping.attachment.ignore_errors` setting to `false`.
  275. [[mapper-attachments-language-detection]]
  276. ==== Language Detection
  277. By default, language detection is disabled (`false`) as it could come with a cost.
  278. This default value can be changed by setting the `index.mapping.attachment.detect_language` setting.
  279. It can also be provided on a per document indexed using the `_detect_language` parameter.
  280. Note that you can force language using `_language` field when sending your actual document:
  281. [source,js]
  282. --------------------------
  283. {
  284. "my_attachment" : {
  285. "_language" : "en",
  286. "_content" : "... base64 encoded attachment ..."
  287. }
  288. }
  289. --------------------------
  290. [[mapper-attachments-highlighting]]
  291. ==== Highlighting attachments
  292. If you want to highlight your attachment content, you will need to set `"store": true` and
  293. `"term_vector":"with_positions_offsets"` for your attachment field. Here is a full script which does it:
  294. [source,js]
  295. --------------------------
  296. PUT /test
  297. PUT /test/person/_mapping
  298. {
  299. "person": {
  300. "properties": {
  301. "file": {
  302. "type": "attachment",
  303. "fields": {
  304. "content": {
  305. "type": "text",
  306. "term_vector":"with_positions_offsets",
  307. "store": true
  308. }
  309. }
  310. }
  311. }
  312. }
  313. }
  314. PUT /test/person/1?refresh=true
  315. {
  316. "file": "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg=="
  317. }
  318. GET /test/person/_search
  319. {
  320. "stored_fields": [],
  321. "query": {
  322. "match": {
  323. "file.content": "king queen"
  324. }
  325. },
  326. "highlight": {
  327. "fields": {
  328. "file.content": {
  329. }
  330. }
  331. }
  332. }
  333. --------------------------
  334. // CONSOLE
  335. It gives back:
  336. [source,js]
  337. --------------------------
  338. {
  339. "took": 9,
  340. "timed_out": false,
  341. "_shards": {
  342. "total": 1,
  343. "successful": 1,
  344. "failed": 0
  345. },
  346. "hits": {
  347. "total": 1,
  348. "max_score": 0.13561106,
  349. "hits": [
  350. {
  351. "_index": "test",
  352. "_type": "person",
  353. "_id": "1",
  354. "_score": 0.13561106,
  355. "highlight": {
  356. "file.content": [
  357. "\"God Save the <em>Queen</em>\" (alternatively \"God Save the <em>King</em>\"\n"
  358. ]
  359. }
  360. }
  361. ]
  362. }
  363. }
  364. --------------------------