mapper-annotated-text.asciidoc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. [[mapper-annotated-text]]
  2. === Mapper Annotated Text Plugin
  3. experimental[]
  4. The mapper-annotated-text plugin provides the ability to index text that is a
  5. combination of free-text and special markup that is typically used to identify
  6. items of interest such as people or organisations (see NER or Named Entity Recognition
  7. tools).
  8. The elasticsearch markup allows one or more additional tokens to be injected, unchanged, into the token
  9. stream at the same position as the underlying text it annotates.
  10. :plugin_name: mapper-annotated-text
  11. include::install_remove.asciidoc[]
  12. [[mapper-annotated-text-usage]]
  13. ==== Using the `annotated-text` field
  14. The `annotated-text` tokenizes text content as per the more common `text` field (see
  15. "limitations" below) but also injects any marked-up annotation tokens directly into
  16. the search index:
  17. [source,js]
  18. --------------------------
  19. PUT my_index
  20. {
  21. "mappings": {
  22. "_doc": {
  23. "properties": {
  24. "my_field": {
  25. "type": "annotated_text"
  26. }
  27. }
  28. }
  29. }
  30. }
  31. --------------------------
  32. // CONSOLE
  33. Such a mapping would allow marked-up text eg wikipedia articles to be indexed as both text
  34. and structured tokens. The annotations use a markdown-like syntax using URL encoding of
  35. one or more values separated by the `&` symbol.
  36. We can use the "_analyze" api to test how an example annotation would be stored as tokens
  37. in the search index:
  38. [source,js]
  39. --------------------------
  40. GET my_index/_analyze
  41. {
  42. "field": "my_field",
  43. "text":"Investors in [Apple](Apple+Inc.) rejoiced."
  44. }
  45. --------------------------
  46. // NOTCONSOLE
  47. Response:
  48. [source,js]
  49. --------------------------------------------------
  50. {
  51. "tokens": [
  52. {
  53. "token": "investors",
  54. "start_offset": 0,
  55. "end_offset": 9,
  56. "type": "<ALPHANUM>",
  57. "position": 0
  58. },
  59. {
  60. "token": "in",
  61. "start_offset": 10,
  62. "end_offset": 12,
  63. "type": "<ALPHANUM>",
  64. "position": 1
  65. },
  66. {
  67. "token": "Apple Inc.", <1>
  68. "start_offset": 13,
  69. "end_offset": 18,
  70. "type": "annotation",
  71. "position": 2
  72. },
  73. {
  74. "token": "apple",
  75. "start_offset": 13,
  76. "end_offset": 18,
  77. "type": "<ALPHANUM>",
  78. "position": 2
  79. },
  80. {
  81. "token": "rejoiced",
  82. "start_offset": 19,
  83. "end_offset": 27,
  84. "type": "<ALPHANUM>",
  85. "position": 3
  86. }
  87. ]
  88. }
  89. --------------------------------------------------
  90. // NOTCONSOLE
  91. <1> Note the whole annotation token `Apple Inc.` is placed, unchanged as a single token in
  92. the token stream and at the same position (position 2) as the text token (`apple`) it annotates.
  93. We can now perform searches for annotations using regular `term` queries that don't tokenize
  94. the provided search values. Annotations are a more precise way of matching as can be seen
  95. in this example where a search for `Beck` will not match `Jeff Beck` :
  96. [source,js]
  97. --------------------------
  98. # Example documents
  99. PUT my_index/_doc/1
  100. {
  101. "my_field": "[Beck](Beck) announced a new tour"<2>
  102. }
  103. PUT my_index/_doc/2
  104. {
  105. "my_field": "[Jeff Beck](Jeff+Beck&Guitarist) plays a strat"<1>
  106. }
  107. # Example search
  108. GET my_index/_search
  109. {
  110. "query": {
  111. "term": {
  112. "my_field": "Beck" <3>
  113. }
  114. }
  115. }
  116. --------------------------
  117. // CONSOLE
  118. <1> As well as tokenising the plain text into single words e.g. `beck`, here we
  119. inject the single token value `Beck` at the same position as `beck` in the token stream.
  120. <2> Note annotations can inject multiple tokens at the same position - here we inject both
  121. the very specific value `Jeff Beck` and the broader term `Guitarist`. This enables
  122. broader positional queries e.g. finding mentions of a `Guitarist` near to `strat`.
  123. <3> A benefit of searching with these carefully defined annotation tokens is that a query for
  124. `Beck` will not match document 2 that contains the tokens `jeff`, `beck` and `Jeff Beck`
  125. WARNING: Any use of `=` signs in annotation values eg `[Prince](person=Prince)` will
  126. cause the document to be rejected with a parse failure. In future we hope to have a use for
  127. the equals signs so wil actively reject documents that contain this today.
  128. [[mapper-annotated-text-tips]]
  129. ==== Data modelling tips
  130. ===== Use structured and unstructured fields
  131. Annotations are normally a way of weaving structured information into unstructured text for
  132. higher-precision search.
  133. `Entity resolution` is a form of document enrichment undertaken by specialist software or people
  134. where references to entities in a document are disambiguated by attaching a canonical ID.
  135. The ID is used to resolve any number of aliases or distinguish between people with the
  136. same name. The hyperlinks connecting Wikipedia's articles are a good example of resolved
  137. entity IDs woven into text.
  138. These IDs can be embedded as annotations in an annotated_text field but it often makes
  139. sense to include them in dedicated structured fields to support discovery via aggregations:
  140. [source,js]
  141. --------------------------
  142. PUT my_index
  143. {
  144. "mappings": {
  145. "_doc": {
  146. "properties": {
  147. "my_unstructured_text_field": {
  148. "type": "annotated_text"
  149. },
  150. "my_structured_people_field": {
  151. "type": "text",
  152. "fields": {
  153. "keyword" :{
  154. "type": "keyword"
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }
  161. }
  162. --------------------------
  163. // CONSOLE
  164. Applications would then typically provide content and discover it as follows:
  165. [source,js]
  166. --------------------------
  167. # Example documents
  168. PUT my_index/_doc/1
  169. {
  170. "my_unstructured_text_field": "[Shay](%40kimchy) created elasticsearch",
  171. "my_twitter_handles": ["@kimchy"] <1>
  172. }
  173. GET my_index/_search
  174. {
  175. "query": {
  176. "query_string": {
  177. "query": "elasticsearch OR logstash OR kibana",<2>
  178. "default_field": "my_unstructured_text_field"
  179. }
  180. },
  181. "aggregations": {
  182. "top_people" :{
  183. "significant_terms" : { <3>
  184. "field" : "my_twitter_handles.keyword"
  185. }
  186. }
  187. }
  188. }
  189. --------------------------
  190. // CONSOLE
  191. <1> Note the `my_twitter_handles` contains a list of the annotation values
  192. also used in the unstructured text. (Note the annotated_text syntax requires escaping).
  193. By repeating the annotation values in a structured field this application has ensured that
  194. the tokens discovered in the structured field can be used for search and highlighting
  195. in the unstructured field.
  196. <2> In this example we search for documents that talk about components of the elastic stack
  197. <3> We use the `my_twitter_handles` field here to discover people who are significantly
  198. associated with the elastic stack.
  199. ===== Avoiding over-matching annotations
  200. By design, the regular text tokens and the annotation tokens co-exist in the same indexed
  201. field but in rare cases this can lead to some over-matching.
  202. The value of an annotation often denotes a _named entity_ (a person, place or company).
  203. The tokens for these named entities are inserted untokenized, and differ from typical text
  204. tokens because they are normally:
  205. * Mixed case e.g. `Madonna`
  206. * Multiple words e.g. `Jeff Beck`
  207. * Can have punctuation or numbers e.g. `Apple Inc.` or `@kimchy`
  208. This means, for the most part, a search for a named entity in the annotated text field will
  209. not have any false positives e.g. when selecting `Apple Inc.` from an aggregation result
  210. you can drill down to highlight uses in the text without "over matching" on any text tokens
  211. like the word `apple` in this context:
  212. the apple was very juicy
  213. However, a problem arises if your named entity happens to be a single term and lower-case e.g. the
  214. company `elastic`. In this case, a search on the annotated text field for the token `elastic`
  215. may match a text document such as this:
  216. he fired an elastic band
  217. To avoid such false matches users should consider prefixing annotation values to ensure
  218. they don't name clash with text tokens e.g.
  219. [elastic](Company_elastic) released version 7.0 of the elastic stack today
  220. [[mapper-annotated-text-highlighter]]
  221. ==== Using the `annotated` highlighter
  222. The `annotated-text` plugin includes a custom highlighter designed to mark up search hits
  223. in a way which is respectful of the original markup:
  224. [source,js]
  225. --------------------------
  226. # Example documents
  227. PUT my_index/_doc/1
  228. {
  229. "my_field": "The cat sat on the [mat](sku3578)"
  230. }
  231. GET my_index/_search
  232. {
  233. "query": {
  234. "query_string": {
  235. "query": "cats"
  236. }
  237. },
  238. "highlight": {
  239. "fields": {
  240. "my_field": {
  241. "type": "annotated", <1>
  242. "require_field_match": false
  243. }
  244. }
  245. }
  246. }
  247. --------------------------
  248. // CONSOLE
  249. <1> The `annotated` highlighter type is designed for use with annotated_text fields
  250. The annotated highlighter is based on the `unified` highlighter and supports the same
  251. settings but does not use the `pre_tags` or `post_tags` parameters. Rather than using
  252. html-like markup such as `<em>cat</em>` the annotated highlighter uses the same
  253. markdown-like syntax used for annotations and injects a key=value annotation where `_hit_term`
  254. is the key and the matched search term is the value e.g.
  255. The [cat](_hit_term=cat) sat on the [mat](sku3578)
  256. The annotated highlighter tries to be respectful of any existing markup in the original
  257. text:
  258. * If the search term matches exactly the location of an existing annotation then the
  259. `_hit_term` key is merged into the url-like syntax used in the `(...)` part of the
  260. existing annotation.
  261. * However, if the search term overlaps the span of an existing annotation it would break
  262. the markup formatting so the original annotation is removed in favour of a new annotation
  263. with just the search hit information in the results.
  264. * Any non-overlapping annotations in the original text are preserved in highlighter
  265. selections
  266. [[mapper-annotated-text-limitations]]
  267. ==== Limitations
  268. The annotated_text field type supports the same mapping settings as the `text` field type
  269. but with the following exceptions:
  270. * No support for `fielddata` or `fielddata_frequency_filter`
  271. * No support for `index_prefixes` or `index_phrases` indexing