suggesters.asciidoc 3.9 KB

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