Completion field type

    Create a mapping with a completion field:

    copy

    Index suggestions into OpenSearch:

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

    copy

    Parameters

    The following table lists the parameters accepted by completion fields.

    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):

    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. "input" : "Chess pieces",
    38. "weight" : 10
    39. },
    40. {
    41. "input" : "Chess board",
    42. "weight" : 5
    43. }
    44. ]
    45. }
    46. },
    47. {
    48. "_index" : "chess_store",
    49. "_type" : "_doc",
    50. "_id" : "3",
    51. "_score" : 1.0,
    52. "_source" : {
    53. "suggestions" : [
    54. "Chess clock",
    55. "Chess timer"
    56. ]
    57. }
    58. }
    59. ]
    60. }
    61. ]
    62. }
    63. }

    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.

    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. "text" : "Chess clock",
    49. "_index" : "chess_store",
    50. "_type" : "_doc",
    51. "_id" : "3",
    52. "_score" : 1.0,
    53. "_source" : {
    54. "suggestions" : [
    55. "Chess clock",
    56. "Chess timer"
    57. ]
    58. }
    59. }
    60. ]
    61. }
    62. ]
    63. }

    Completion query parameters

    The following table lists the parameters accepted by the completion suggester 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.

    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:

    copy

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