OpenSearch provides several ways to run a script; the following sections show how to run a script by passing script information in the request body of a request.

Example request

The following request runs the stored script that was created in Create or update stored script. The script sums the ratings for each book and displays the sum in the total_ratings field in the output.

  • The script’s target is the books index.

  • The "match_all": {} property value is an empty object indicating to process each document in the index.

copy

Example response

The GET books/_search request returns the following fields:

  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" : 3,
  13. "relation" : "eq"
  14. },
  15. "max_score" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "books",
  19. "_id" : "1",
  20. "_score" : 1.0,
  21. "fields" : {
  22. "total_ratings" : [
  23. 12
  24. ]
  25. }
  26. },
  27. {
  28. "_index" : "books",
  29. "_id" : "2",
  30. "_score" : 1.0,
  31. "fields" : {
  32. "total_ratings" : [
  33. 15
  34. ]
  35. }
  36. },
  37. {
  38. "_index" : "books",
  39. "_id" : "3",
  40. "_score" : 1.0,
  41. "fields" : {
  42. "total_ratings" : [
  43. 8
  44. ]
  45. }
  46. ]
  47. }
  48. }

Response fields

Hits object

Document object

To pass different parameters to the script each time when running a query, define params in script_fields.

The following request runs the stored script that was created in . The script sums the ratings for each book, multiplies the summed value by the parameter, and displays the result in the output.

  • The "match_all": {} property value is an empty object, indicating that it processes each document in the index.

  • The total_ratings field value is the result of the multiplier-script execution. See Creating or updating a stored script with parameters.

  • "multiplier": 2 in the params field is a variable passed to the stored script multiplier-script:

copy

Example response

  1. {
  2. "took" : 12,
  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" : 1.0,
  16. "hits" : [
  17. {
  18. "_index" : "books",
  19. "_type" : "_doc",
  20. "_id" : "3",
  21. "_score" : 1.0,
  22. "fields" : {
  23. "total_ratings" : [
  24. 16
  25. ]
  26. }
  27. },
  28. {
  29. "_index" : "books",
  30. "_type" : "_doc",
  31. "_id" : "2",
  32. "_score" : 1.0,
  33. "fields" : {
  34. "total_ratings" : [
  35. 30
  36. ]
  37. }
  38. },
  39. {
  40. "_index" : "books",
  41. "_type" : "_doc",
  42. "_id" : "1",
  43. "_score" : 1.0,
  44. "fields" : {
  45. "total_ratings" : [
  46. 24
  47. ]
  48. }
  49. }
  50. ]
  51. }

Sample response

  1. "took" : 90,
  2. "timed_out" : false,
  3. "_shards" : {
  4. "total" : 5,
  5. "successful" : 5,
  6. "skipped" : 0,
  7. "failed" : 0
  8. },
  9. "hits" : {
  10. "total" : {
  11. "value" : 3,
  12. "relation" : "eq"
  13. },
  14. "max_score" : null,
  15. "hits" : [
  16. {
  17. "_index" : "books",
  18. "_type" : "_doc",
  19. "_id" : "2",
  20. "_score" : null,
  21. "fields" : {
  22. "total_ratings" : [
  23. 30
  24. ]
  25. },
  26. "sort" : [
  27. 30.0
  28. ]
  29. },
  30. {
  31. "_index" : "books",
  32. "_type" : "_doc",
  33. "_id" : "1",
  34. "_score" : null,
  35. "fields" : {
  36. "total_ratings" : [
  37. 24
  38. ]
  39. },
  40. "sort" : [
  41. 24.0
  42. ]
  43. },
  44. {
  45. "_index" : "books",
  46. "_type" : "_doc",
  47. "_id" : "3",
  48. "_score" : null,
  49. "fields" : {
  50. "total_ratings" : [
  51. 16
  52. ]
  53. },
  54. "sort" : [
  55. 16.0
  56. ]
  57. }
  58. ]