stemming.asciidoc 5.0 KB

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