suggesters.asciidoc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. [[search-suggesters]]
  2. === Suggesters
  3. Suggests similar looking terms based on a provided text by using a suggester.
  4. Parts of the suggest feature are still under development.
  5. [source,console]
  6. --------------------------------------------------
  7. POST my-index-000001/_search
  8. {
  9. "query" : {
  10. "match": {
  11. "message": "tring out Elasticsearch"
  12. }
  13. },
  14. "suggest" : {
  15. "my-suggestion" : {
  16. "text" : "tring out Elasticsearch",
  17. "term" : {
  18. "field" : "message"
  19. }
  20. }
  21. }
  22. }
  23. --------------------------------------------------
  24. // TEST[setup:messages]
  25. [[search-suggesters-api-request]]
  26. ==== {api-request-title}
  27. The suggest feature suggests similar looking terms based on a provided text by
  28. using a suggester. The suggest request part is defined alongside the query part
  29. in a `_search` request. If the query part is left out, only suggestions are
  30. returned.
  31. NOTE: `_suggest` endpoint has been deprecated in favour of using suggest via
  32. `_search` endpoint. In 5.0, the `_search` endpoint has been optimized for
  33. suggest only search requests.
  34. [[search-suggesters-api-example]]
  35. ==== {api-examples-title}
  36. Several suggestions can be specified per request. Each suggestion is identified
  37. with an arbitrary name. In the example below two suggestions are requested. Both
  38. `my-suggest-1` and `my-suggest-2` suggestions use the `term` suggester, but have
  39. a different `text`.
  40. [source,console]
  41. --------------------------------------------------
  42. POST _search
  43. {
  44. "suggest": {
  45. "my-suggest-1" : {
  46. "text" : "tring out Elasticsearch",
  47. "term" : {
  48. "field" : "message"
  49. }
  50. },
  51. "my-suggest-2" : {
  52. "text" : "kmichy",
  53. "term" : {
  54. "field" : "user.id"
  55. }
  56. }
  57. }
  58. }
  59. --------------------------------------------------
  60. // TEST[setup:messages]
  61. // TEST[s/^/PUT my-index-000001\/_mapping\n{"properties":{"user":{"properties":{"id":{"type":"keyword"}}}}}\n/]
  62. The below suggest response example includes the suggestion response for
  63. `my-suggest-1` and `my-suggest-2`. Each suggestion part contains
  64. entries. Each entry is effectively a token from the suggest text and
  65. contains the suggestion entry text, the original start offset and length
  66. in the suggest text and if found an arbitrary number of options.
  67. [source,console-result]
  68. --------------------------------------------------
  69. {
  70. "_shards": ...
  71. "hits": ...
  72. "took": 2,
  73. "timed_out": false,
  74. "suggest": {
  75. "my-suggest-1": [ {
  76. "text": "tring",
  77. "offset": 0,
  78. "length": 5,
  79. "options": [ {"text": "trying", "score": 0.8, "freq": 1 } ]
  80. }, {
  81. "text": "out",
  82. "offset": 6,
  83. "length": 3,
  84. "options": []
  85. }, {
  86. "text": "elasticsearch",
  87. "offset": 10,
  88. "length": 13,
  89. "options": []
  90. } ],
  91. "my-suggest-2": ...
  92. }
  93. }
  94. --------------------------------------------------
  95. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": "$body._shards",/]
  96. // TESTRESPONSE[s/"hits": .../"hits": "$body.hits",/]
  97. // TESTRESPONSE[s/"took": 2,/"took": "$body.took",/]
  98. // TESTRESPONSE[s/"my-suggest-2": \.\.\./"my-suggest-2": "$body.suggest.my-suggest-2"/]
  99. Each options array contains an option object that includes the
  100. suggested text, its document frequency and score compared to the suggest
  101. entry text. The meaning of the score depends on the used suggester. The
  102. term suggester's score is based on the edit distance.
  103. [discrete]
  104. [[global-suggest]]
  105. ===== Global suggest text
  106. To avoid repetition of the suggest text, it is possible to define a
  107. global text. In the example below the suggest text is defined globally
  108. and applies to the `my-suggest-1` and `my-suggest-2` suggestions.
  109. [source,console]
  110. --------------------------------------------------
  111. POST _search
  112. {
  113. "suggest": {
  114. "text" : "tring out Elasticsearch",
  115. "my-suggest-1" : {
  116. "term" : {
  117. "field" : "message"
  118. }
  119. },
  120. "my-suggest-2" : {
  121. "term" : {
  122. "field" : "user"
  123. }
  124. }
  125. }
  126. }
  127. --------------------------------------------------
  128. The suggest text can in the above example also be specified as
  129. suggestion specific option. The suggest text specified on suggestion
  130. level override the suggest text on the global level.
  131. include::suggesters/term-suggest.asciidoc[]
  132. include::suggesters/phrase-suggest.asciidoc[]
  133. include::suggesters/completion-suggest.asciidoc[]
  134. include::suggesters/context-suggest.asciidoc[]
  135. include::suggesters/misc.asciidoc[]