mapper-attachments.asciidoc 11 KB

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