Search experience

Expectations from search engines have evolved over the years. Just returning relevant results quickly is no longer enough for most users. OpenSearch includes many features that enhance the user’s search experience as follows:

FeatureDescription
Autocomplete queriesSuggest phrases as the user types.
Paginate resultsRather than a single, long list, break search results into pages.
Scroll searchReturn a large number of results in batches.
Sort resultsAllow sorting results by different criteria.
Highlight query matchesHighlight the search term in the results.

Autocomplete queries

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 three methods:

  • Prefix matching
  • Edge N-gram matching
  • Completion suggesters

These methods are described in the following sections.

Prefix matching

Prefix matching finds documents that matches the last term in the 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 all text_entry fields that begin with the prefix “qui.” To make the word order and relative positions flexible, specify a slop value. To learn about the slop option, see Options.

Sample Request

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

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. To learn about the max_expansions option, see Options.

Sample Request

  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. }

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 and completion suggester methods are index time.

Edge N-gram matching

During indexing, edge N-grams chop up 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:

Sample Request

  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 where you use a different analyzer on the index and query side.

Sample Request

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

Sample Response

  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. "_type": "_doc",
  20. "_id": "22006",
  21. "_score": 9.712725,
  22. "_source": {
  23. "type": "line",
  24. "line_id": 22007,
  25. "play_name": "Antony and Cleopatra",
  26. "speech_number": 12,
  27. "line_number": "5.2.44",
  28. "speaker": "CLEOPATRA",
  29. "text_entry": "Quick, quick, good hands."
  30. }
  31. },
  32. {
  33. "_index": "shakespeare",
  34. "_type": "_doc",
  35. "_id": "54665",
  36. "_score": 9.712725,
  37. "_source": {
  38. "type": "line",
  39. "line_id": 54666,
  40. "play_name": "Loves Labours Lost",
  41. "speech_number": 21,
  42. "line_number": "5.1.52",
  43. "speaker": "HOLOFERNES",
  44. "text_entry": "Quis, quis, thou consonant?"
  45. }
  46. }
  47. ]
  48. }
  49. }

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’s 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 back 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.

Sample Response

  1. {
  2. "took": 9,
  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. "text_entry": [
  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. "_type": "_doc",
  29. "_id": "50652",
  30. "_score": 1,
  31. "_source": {
  32. "type": "line",
  33. "line_id": 50653,
  34. "play_name": "King Lear",
  35. "speech_number": 68,
  36. "line_number": "2.4.230",
  37. "speaker": "KING LEAR",
  38. "text_entry": "To be a comrade with the wolf and owl,--"
  39. }
  40. },
  41. {
  42. "text": "To be a make-peace shall become my age:",
  43. "_index": "shakespeare",
  44. "_type": "_doc",
  45. "_id": "78566",
  46. "_score": 1,
  47. "_source": {
  48. "type": "line",
  49. "line_id": 78567,
  50. "play_name": "Richard II",
  51. "speech_number": 20,
  52. "line_number": "1.1.160",
  53. "speaker": "JOHN OF GAUNT",
  54. "text_entry": "To be a make-peace shall become my age:"
  55. }
  56. }
  57. ]
  58. }
  59. ]
  60. }
  61. }

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 m",
  6. "completion": {
  7. "field": "text_entry",
  8. "size": 3
  9. }
  10. }
  11. }
  12. }

Sample Response

  1. {
  2. "took": 3,
  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. "text_entry": [
  20. {
  21. "text": "To m",
  22. "offset": 0,
  23. "length": 5,
  24. "options": [
  25. {
  26. "text": "To make a bastard and a slave of me!",
  27. "_index": "shakespeare",
  28. "_type": "_doc",
  29. "_id": "5369",
  30. "_score": 4,
  31. "_source": {
  32. "type": "line",
  33. "line_id": 5370,
  34. "play_name": "Henry VI Part 1",
  35. "speech_number": 2,
  36. "line_number": "4.5.15",
  37. "speaker": "JOHN TALBOT",
  38. "text_entry": "To make a bastard and a slave of me!"
  39. }
  40. },
  41. {
  42. "text": "To make a bloody supper in the Tower.",
  43. "_index": "shakespeare",
  44. "_type": "_doc",
  45. "_id": "12504",
  46. "_score": 4,
  47. "_source": {
  48. "type": "line",
  49. "line_id": 12505,
  50. "play_name": "Henry VI Part 3",
  51. "speech_number": 40,
  52. "line_number": "5.5.85",
  53. "speaker": "CLARENCE",
  54. "text_entry": "To make a bloody supper in the Tower."
  55. }
  56. }
  57. ]
  58. }
  59. ]
  60. }
  61. }

The suggest parameter finds suggestions using only prefix matching. For example, you don’t get back “To be, or not to be,” which you might want as a suggestion. To work around this issue, manually add curated suggestions and add weights to prioritize your suggestions.

Index a document with an input suggestion and assign a weight:

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

Perform the same search as before:

  1. GET shakespeare/_search
  2. {
  3. "suggest": {
  4. "autocomplete": {
  5. "prefix": "To m",
  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": 1021,
  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 m",
  22. "offset": 0,
  23. "length": 5,
  24. "options": [
  25. {
  26. "text": "To be, or not to be: that is the question:",
  27. "_index": "shakespeare",
  28. "_type": "_doc",
  29. "_id": "1",
  30. "_score": 30,
  31. "_source": {
  32. "text": "To me",
  33. "text_entry": {
  34. "input": [
  35. "To be, or not to be: that is the question:"
  36. ],
  37. "weight": 10
  38. }
  39. }
  40. },
  41. {
  42. "text": "To make a bastard and a slave of me!",
  43. "_index": "shakespeare",
  44. "_type": "_doc",
  45. "_id": "5369",
  46. "_score": 4,
  47. "_source": {
  48. "type": "line",
  49. "line_id": 5370,
  50. "play_name": "Henry VI Part 1",
  51. "speech_number": 2,
  52. "line_number": "4.5.15",
  53. "speaker": "JOHN TALBOT",
  54. "text_entry": "To make a bastard and a slave of me!"
  55. }
  56. }
  57. ]
  58. }
  59. ]
  60. }
  61. }

Use the term suggester to suggest corrected spellings for individual words. The term suggester uses an edit distance to compute suggestions. Edit distance is the number of characters that need to be changed for a term to match.

In this example, the user misspells a search term:

  1. GET shakespeare/_search
  2. {
  3. "suggest": {
  4. "spell-check": {
  5. "text": "lief",
  6. "term": {
  7. "field": "text_entry"
  8. }
  9. }
  10. }
  11. }

The term suggester returns a list of corrections:

  1. {
  2. "took": 48,
  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. "spell-check": [
  20. {
  21. "text": "lief",
  22. "offset": 0,
  23. "length": 4,
  24. "options": [
  25. {
  26. "text": "lifes",
  27. "score": 0.8,
  28. "freq": 21
  29. },
  30. {
  31. "text": "life",
  32. "score": 0.75,
  33. "freq": 805
  34. },
  35. {
  36. "text": "lives",
  37. "score": 0.6,
  38. "freq": 187
  39. },
  40. {
  41. "text": "liege",
  42. "score": 0.6,
  43. "freq": 138
  44. },
  45. {
  46. "text": "lived",
  47. "score": 0.6,
  48. "freq": 80
  49. }
  50. ]
  51. }
  52. ]
  53. }
  54. }

The higher the score, the better the suggestion is. The frequency represents the number of times the term appears in the documents of that index.

To implement a “Did you mean suggestion?” feature, use a phrase suggester. The phrase suggester is similar to the term suggester, except that it uses N-gram language models to suggest whole phrases instead of individual words.

Create a custom analyzer called trigram that uses a shingle filter. This filter is similar to the edge_ngram filter, but it applies to words instead of letters:

  1. PUT shakespeare
  2. {
  3. "settings": {
  4. "index": {
  5. "analysis": {
  6. "analyzer": {
  7. "trigram": {
  8. "type": "custom",
  9. "tokenizer": "standard",
  10. "filter": [
  11. "lowercase",
  12. "shingle"
  13. ]
  14. }
  15. },
  16. "filter": {
  17. "shingle": {
  18. "type": "shingle",
  19. "min_shingle_size": 2,
  20. "max_shingle_size": 3
  21. }
  22. }
  23. }
  24. }
  25. },
  26. "mappings": {
  27. "properties": {
  28. "text_entry": {
  29. "type": "text",
  30. "fields": {
  31. "trigram": {
  32. "type": "text",
  33. "analyzer": "trigram"
  34. }
  35. }
  36. }
  37. }
  38. }
  39. }

This example includes as incorrect phrase:

  1. POST shakespeare/_search
  2. {
  3. "suggest": {
  4. "text": "That the qution",
  5. "simple_phrase": {
  6. "phrase": {
  7. "field": "text_entry.trigram"
  8. }
  9. }
  10. }
  11. }

You get back the corrected phrase:

  1. {
  2. "took": 3,
  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. "simple_phrase": [
  20. {
  21. "text": "That the qution",
  22. "offset": 0,
  23. "length": 18,
  24. "options": [
  25. {
  26. "text": "that is the question",
  27. "score": 0.0015543294
  28. }
  29. ]
  30. }
  31. ]
  32. }
  33. }

Paginate results

The from and size parameters return results to your users one page at a time.

The from parameter is the document number that you want to start showing the results from. The size parameter is the number of results that you want to show. Together, they let you return a subset of the search results.

For example, if the value of size is 10 and the value of from is 0, you see the first 10 results. If you change the value of from to 10, you see the next 10 results (because the results are zero-indexed). So, if you want to see results starting from result 11, from must be 10.

  1. GET shakespeare/_search
  2. {
  3. "from": 0,
  4. "size": 10,
  5. "query": {
  6. "match": {
  7. "play_name": "Hamlet"
  8. }
  9. }
  10. }

To calculate the from parameter relative to the page number:

  1. from = size * (page_number - 1)

Each time the user chooses the next page of the results, your application needs to make the same search query with an incremented from value.

You can also specify the from and size parameters in the search URI:

  1. GET shakespeare/_search?from=0&size=10

If you only specify the size parameter, the from parameter defaults to 0.

Querying for pages deep in your results can have a significant performance impact, so OpenSearch limits this approach to 10,000 results.

The from and size parameters are stateless, so the results are based on the latest available data. This can cause inconsistent pagination. For example, assume a user stays on the first page of the results for a minute and then navigates to the second page; in that time, a new document is indexed in the background which is relevant enough to show up on the first page. In this scenario, the last result of the first page is pushed to the second page, so the user ends up seeing a result on the second page that they already saw on the first page.

Use the scroll operation for consistent pagination. The scroll operation keeps a search context open for a certain period of time. Any data changes do not affect the results during this time.

The from and size parameters allow you to paginate your search results, but with a limit of 10,000 results at a time.

If you need to request massive volumes of data from, for example, a machine learning job, use the scroll operation instead. The scroll operation allows you to request an unlimited number of results.

To use the scroll operation, add a scroll parameter to the request header with a search context to tell OpenSearch how long you need to keep scrolling. This search context needs to be long enough to process a single batch of results.

To set the number of results that you want returned for each batch, use the size parameter:

  1. GET shakespeare/_search?scroll=10m
  2. {
  3. "size": 10000
  4. }

OpenSearch caches the results and returns a scroll ID to access them in batches:

  1. "_scroll_id" : "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAUWdmpUZDhnRFBUcWFtV21nMmFwUGJEQQ=="

Pass this scroll ID to the scroll operation to get back the next batch of results:

  1. GET _search/scroll
  2. {
  3. "scroll": "10m",
  4. "scroll_id": "DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAUWdmpUZDhnRFBUcWFtV21nMmFwUGJEQQ=="
  5. }

Using this scroll ID, you get results in batches of 10,000 as long as the search context is still open. Typically, the scroll ID does not change between requests, but it can change, so make sure to always use the latest scroll ID. If you don’t send the next scroll request within the set search context, the scroll operation does not return any results.

If you expect billions of results, use a sliced scroll. Slicing allows you to perform multiple scroll operations for the same request, but in parallel. Set the ID and the maximum number of slices for the scroll:

  1. GET shakespeare/_search?scroll=10m
  2. {
  3. "slice": {
  4. "id": 0,
  5. "max": 10
  6. },
  7. "query": {
  8. "match_all": {}
  9. }
  10. }

With a single scroll ID, you get back 10 results. You can have up to 10 IDs. Perform the same command with ID equal to 1:

  1. GET shakespeare/_search?scroll=10m
  2. {
  3. "slice": {
  4. "id": 1,
  5. "max": 10
  6. },
  7. "query": {
  8. "match_all": {}
  9. }
  10. }

Close the search context when you’re done scrolling, because it continues to consume computing resources until the timeout:

  1. DELETE _search/scroll/DXF1ZXJ5QW5kRmV0Y2gBAAAAAAAAAAcWdmpUZDhnRFBUcWFtV21nMmFwUGJEQQ==

Sample Response

  1. {
  2. "succeeded": true,
  3. "num_freed": 1
  4. }

To close all open scroll contexts:

  1. DELETE _search/scroll/_all

The scroll operation corresponds to a specific timestamp. It doesn’t consider documents added after that timestamp as potential results.

Because open search contexts consume a lot of memory, we suggest you don’t use the scroll operation for frequent user queries that don’t need the search context open. Instead, use the sort parameter with the search_after parameter to scroll responses for user queries.

Sort results

Sorting allows your users to sort the results in a way that’s most meaningful to them.

By default, full-text queries sort results by the relevance score. You can choose to sort the results by any field value in either ascending or descending order.

For example, to sort results by descending order of a line_id value:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "term": {
  5. "play_name": {
  6. "value": "Henry IV"
  7. }
  8. }
  9. },
  10. "sort": [
  11. {
  12. "line_id": {
  13. "order": "desc"
  14. }
  15. }
  16. ]
  17. }

The sort parameter is an array, so you can specify multiple field values in the order of their priority.

If you have two fields with the same value for line_id, OpenSearch uses speech_number, which is the second option for sorting.

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "term": {
  5. "play_name": {
  6. "value": "Henry IV"
  7. }
  8. }
  9. },
  10. "sort": [
  11. {
  12. "line_id": {
  13. "order": "desc"
  14. }
  15. },
  16. {
  17. "speech_number": {
  18. "order": "desc"
  19. }
  20. }
  21. ]
  22. }

You can continue to sort by any number of field values to get the results in just the right order. It doesn’t have to be a numerical value—you can also sort by date or timestamp fields:

  1. "sort": [
  2. {
  3. "date": {
  4. "order": "desc"
  5. }
  6. }
  7. ]

For numeric fields that contain an array of numbers, you can sort by avg, sum, and median modes:

  1. "sort": [
  2. {
  3. "price": {
  4. "order": "asc",
  5. "mode": "avg"
  6. }
  7. }
  8. ]

To sort by the minimum or maximum values, use the min or max modes. These modes work for both numeric and string data types.

A text field that’s analyzed cannot be used to sort documents, because the inverted index only contains the individual tokenized terms and not the entire string. So, you cannot sort by the play_name, for example.

One workaround is map a raw version of the text field as a keyword type, so it won’t be analyzed and you have a copy of the full original version for sorting purposes.

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "term": {
  5. "play_name": {
  6. "value": "Henry IV"
  7. }
  8. }
  9. },
  10. "sort": [
  11. {
  12. "play_name.keyword": {
  13. "order": "desc"
  14. }
  15. }
  16. ]
  17. }

You get back results sorted by the play_name field in alphabetic order.

Use sort with search_after parameter for more efficient scrolling. You get back results after the values you specify in the search_after array.

Make sure you have the same number of values in the search_after array as in the sort array, also ordered in the same way. In this case, you get back results after line_id = 3202 and speech_number = 8.

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "term": {
  5. "play_name": {
  6. "value": "Henry IV"
  7. }
  8. }
  9. },
  10. "sort": [
  11. {
  12. "line_id": {
  13. "order": "desc"
  14. }
  15. },
  16. {
  17. "speech_number": {
  18. "order": "desc"
  19. }
  20. }
  21. ],
  22. "search_after": [
  23. "3202",
  24. "8"
  25. ]
  26. }

Highlight query matches

Highlighting emphasizes the search term(s) in the results.

To highlight the search terms, add a highlight parameter outside of the query block:

  1. GET shakespeare/_search
  2. {
  3. "query": {
  4. "match": {
  5. "text_entry": "life"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "text_entry": {}
  11. }
  12. }
  13. }

For each document in the results, you get back a highlight object that shows your search term wrapped in an em tag:

  1. "highlight": {
  2. "text_entry": [
  3. "my <em>life</em>, except my <em>life</em>."
  4. ]
  5. }

Design your application code to parse the results from the highlight object and perform some action on the search terms, such as changing their color, bolding, italicizing, and so on.

To change the default em tags, use the pretag and posttag parameters:

  1. GET shakespeare/_search?format=yaml
  2. {
  3. "query": {
  4. "match": {
  5. "play_name": "Henry IV"
  6. }
  7. },
  8. "highlight": {
  9. "pre_tags": [
  10. "<strong>"
  11. ],
  12. "post_tags": [
  13. "</strong>"
  14. ],
  15. "fields": {
  16. "play_name": {}
  17. }
  18. }
  19. }

The highlight parameter highlights the original terms even when using synonyms or stemming for the search itself.