stemming.asciidoc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. [[mixing-exact-search-with-stemming]]
  2. === Mixing exact search with stemming
  3. When building a search application, stemming is often a must as it is desirable
  4. for a query on `skiing` to match documents that contain `ski` or `skis`. But
  5. what if a user wants to search for `skiing` specifically? The typical way to do
  6. this would be to use a <<multi-fields,multi-field>> in order to have the same
  7. content indexed in two different ways:
  8. [source,js]
  9. --------------------------------------------------
  10. PUT index
  11. {
  12. "settings": {
  13. "analysis": {
  14. "analyzer": {
  15. "english_exact": {
  16. "tokenizer": "standard",
  17. "filter": [
  18. "lowercase"
  19. ]
  20. }
  21. }
  22. }
  23. },
  24. "mappings": {
  25. "_doc": {
  26. "properties": {
  27. "body": {
  28. "type": "text",
  29. "analyzer": "english",
  30. "fields": {
  31. "exact": {
  32. "type": "text",
  33. "analyzer": "english_exact"
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }
  40. }
  41. PUT index/_doc/1
  42. {
  43. "body": "Ski resort"
  44. }
  45. PUT index/_doc/2
  46. {
  47. "body": "A pair of skis"
  48. }
  49. POST index/_refresh
  50. --------------------------------------------------
  51. // CONSOLE
  52. With such a setup, searching for `ski` on `body` would return both documents:
  53. [source,js]
  54. --------------------------------------------------
  55. GET index/_search
  56. {
  57. "query": {
  58. "simple_query_string": {
  59. "fields": [ "body" ],
  60. "query": "ski"
  61. }
  62. }
  63. }
  64. --------------------------------------------------
  65. // CONSOLE
  66. // TEST[continued]
  67. [source,js]
  68. --------------------------------------------------
  69. {
  70. "took": 2,
  71. "timed_out": false,
  72. "_shards": {
  73. "total": 1,
  74. "successful": 1,
  75. "skipped" : 0,
  76. "failed": 0
  77. },
  78. "hits": {
  79. "total": 2,
  80. "max_score": 0.18232156,
  81. "hits": [
  82. {
  83. "_index": "index",
  84. "_type": "_doc",
  85. "_id": "1",
  86. "_score": 0.18232156,
  87. "_source": {
  88. "body": "Ski resort"
  89. }
  90. },
  91. {
  92. "_index": "index",
  93. "_type": "_doc",
  94. "_id": "2",
  95. "_score": 0.18232156,
  96. "_source": {
  97. "body": "A pair of skis"
  98. }
  99. }
  100. ]
  101. }
  102. }
  103. --------------------------------------------------
  104. // TESTRESPONSE[s/"took": 2,/"took": "$body.took",/]
  105. On the other hand, searching for `ski` on `body.exact` would only return
  106. document `1` since the analysis chain of `body.exact` does not perform
  107. stemming.
  108. [source,js]
  109. --------------------------------------------------
  110. GET index/_search
  111. {
  112. "query": {
  113. "simple_query_string": {
  114. "fields": [ "body.exact" ],
  115. "query": "ski"
  116. }
  117. }
  118. }
  119. --------------------------------------------------
  120. // CONSOLE
  121. // TEST[continued]
  122. [source,js]
  123. --------------------------------------------------
  124. {
  125. "took": 1,
  126. "timed_out": false,
  127. "_shards": {
  128. "total": 1,
  129. "successful": 1,
  130. "skipped" : 0,
  131. "failed": 0
  132. },
  133. "hits": {
  134. "total": 1,
  135. "max_score": 0.8025915,
  136. "hits": [
  137. {
  138. "_index": "index",
  139. "_type": "_doc",
  140. "_id": "1",
  141. "_score": 0.8025915,
  142. "_source": {
  143. "body": "Ski resort"
  144. }
  145. }
  146. ]
  147. }
  148. }
  149. --------------------------------------------------
  150. // TESTRESPONSE[s/"took": 1,/"took": "$body.took",/]
  151. This is not something that is easy to expose to end users, as we would need to
  152. have a way to figure out whether they are looking for an exact match or not and
  153. redirect to the appropriate field accordingly. Also what to do if only parts of
  154. the query need to be matched exactly while other parts should still take
  155. stemming into account?
  156. Fortunately, the `query_string` and `simple_query_string` queries have a feature
  157. that solve this exact problem: `quote_field_suffix`. This tell Elasticsearch
  158. that the words that appear in between quotes are to be redirected to a different
  159. field, see below:
  160. [source,js]
  161. --------------------------------------------------
  162. GET index/_search
  163. {
  164. "query": {
  165. "simple_query_string": {
  166. "fields": [ "body" ],
  167. "quote_field_suffix": ".exact",
  168. "query": "\"ski\""
  169. }
  170. }
  171. }
  172. --------------------------------------------------
  173. // CONSOLE
  174. // TEST[continued]
  175. [source,js]
  176. --------------------------------------------------
  177. {
  178. "took": 2,
  179. "timed_out": false,
  180. "_shards": {
  181. "total": 1,
  182. "successful": 1,
  183. "skipped" : 0,
  184. "failed": 0
  185. },
  186. "hits": {
  187. "total": 1,
  188. "max_score": 0.8025915,
  189. "hits": [
  190. {
  191. "_index": "index",
  192. "_type": "_doc",
  193. "_id": "1",
  194. "_score": 0.8025915,
  195. "_source": {
  196. "body": "Ski resort"
  197. }
  198. }
  199. ]
  200. }
  201. }
  202. --------------------------------------------------
  203. // TESTRESPONSE[s/"took": 2,/"took": "$body.took",/]
  204. In the above case, since `ski` was in-between quotes, it was searched on the
  205. `body.exact` field due to the `quote_field_suffix` parameter, so only document
  206. `1` matched. This allows users to mix exact search with stemmed search as they
  207. like.