This version of the OpenSearch documentation is no longer maintained. For the latest version, see the current documentation. For information about OpenSearch version maintenance, see Release Schedule and Maintenance Policy.

JSON Support

SQL plugin supports JSON by following PartiQL specification, a SQL-compatible query language that lets you query semi-structured and nested data for any data format. The SQL plugin only supports a subset of the PartiQL specification.

Querying nested collection

PartiQL extends SQL to allow you to query and unnest nested collections. In OpenSearch, this is very useful to query a JSON index with nested objects or fields.

To follow along, use the bulk operation to index some sample data:

  1. POST employees_nested/_bulk?refresh
  2. {"index":{"_id":"1"}}
  3. {"id":3,"name":"Bob Smith","title":null,"projects":[{"name":"SQL Spectrum querying","started_year":1990},{"name":"SQL security","started_year":1999},{"name":"OpenSearch security","started_year":2015}]}
  4. {"index":{"_id":"2"}}
  5. {"id":4,"name":"Susan Smith","title":"Dev Mgr","projects":[]}
  6. {"index":{"_id":"3"}}
  7. {"id":6,"name":"Jane Smith","title":"Software Eng 2","projects":[{"name":"SQL security","started_year":1998},{"name":"Hello security","started_year":2015,"address":[{"city":"Dallas","state":"TX"}]}]}

Example 1: Unnesting a nested collection

This example finds the nested document (projects) with a field value (name) that satisfies the predicate (contains security). Because each parent document can have more than one nested documents, the nested document that matches is flattened. In other words, the final result is the cartesian product between the parent and nested documents.

  1. SELECT e.name AS employeeName,
  2. p.name AS projectName
  3. FROM employees_nested AS e,
  4. e.projects AS p
  5. WHERE p.name LIKE '%security%'

Explain:

  1. {
  2. "from" : 0,
  3. "size" : 200,
  4. "query" : {
  5. "bool" : {
  6. "filter" : [
  7. {
  8. "bool" : {
  9. "must" : [
  10. {
  11. "nested" : {
  12. "query" : {
  13. "wildcard" : {
  14. "projects.name" : {
  15. "wildcard" : "*security*",
  16. "boost" : 1.0
  17. }
  18. }
  19. },
  20. "path" : "projects",
  21. "ignore_unmapped" : false,
  22. "score_mode" : "none",
  23. "boost" : 1.0,
  24. "inner_hits" : {
  25. "ignore_unmapped" : false,
  26. "from" : 0,
  27. "size" : 3,
  28. "version" : false,
  29. "seq_no_primary_term" : false,
  30. "explain" : false,
  31. "track_scores" : false,
  32. "_source" : {
  33. "includes" : [
  34. "projects.name"
  35. ],
  36. "excludes" : [ ]
  37. }
  38. }
  39. }
  40. }
  41. ],
  42. "adjust_pure_negative" : true,
  43. "boost" : 1.0
  44. }
  45. }
  46. ],
  47. "adjust_pure_negative" : true,
  48. "boost" : 1.0
  49. }
  50. },
  51. "_source" : {
  52. "includes" : [
  53. "name"
  54. ],
  55. "excludes" : [ ]
  56. }
  57. }

Result set:

employeeNameprojectName
Bob SmithOpenSearch Security
Bob SmithSQL security
Jane SmithHello security
Jane SmithSQL security

Example 2: Unnesting in existential subquery

To unnest a nested collection in a subquery to check if it satisfies a condition:

  1. SELECT e.name AS employeeName
  2. FROM employees_nested AS e
  3. WHERE EXISTS (
  4. SELECT *
  5. FROM e.projects AS p
  6. WHERE p.name LIKE '%security%'
  7. )

Explain:

  1. {
  2. "from" : 0,
  3. "size" : 200,
  4. "query" : {
  5. "bool" : {
  6. "filter" : [
  7. {
  8. "bool" : {
  9. "must" : [
  10. {
  11. "nested" : {
  12. "query" : {
  13. "bool" : {
  14. "must" : [
  15. {
  16. "bool" : {
  17. "must" : [
  18. {
  19. "bool" : {
  20. "must_not" : [
  21. {
  22. "bool" : {
  23. "must_not" : [
  24. {
  25. "exists" : {
  26. "field" : "projects",
  27. "boost" : 1.0
  28. }
  29. }
  30. ],
  31. "adjust_pure_negative" : true,
  32. "boost" : 1.0
  33. }
  34. }
  35. ],
  36. "adjust_pure_negative" : true,
  37. "boost" : 1.0
  38. }
  39. },
  40. {
  41. "wildcard" : {
  42. "projects.name" : {
  43. "wildcard" : "*security*",
  44. "boost" : 1.0
  45. }
  46. }
  47. }
  48. ],
  49. "adjust_pure_negative" : true,
  50. "boost" : 1.0
  51. }
  52. }
  53. ],
  54. "adjust_pure_negative" : true,
  55. "boost" : 1.0
  56. }
  57. },
  58. "path" : "projects",
  59. "ignore_unmapped" : false,
  60. "score_mode" : "none",
  61. "boost" : 1.0
  62. }
  63. }
  64. ],
  65. "adjust_pure_negative" : true,
  66. "boost" : 1.0
  67. }
  68. }
  69. ],
  70. "adjust_pure_negative" : true,
  71. "boost" : 1.0
  72. }
  73. },
  74. "_source" : {
  75. "includes" : [
  76. "name"
  77. ],
  78. "excludes" : [ ]
  79. }
  80. }

Result set:

employeeName
Bob Smith
Jane Smith