Autocomplete functionality

Autocomplete shows suggestions to users while they type.

For example, if a user types “pop,” OpenSearch provides suggestions like “popcorn” or “popsicles.” These suggestions preempt your user’s intention and lead them to a possible search term more quickly.

OpenSearch lets you design autocomplete that updates with each keystroke, provides a few relevant suggestions, and tolerates typos.

Implement autocomplete using one of the following methods:

While prefix matching happens at query time, the other three methods happen at index time. All methods are described in the following sections.

Prefix matching

Prefix matching finds documents that match the last term in a query string.

For example, assume that the user types “qui” into a search UI. To autocomplete this phrase, use the match_phrase_prefix query to search for all text_entry field values that begin with the prefix “qui”:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "text_entry": {
  6. "query": "qui",
  7. "slop": 3
  8. }
  9. }
  10. }
  11. }

To make the word order and relative positions flexible, specify a slop value. To learn about the slop option, see Other advanced options.

Prefix matching doesn’t require any special mappings. It works with your data as is. However, it’s a fairly resource-intensive operation. A prefix of a could match hundreds of thousands of terms and not be useful to your user. To limit the impact of prefix expansion, set max_expansions to a reasonable number:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "text_entry": {
  6. "query": "qui",
  7. "slop": 3,
  8. "max_expansions": 10
  9. }
  10. }
  11. }
  12. }

To learn about the max_expansions option, see Other advanced options.

The ease of implementing query-time autocomplete comes at the cost of performance. When implementing this feature on a large scale, we recommend an index-time solution. With an index-time solution, you might experience slower indexing, but it’s a price you pay only once and not for every query. The edge n-gram, search-as-you-type, and completion suggester methods are index-time solutions.

Edge n-gram matching

During indexing, edge n-grams split a word into a sequence of n characters to support a faster lookup of partial search terms.

If you n-gram the word “quick,” the results depend on the value of n.

nTypen-gram
1Unigram[ q, u, i, c, k ]
2Bigram[ qu, ui, ic, ck ]
3Trigram[ qui, uic, ick ]
4Four-gram[ quic, uick ]
5Five-gram[ quick ]

Autocomplete needs only the beginning n-grams of a search phrase, so OpenSearch uses a special type of n-gram called edge n-gram.

Edge n-gramming the word “quick” results in the following:

  • q
  • qu
  • qui
  • quic
  • quick

This follows the same sequence the user types.

To configure a field to use edge n-grams, create an autocomplete analyzer with an edge_ngram filter:

  1. PUT shakespeare
  2. {
  3. "mappings": {
  4. "properties": {
  5. "text_entry": {
  6. "type": "text",
  7. "analyzer": "autocomplete"
  8. }
  9. }
  10. },
  11. "settings": {
  12. "analysis": {
  13. "filter": {
  14. "edge_ngram_filter": {
  15. "type": "edge_ngram",
  16. "min_gram": 1,
  17. "max_gram": 20
  18. }
  19. },
  20. "analyzer": {
  21. "autocomplete": {
  22. "type": "custom",
  23. "tokenizer": "standard",
  24. "filter": [
  25. "lowercase",
  26. "edge_ngram_filter"
  27. ]
  28. }
  29. }
  30. }
  31. }
  32. }

This example creates the index and instantiates the edge n-gram filter and analyzer.

The edge_ngram_filter produces edge n-grams with a minimum n-gram length of 1 (a single letter) and a maximum length of 20. So it offers suggestions for words of up to 20 letters.

The autocomplete analyzer tokenizes a string into individual terms, lowercases the terms, and then produces edge n-grams for each term using the edge_ngram_filter.

Use the analyze operation to test this analyzer:

  1. POST shakespeare/_analyze
  2. {
  3. "analyzer": "autocomplete",
  4. "text": "quick"
  5. }

It returns edge n-grams as tokens:

  • q
  • qu
  • qui
  • quic
  • quick

Use the standard analyzer at search time. Otherwise, the search query splits into edge n-grams and you get results for everything that matches q, u, and i. This is one of the few occasions when you use different analyzers at index time and at query time:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match": {
  5. "text_entry": {
  6. "query": "qui",
  7. "analyzer": "standard"
  8. }
  9. }
  10. }
  11. }

The response contains the matching documents:

  1. {
  2. "took": 5,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 1,
  6. "successful": 1,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": {
  12. "value": 533,
  13. "relation": "eq"
  14. },
  15. "max_score": 9.712725,
  16. "hits": [
  17. {
  18. "_index": "shakespeare",
  19. "_id": "22006",
  20. "_score": 9.712725,
  21. "_source": {
  22. "type": "line",
  23. "line_id": 22007,
  24. "play_name": "Antony and Cleopatra",
  25. "speech_number": 12,
  26. "line_number": "5.2.44",
  27. "speaker": "CLEOPATRA",
  28. "text_entry": "Quick, quick, good hands."
  29. }
  30. },
  31. {
  32. "_index": "shakespeare",
  33. "_id": "54665",
  34. "_score": 9.712725,
  35. "_source": {
  36. "type": "line",
  37. "line_id": 54666,
  38. "play_name": "Loves Labours Lost",
  39. "speech_number": 21,
  40. "line_number": "5.1.52",
  41. "speaker": "HOLOFERNES",
  42. "text_entry": "Quis, quis, thou consonant?"
  43. }
  44. }
  45. ...
  46. ]
  47. }
  48. }

Alternatively, specify the search_analyzer in the mapping itself:

  1. "mappings": {
  2. "properties": {
  3. "text_entry": {
  4. "type": "text",
  5. "analyzer": "autocomplete",
  6. "search_analyzer": "standard"
  7. }
  8. }
  9. }

Completion suggester

The completion suggester accepts a list of suggestions and builds them into a finite-state transducer (FST), an optimized data structure that is essentially a graph. This data structure lives in memory and is optimized for fast prefix lookups. To learn more about FSTs, see Wikipedia.

As the user types, the completion suggester moves through the FST graph one character at a time along a matching path. After it runs out of user input, it examines the remaining endings to produce a list of suggestions.

The completion suggester makes your autocomplete solution as efficient as possible and lets you have explicit control over its suggestions.

Use a dedicated field type called completion, which stores the FST-like data structures in the index:

  1. PUT shakespeare
  2. {
  3. "mappings": {
  4. "properties": {
  5. "text_entry": {
  6. "type": "completion"
  7. }
  8. }
  9. }
  10. }

To get suggestions, use the search endpoint with the suggest parameter:

  1. GET shakespeare/_search
  2. {
  3. "suggest": {
  4. "autocomplete": {
  5. "prefix": "To be",
  6. "completion": {
  7. "field": "text_entry"
  8. }
  9. }
  10. }
  11. }

The phrase “to be” is prefix matched with the FST of the text_entry field:

  1. {
  2. "took" : 29,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "suggest" : {
  19. "autocomplete" : [
  20. {
  21. "text" : "To be",
  22. "offset" : 0,
  23. "length" : 5,
  24. "options" : [
  25. {
  26. "text" : "To be a comrade with the wolf and owl,--",
  27. "_index" : "shakespeare",
  28. "_id" : "50652",
  29. "_score" : 1.0,
  30. "_source" : {
  31. "type" : "line",
  32. "line_id" : 50653,
  33. "play_name" : "King Lear",
  34. "speech_number" : 68,
  35. "line_number" : "2.4.230",
  36. "speaker" : "KING LEAR",
  37. "text_entry" : "To be a comrade with the wolf and owl,--"
  38. }
  39. },
  40. {
  41. "text" : "To be a make-peace shall become my age:",
  42. "_index" : "shakespeare",
  43. "_id" : "78566",
  44. "_score" : 1.0,
  45. "_source" : {
  46. "type" : "line",
  47. "line_id" : 78567,
  48. "play_name" : "Richard II",
  49. "speech_number" : 20,
  50. "line_number" : "1.1.160",
  51. "speaker" : "JOHN OF GAUNT",
  52. "text_entry" : "To be a make-peace shall become my age:"
  53. }
  54. },
  55. {
  56. "text" : "To be a party in this injury.",
  57. "_index" : "shakespeare",
  58. "_id" : "75259",
  59. "_score" : 1.0,
  60. "_source" : {
  61. "type" : "line",
  62. "line_id" : 75260,
  63. "play_name" : "Othello",
  64. "speech_number" : 57,
  65. "line_number" : "5.1.93",
  66. "speaker" : "IAGO",
  67. "text_entry" : "To be a party in this injury."
  68. }
  69. },
  70. {
  71. "text" : "To be a preparation gainst the Polack;",
  72. "_index" : "shakespeare",
  73. "_id" : "33591",
  74. "_score" : 1.0,
  75. "_source" : {
  76. "type" : "line",
  77. "line_id" : 33592,
  78. "play_name" : "Hamlet",
  79. "speech_number" : 17,
  80. "line_number" : "2.2.67",
  81. "speaker" : "VOLTIMAND",
  82. "text_entry" : "To be a preparation gainst the Polack;"
  83. }
  84. },
  85. {
  86. "text" : "To be a public spectacle to all:",
  87. "_index" : "shakespeare",
  88. "_id" : "3709",
  89. "_score" : 1.0,
  90. "_source" : {
  91. "type" : "line",
  92. "line_id" : 3710,
  93. "play_name" : "Henry VI Part 1",
  94. "speech_number" : 6,
  95. "line_number" : "1.4.41",
  96. "speaker" : "TALBOT",
  97. "text_entry" : "To be a public spectacle to all:"
  98. }
  99. }
  100. ]
  101. }
  102. ]
  103. }
  104. }

To specify the number of suggestions that you want to return, use the size parameter:

  1. GET shakespeare/_search
  2. {
  3. "suggest": {
  4. "autocomplete": {
  5. "prefix": "To n",
  6. "completion": {
  7. "field": "text_entry",
  8. "size": 3
  9. }
  10. }
  11. }
  12. }

The maximum of three documents is returned:

  1. {
  2. "took" : 4109,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "suggest" : {
  19. "autocomplete" : [
  20. {
  21. "text" : "To n",
  22. "offset" : 0,
  23. "length" : 4,
  24. "options" : [
  25. {
  26. "text" : "To NESTOR",
  27. "_index" : "shakespeare",
  28. "_id" : "99707",
  29. "_score" : 1.0,
  30. "_source" : {
  31. "type" : "line",
  32. "line_id" : 99708,
  33. "play_name" : "Troilus and Cressida",
  34. "speech_number" : 3,
  35. "line_number" : "",
  36. "speaker" : "ULYSSES",
  37. "text_entry" : "To NESTOR"
  38. }
  39. },
  40. {
  41. "text" : "To name the bigger light, and how the less,",
  42. "_index" : "shakespeare",
  43. "_id" : "91884",
  44. "_score" : 1.0,
  45. "_source" : {
  46. "type" : "line",
  47. "line_id" : 91885,
  48. "play_name" : "The Tempest",
  49. "speech_number" : 91,
  50. "line_number" : "1.2.394",
  51. "speaker" : "CALIBAN",
  52. "text_entry" : "To name the bigger light, and how the less,"
  53. }
  54. },
  55. {
  56. "text" : "To nature none more bound; his training such,",
  57. "_index" : "shakespeare",
  58. "_id" : "40510",
  59. "_score" : 1.0,
  60. "_source" : {
  61. "type" : "line",
  62. "line_id" : 40511,
  63. "play_name" : "Henry VIII",
  64. "speech_number" : 18,
  65. "line_number" : "1.2.126",
  66. "speaker" : "KING HENRY VIII",
  67. "text_entry" : "To nature none more bound; his training such,"
  68. }
  69. }
  70. ]
  71. }
  72. ]
  73. }
  74. }

The suggest parameter finds suggestions using only prefix matching. For example, the document “To be, or not to be” is not part of the results. If you want specific documents returned as suggestions, you can manually add curated suggestions and add weights to prioritize your suggestions.

Index a document with input suggestions and assign a weight:

  1. PUT shakespeare/_doc/1?refresh=true
  2. {
  3. "text_entry": {
  4. "input": [
  5. "To n", "To be, or not to be: that is the question:"
  6. ],
  7. "weight": 10
  8. }
  9. }

Perform the same search:

  1. GET shakespeare/_search
  2. {
  3. "suggest": {
  4. "autocomplete": {
  5. "prefix": "To n",
  6. "completion": {
  7. "field": "text_entry",
  8. "size": 3
  9. }
  10. }
  11. }
  12. }

You see the indexed document as the first result:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "suggest" : {
  19. "autocomplete" : [
  20. {
  21. "text" : "To n",
  22. "offset" : 0,
  23. "length" : 4,
  24. "options" : [
  25. {
  26. "text" : "To n",
  27. "_index" : "shakespeare",
  28. "_id" : "1",
  29. "_score" : 10.0,
  30. "_source" : {
  31. "text_entry" : {
  32. "input" : [
  33. "To n",
  34. "To be, or not to be: that is the question:"
  35. ],
  36. "weight" : 10
  37. }
  38. }
  39. },
  40. {
  41. "text" : "To NESTOR",
  42. "_index" : "shakespeare",
  43. "_id" : "99707",
  44. "_score" : 1.0,
  45. "_source" : {
  46. "type" : "line",
  47. "line_id" : 99708,
  48. "play_name" : "Troilus and Cressida",
  49. "speech_number" : 3,
  50. "line_number" : "",
  51. "speaker" : "ULYSSES",
  52. "text_entry" : "To NESTOR"
  53. }
  54. },
  55. {
  56. "text" : "To name the bigger light, and how the less,",
  57. "_index" : "shakespeare",
  58. "_id" : "91884",
  59. "_score" : 1.0,
  60. "_source" : {
  61. "type" : "line",
  62. "line_id" : 91885,
  63. "play_name" : "The Tempest",
  64. "speech_number" : 91,
  65. "line_number" : "1.2.394",
  66. "speaker" : "CALIBAN",
  67. "text_entry" : "To name the bigger light, and how the less,"
  68. }
  69. }
  70. ]
  71. }
  72. ]
  73. }
  74. }

You can also allow for misspellings in queries by specifying the fuzzy parameter:

  1. GET shakespeare/_search
  2. {
  3. "suggest": {
  4. "autocomplete": {
  5. "prefix": "rosenkrantz",
  6. "completion": {
  7. "field": "text_entry",
  8. "size": 3,
  9. "fuzzy" : {
  10. "fuzziness" : "AUTO"
  11. }
  12. }
  13. }
  14. }
  15. }

The result matches the correct spelling:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "suggest" : {
  19. "autocomplete" : [
  20. {
  21. "text" : "rosenkrantz",
  22. "offset" : 0,
  23. "length" : 11,
  24. "options" : [
  25. {
  26. "text" : "ROSENCRANTZ:",
  27. "_index" : "shakespeare",
  28. "_id" : "35196",
  29. "_score" : 5.0,
  30. "_source" : {
  31. "type" : "line",
  32. "line_id" : 35197,
  33. "play_name" : "Hamlet",
  34. "speech_number" : 2,
  35. "line_number" : "4.2.1",
  36. "speaker" : "HAMLET",
  37. "text_entry" : "ROSENCRANTZ:"
  38. }
  39. }
  40. ]
  41. }
  42. ]
  43. }
  44. }

You can use a regular expression to define the prefix for the completion suggester query:

  1. GET shakespeare/_search
  2. {
  3. "suggest": {
  4. "autocomplete": {
  5. "prefix": "rosen*",
  6. "completion": {
  7. "field": "text_entry",
  8. "size": 3
  9. }
  10. }
  11. }
  12. }

For more information, see the completion field type documentation.

Search as you type

OpenSearch has a dedicated search_as_you_type field type that is optimized for search-as-you-type functionality and can match terms using both prefix and infix completion. The search_as_you_type field does not require you to set up a custom analyzer or index suggestions beforehand.

First, map the field as search_as_you_type:

  1. PUT shakespeare
  2. {
  3. "mappings": {
  4. "properties": {
  5. "text_entry": {
  6. "type": "search_as_you_type"
  7. }
  8. }
  9. }
  10. }

After you index a document, OpenSearch automatically creates and stores its n-grams and edge n-grams. For example, consider the string that is the question. First, it is split into terms using the standard analyzer, and the terms are stored in the text_entry field:

  1. [
  2. "that",
  3. "is",
  4. "the",
  5. "question"
  6. ]

In addition to storing these terms, the following 2-grams for this field are stored in the field text_entry._2gram:

  1. [
  2. "that is",
  3. "is the",
  4. "the question"
  5. ]

The following 3-grams for this field are stored in the field text_entry._3gram:

  1. [
  2. "that is the",
  3. "is the question"
  4. ]

Finally, after an edge n-gram token filter is applied, the resulting terms are stored in the text_entry._index_prefix field:

  1. [
  2. "t",
  3. "th",
  4. "tha",
  5. "that",
  6. ...
  7. ]

You can then match terms in any order using the bool_prefix type of a multi-match query:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "uncle what",
  6. "type": "bool_prefix",
  7. "fields": [
  8. "text_entry",
  9. "text_entry._2gram",
  10. "text_entry._3gram"
  11. ]
  12. }
  13. },
  14. "size": 3
  15. }

The documents in which the words appear in the same order as in the query are ranked higher in the results:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 4759,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 10.437667,
  16. "hits" : [
  17. {
  18. "_index" : "shakespeare",
  19. "_id" : "2817",
  20. "_score" : 10.437667,
  21. "_source" : {
  22. "type" : "line",
  23. "line_id" : 2818,
  24. "play_name" : "Henry IV",
  25. "speech_number" : 5,
  26. "line_number" : "5.2.31",
  27. "speaker" : "HOTSPUR",
  28. "text_entry" : "Uncle, what news?"
  29. }
  30. },
  31. {
  32. "_index" : "shakespeare",
  33. "_id" : "37085",
  34. "_score" : 9.437667,
  35. "_source" : {
  36. "type" : "line",
  37. "line_id" : 37086,
  38. "play_name" : "Henry V",
  39. "speech_number" : 26,
  40. "line_number" : "1.2.262",
  41. "speaker" : "KING HENRY V",
  42. "text_entry" : "What treasure, uncle?"
  43. }
  44. },
  45. {
  46. "_index" : "shakespeare",
  47. "_id" : "79274",
  48. "_score" : 9.358302,
  49. "_source" : {
  50. "type" : "line",
  51. "line_id" : 79275,
  52. "play_name" : "Richard II",
  53. "speech_number" : 29,
  54. "line_number" : "2.1.187",
  55. "speaker" : "KING RICHARD II",
  56. "text_entry" : "Why, uncle, whats the matter?"
  57. }
  58. }
  59. ]
  60. }
  61. }

To match terms in order, you can use a match_phrase_prefix query:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "text_entry": "uncle wha"
  6. }
  7. },
  8. "size": 3
  9. }

The response contains documents that match the prefix:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 6,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 16.37664,
  16. "hits" : [
  17. {
  18. "_index" : "shakespeare",
  19. "_id" : "2817",
  20. "_score" : 16.37664,
  21. "_source" : {
  22. "type" : "line",
  23. "line_id" : 2818,
  24. "play_name" : "Henry IV",
  25. "speech_number" : 5,
  26. "line_number" : "5.2.31",
  27. "speaker" : "HOTSPUR",
  28. "text_entry" : "Uncle, what news?"
  29. }
  30. },
  31. {
  32. "_index" : "shakespeare",
  33. "_id" : "6789",
  34. "_score" : 16.37664,
  35. "_source" : {
  36. "type" : "line",
  37. "line_id" : 6790,
  38. "play_name" : "Henry VI Part 2",
  39. "speech_number" : 60,
  40. "line_number" : "1.3.202",
  41. "speaker" : "KING HENRY VI",
  42. "text_entry" : "Uncle, what shall we say to this in law?"
  43. }
  44. },
  45. {
  46. "_index" : "shakespeare",
  47. "_id" : "7877",
  48. "_score" : 16.37664,
  49. "_source" : {
  50. "type" : "line",
  51. "line_id" : 7878,
  52. "play_name" : "Henry VI Part 2",
  53. "speech_number" : 13,
  54. "line_number" : "3.2.28",
  55. "speaker" : "KING HENRY VI",
  56. "text_entry" : "Where is our uncle? whats the matter, Suffolk?"
  57. }
  58. }
  59. ]
  60. }
  61. }

Finally, to match the last term exactly and not as a prefix, you can use a match_phrase query:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "text_entry": "uncle what"
  6. }
  7. },
  8. "size": 5
  9. }

The response contains exact matches:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 14.437452,
  16. "hits" : [
  17. {
  18. "_index" : "shakespeare",
  19. "_id" : "2817",
  20. "_score" : 14.437452,
  21. "_source" : {
  22. "type" : "line",
  23. "line_id" : 2818,
  24. "play_name" : "Henry IV",
  25. "speech_number" : 5,
  26. "line_number" : "5.2.31",
  27. "speaker" : "HOTSPUR",
  28. "text_entry" : "Uncle, what news?"
  29. }
  30. },
  31. {
  32. "_index" : "shakespeare",
  33. "_id" : "6789",
  34. "_score" : 9.461917,
  35. "_source" : {
  36. "type" : "line",
  37. "line_id" : 6790,
  38. "play_name" : "Henry VI Part 2",
  39. "speech_number" : 60,
  40. "line_number" : "1.3.202",
  41. "speaker" : "KING HENRY VI",
  42. "text_entry" : "Uncle, what shall we say to this in law?"
  43. }
  44. },
  45. {
  46. "_index" : "shakespeare",
  47. "_id" : "100955",
  48. "_score" : 8.947967,
  49. "_source" : {
  50. "type" : "line",
  51. "line_id" : 100956,
  52. "play_name" : "Troilus and Cressida",
  53. "speech_number" : 28,
  54. "line_number" : "3.2.98",
  55. "speaker" : "CRESSIDA",
  56. "text_entry" : "Well, uncle, what folly I commit, I dedicate to you."
  57. }
  58. }
  59. ]
  60. }
  61. }

If you modify the text in the previous match_phrase query and omit the last letter, none of the documents in the previous response are returned:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "text_entry": "uncle wha"
  6. }
  7. }
  8. }

The result is empty:

  1. {
  2. "took" : 1,
  3. "timed_out" : false,
  4. "_shards" : {
  5. "total" : 1,
  6. "successful" : 1,
  7. "skipped" : 0,
  8. "failed" : 0
  9. },
  10. "hits" : {
  11. "total" : {
  12. "value" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. }
  18. }

For more information, see the search_as_you_type field type documentation.