Completion field type

Introduced 1.0

A completion field type provides autocomplete functionality through a completion suggester. The completion suggester is a prefix suggester, so it matches the beginning of text only. A completion suggester creates an in-memory data structure, which provides faster lookups but leads to increased memory usage. You need to upload a list of all possible completions into the index before using this feature.

Example

Create a mapping with a completion field:

  1. PUT chess_store
  2. {
  3. "mappings": {
  4. "properties": {
  5. "suggestions": {
  6. "type": "completion"
  7. },
  8. "product": {
  9. "type": "keyword"
  10. }
  11. }
  12. }
  13. }

copy

Index suggestions into OpenSearch:

  1. PUT chess_store/_doc/1
  2. {
  3. "suggestions": {
  4. "input": ["Books on openings", "Books on endgames"],
  5. "weight" : 10
  6. }
  7. }

copy

Parameters

The following table lists the parameters accepted by completion fields.

ParameterDescription
inputA list of possible completions as a string or array of strings. Cannot contain \u0000 (null), \u001f (information separator one), or \u001e (information separator two). Required.
weightA positive integer or a positive integer string for ranking suggestions. Optional.

Multiple suggestions can be indexed as follows:

  1. PUT chess_store/_doc/2
  2. {
  3. "suggestions": [
  4. {
  5. "input": "Chess set",
  6. "weight": 20
  7. },
  8. {
  9. "input": "Chess pieces",
  10. "weight": 10
  11. },
  12. {
  13. "input": "Chess board",
  14. "weight": 5
  15. }
  16. ]
  17. }

copy

As an alternative, you can use the following shorthand notation (note that you cannot provide the weight parameter in this notation):

  1. PUT chess_store/_doc/3
  2. {
  3. "suggestions" : [ "Chess clock", "Chess timer" ]
  4. }

copy

Querying completion field types

To query completion field types, specify the prefix that you want to search for and the name of the field in which to look for suggestions.

Query the index for suggestions that start with the word “chess”:

  1. GET chess_store/_search
  2. {
  3. "suggest": {
  4. "product-suggestions": {
  5. "prefix": "chess",
  6. "completion": {
  7. "field": "suggestions"
  8. }
  9. }
  10. }
  11. }

copy

The response contains autocomplete suggestions:

  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. "product-suggestions" : [
  20. {
  21. "text" : "chess",
  22. "offset" : 0,
  23. "length" : 5,
  24. "options" : [
  25. {
  26. "text" : "Chess set",
  27. "_index" : "chess_store",
  28. "_type" : "_doc",
  29. "_id" : "2",
  30. "_score" : 20.0,
  31. "_source" : {
  32. "suggestions" : [
  33. {
  34. "input" : "Chess set",
  35. "weight" : 20
  36. },
  37. {
  38. "input" : "Chess pieces",
  39. "weight" : 10
  40. },
  41. {
  42. "input" : "Chess board",
  43. "weight" : 5
  44. }
  45. ]
  46. }
  47. },
  48. {
  49. "text" : "Chess clock",
  50. "_index" : "chess_store",
  51. "_type" : "_doc",
  52. "_id" : "3",
  53. "_score" : 1.0,
  54. "_source" : {
  55. "suggestions" : [
  56. "Chess clock",
  57. "Chess timer"
  58. ]
  59. }
  60. }
  61. ]
  62. }
  63. ]
  64. }
  65. }

In the response, the _score field contains the value of the weight parameter that was set up at index time. The text field is populated with the suggestion’s input parameter.

By default, the response contains the whole document, including the _source field, which may impact performance. To return only the suggestions field, you can specify that in the _source parameter. You can also restrict the number of returned suggestions by specifying the size parameter.

  1. GET chess_store/_search
  2. {
  3. "_source": "suggestions",
  4. "suggest": {
  5. "product-suggestions": {
  6. "prefix": "chess",
  7. "completion": {
  8. "field": "suggestions",
  9. "size" : 3
  10. }
  11. }
  12. }
  13. }

copy

The response contains the suggestions:

  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" : 0,
  13. "relation" : "eq"
  14. },
  15. "max_score" : null,
  16. "hits" : [ ]
  17. },
  18. "suggest" : {
  19. "product-suggestions" : [
  20. {
  21. "text" : "chess",
  22. "offset" : 0,
  23. "length" : 5,
  24. "options" : [
  25. {
  26. "text" : "Chess set",
  27. "_index" : "chess_store",
  28. "_type" : "_doc",
  29. "_id" : "2",
  30. "_score" : 20.0,
  31. "_source" : {
  32. "suggestions" : [
  33. {
  34. "input" : "Chess set",
  35. "weight" : 20
  36. },
  37. {
  38. "input" : "Chess pieces",
  39. "weight" : 10
  40. },
  41. {
  42. "input" : "Chess board",
  43. "weight" : 5
  44. }
  45. ]
  46. }
  47. },
  48. {
  49. "text" : "Chess clock",
  50. "_index" : "chess_store",
  51. "_type" : "_doc",
  52. "_id" : "3",
  53. "_score" : 1.0,
  54. "_source" : {
  55. "suggestions" : [
  56. "Chess clock",
  57. "Chess timer"
  58. ]
  59. }
  60. }
  61. ]
  62. }
  63. ]
  64. }
  65. }

To take advantage of source filtering, use the suggest functionality on the _search endpoint. The _suggest endpoint does not support source filtering.

Completion query parameters

The following table lists the parameters accepted by the completion suggester query.

ParameterDescription
fieldA string that specifies the field on which to run the query. Required.
sizeAn integer that specifies the maximum number of returned suggestions. Optional. Default is 5.
skip_duplicatesA Boolean value that specifies whether to skip duplicate suggestions. Optional. Default is false.

Fuzzy completion query

To allow for fuzzy matching, you can specify the fuzziness parameter for the completion query. In this case, even if the user mistypes a search term, the completion query still returns results. Additionally, the longer the prefix that matches the query, the higher the document’s score.

  1. GET chess_store/_search
  2. {
  3. "suggest": {
  4. "product-suggestions": {
  5. "prefix": "chesc",
  6. "completion": {
  7. "field": "suggestions",
  8. "size" : 3,
  9. "fuzzy" : {
  10. "fuzziness" : "AUTO"
  11. }
  12. }
  13. }
  14. }
  15. }

copy

To use all default fuzziness options, specify "fuzzy": {} or "fuzzy": true.

The following table lists the parameters accepted by the fuzzy completion suggester query. All of the parameters are optional.

ParameterDescription
fuzzinessFuzziness can be set as one of the following:
1. An integer that specifies the maximum allowed Damerau–Levenshtein distance for this edit.
2. AUTO: Strings of 0–2 characters must match exactly, strings of 3–5 characters allow 1 edit, and strings longer than 5 characters allow 2 edits.
Default is AUTO.
min_lengthAn integer that specifies the minimum length the input must be to start returning suggestions. If the search term is shorter than min_length, no suggestions are returned. Default is 3.
prefix_lengthAn integer that specifies the minimum length the matched prefix must be to start returning suggestions. If the prefix of prefix_length is not matched, but the search term is still within the Damerau–Levenshtein distance, no suggestions are returned. Default is 1.
transpositionsA Boolean value that specifies to count transpositions (interchanges of adjacent characters) as one edit instead of two. Example: The suggestion’s input parameter is abcde and the fuzziness is 1. If transpositions is set to true, abdce will match, but if transpositions is set to false, abdce will not match. Default is true.
unicode_awareA Boolean value that specifies whether to use Unicode code points when measuring the edit distance, transposition, and length. If unicode_aware is set to true, the measurement is slower. Default is false, in which case distances are measured in bytes.

Regex queries

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

For example, to search for strings that start with “a” and have a “d” later on, use the following query:

  1. GET chess_store/_search
  2. {
  3. "suggest": {
  4. "product-suggestions": {
  5. "regex": "a.*d",
  6. "completion": {
  7. "field": "suggestions"
  8. }
  9. }
  10. }
  11. }

copy

The response matches the string “abcde”:

  1. {
  2. "took" : 2,
  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. "product-suggestions" : [
  20. {
  21. "text" : "a.*d",
  22. "offset" : 0,
  23. "length" : 4,
  24. "options" : [
  25. {
  26. "text" : "abcde",
  27. "_index" : "chess_store",
  28. "_type" : "_doc",
  29. "_id" : "2",
  30. "_score" : 20.0,
  31. "_source" : {
  32. "suggestions" : [
  33. {
  34. "input" : "abcde",
  35. "weight" : 20
  36. }
  37. ]
  38. }
  39. }
  40. ]
  41. }
  42. ]
  43. }
  44. }