mapper-attachments.asciidoc 10 KB

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