match-phrase-prefix-query.asciidoc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. [[query-dsl-match-query-phrase-prefix]]
  2. === Match Phrase Prefix Query
  3. The `match_phrase_prefix` is the same as `match_phrase`, except that it
  4. allows for prefix matches on the last term in the text. For example:
  5. [source,js]
  6. --------------------------------------------------
  7. GET /_search
  8. {
  9. "query": {
  10. "match_phrase_prefix" : {
  11. "message" : "quick brown f"
  12. }
  13. }
  14. }
  15. --------------------------------------------------
  16. // CONSOLE
  17. It accepts the same parameters as the phrase type. In addition, it also
  18. accepts a `max_expansions` parameter (default `50`) that can control to how
  19. many suffixes the last term will be expanded. It is highly recommended to set
  20. it to an acceptable value to control the execution time of the query. For
  21. example:
  22. [source,js]
  23. --------------------------------------------------
  24. GET /_search
  25. {
  26. "query": {
  27. "match_phrase_prefix" : {
  28. "message" : {
  29. "query" : "quick brown f",
  30. "max_expansions" : 10
  31. }
  32. }
  33. }
  34. }
  35. --------------------------------------------------
  36. // CONSOLE
  37. [IMPORTANT]
  38. ===================================================
  39. The `match_phrase_prefix` query is a poor-man's autocomplete. It is very easy
  40. to use, which lets you get started quickly with _search-as-you-type_ but its
  41. results, which usually are good enough, can sometimes be confusing.
  42. Consider the query string `quick brown f`. This query works by creating a
  43. phrase query out of `quick` and `brown` (i.e. the term `quick` must exist and
  44. must be followed by the term `brown`). Then it looks at the sorted term
  45. dictionary to find the first 50 terms that begin with `f`, and
  46. adds these terms to the phrase query.
  47. The problem is that the first 50 terms may not include the term `fox` so the
  48. phrase `quick brown fox` will not be found. This usually isn't a problem as
  49. the user will continue to type more letters until the word they are looking
  50. for appears.
  51. For better solutions for _search-as-you-type_ see the
  52. <<search-suggesters-completion,completion suggester>> and
  53. {defguide}/_index_time_search_as_you_type.html[Index-Time Search-as-You-Type].
  54. ===================================================