mapper-annotated-text.asciidoc 9.8 KB

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