mapper-attachments.asciidoc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. [[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. // CONSOLE
  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. // CONSOLE
  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. // CONSOLE
  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. // CONSOLE
  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. // CONSOLE
  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. // CONSOLE
  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 (text, 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. // CONSOLE
  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 `text` or `date`) since it is already known.
  142. ==== Querying or accessing metadata
  143. If you need to query on metadata fields, use the attachment field name dot the metadata field. For example:
  144. [source,js]
  145. --------------------------
  146. DELETE /test
  147. PUT /test
  148. PUT /test/person/_mapping
  149. {
  150. "person": {
  151. "properties": {
  152. "file": {
  153. "type": "attachment",
  154. "fields": {
  155. "content_type": {
  156. "type": "text",
  157. "store": true
  158. }
  159. }
  160. }
  161. }
  162. }
  163. }
  164. PUT /test/person/1?refresh=true
  165. {
  166. "file": "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg=="
  167. }
  168. GET /test/person/_search
  169. {
  170. "fields": [ "file.content_type" ],
  171. "query": {
  172. "match": {
  173. "file.content_type": "text plain"
  174. }
  175. }
  176. }
  177. --------------------------
  178. // CONSOLE
  179. Will give you:
  180. [source,js]
  181. --------------------------
  182. {
  183. "took": 2,
  184. "timed_out": false,
  185. "_shards": {
  186. "total": 5,
  187. "successful": 5,
  188. "failed": 0
  189. },
  190. "hits": {
  191. "total": 1,
  192. "max_score": 0.16273327,
  193. "hits": [
  194. {
  195. "_index": "test",
  196. "_type": "person",
  197. "_id": "1",
  198. "_score": 0.16273327,
  199. "fields": {
  200. "file.content_type": [
  201. "text/plain; charset=ISO-8859-1"
  202. ]
  203. }
  204. }
  205. ]
  206. }
  207. }
  208. --------------------------
  209. [[mapper-attachments-indexed-characters]]
  210. ==== Indexed Characters
  211. By default, `100000` characters are extracted when indexing the content. This default value can be changed by setting
  212. the `index.mapping.attachment.indexed_chars` setting. It can also be provided on a per document indexed using the
  213. `_indexed_chars` parameter. `-1` can be set to extract all text, but note that all the text needs to be allowed to be
  214. represented in memory:
  215. [source,js]
  216. --------------------------
  217. PUT /test/person/1
  218. {
  219. "my_attachment" : {
  220. "_indexed_chars" : -1,
  221. "_content" : "... base64 encoded attachment ..."
  222. }
  223. }
  224. --------------------------
  225. // CONSOLE
  226. [[mapper-attachments-error-handling]]
  227. ==== Metadata parsing error handling
  228. While extracting metadata content, errors could happen for example when parsing dates.
  229. Parsing errors are ignored so your document is indexed.
  230. You can disable this feature by setting the `index.mapping.attachment.ignore_errors` setting to `false`.
  231. [[mapper-attachments-language-detection]]
  232. ==== Language Detection
  233. By default, language detection is disabled (`false`) as it could come with a cost.
  234. This default value can be changed by setting the `index.mapping.attachment.detect_language` setting.
  235. It can also be provided on a per document indexed using the `_detect_language` parameter.
  236. Note that you can force language using `_language` field when sending your actual document:
  237. [source,js]
  238. --------------------------
  239. {
  240. "my_attachment" : {
  241. "_language" : "en",
  242. "_content" : "... base64 encoded attachment ..."
  243. }
  244. }
  245. --------------------------
  246. [[mapper-attachments-highlighting]]
  247. ==== Highlighting attachments
  248. If you want to highlight your attachment content, you will need to set `"store": true` and
  249. `"term_vector":"with_positions_offsets"` for your attachment field. Here is a full script which does it:
  250. [source,js]
  251. --------------------------
  252. DELETE /test
  253. PUT /test
  254. PUT /test/person/_mapping
  255. {
  256. "person": {
  257. "properties": {
  258. "file": {
  259. "type": "attachment",
  260. "fields": {
  261. "content": {
  262. "type": "text",
  263. "term_vector":"with_positions_offsets",
  264. "store": true
  265. }
  266. }
  267. }
  268. }
  269. }
  270. }
  271. PUT /test/person/1?refresh=true
  272. {
  273. "file": "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg=="
  274. }
  275. GET /test/person/_search
  276. {
  277. "fields": [],
  278. "query": {
  279. "match": {
  280. "file.content": "king queen"
  281. }
  282. },
  283. "highlight": {
  284. "fields": {
  285. "file.content": {
  286. }
  287. }
  288. }
  289. }
  290. --------------------------
  291. // CONSOLE
  292. It gives back:
  293. [source,js]
  294. --------------------------
  295. {
  296. "took": 9,
  297. "timed_out": false,
  298. "_shards": {
  299. "total": 1,
  300. "successful": 1,
  301. "failed": 0
  302. },
  303. "hits": {
  304. "total": 1,
  305. "max_score": 0.13561106,
  306. "hits": [
  307. {
  308. "_index": "test",
  309. "_type": "person",
  310. "_id": "1",
  311. "_score": 0.13561106,
  312. "highlight": {
  313. "file.content": [
  314. "\"God Save the <em>Queen</em>\" (alternatively \"God Save the <em>King</em>\"\n"
  315. ]
  316. }
  317. }
  318. ]
  319. }
  320. }
  321. --------------------------