Nested query
The OBJECT type is used to store nested structured data (such as user information or order details) and supports combining multiple fields into a nested structure. After independent storage (is_nested) is configured for an OBJECT field, the query conditions must all be satisfied within the same nested object, which prevents incorrect matches across objects. With Nested queries, you can precisely retrieve data in nested structures. This applies to scenarios that require exact matching and filtering on complex objects.
Limits
An OBJECT type field cannot be set as the primary key.
An OBJECT type field does not support multiple values.
An OBJECT type field cannot be used as a vector field.
An OBJECT type field does not support data source or data processing configuration.
Vector type fields cannot be nested within an OBJECT type.
Configure an OBJECT field
In the Field Configuration step of adding or modifying a table, you can add an OBJECT type field and configure its schema.
Add an OBJECT field
On the field configuration page, click +Import field or manually add a field row.
In the drop-down list of the Field Type column, select OBJECT.
After you select the OBJECT type, the Primary Key and Multi-value options cannot be selected for this field, and the Vector Field and Data Source options are dimmed.
Configure the schema
In the OBJECT field row of the field list, click Configure schema. The schema configuration drawer appears on the right side of the page.
Enter the schema configuration in JSON format in the input box. The schema configuration must be valid JSON and cannot be empty.
Click OK to complete the schema configuration.
The schema defines the internal structure of an OBJECT field, including the types and index methods of its subfields. The following is a schema example that contains a nested structure:
{
"fields": {
"age": {
"field_type": "INTEGER",
"index_type": "NUMBER"
},
"name": {
"field_type": "STRING",
"index_type": "STRING"
},
"tags": {
"fields": {
"k1": {
"field_type": "STRING",
"index_type": "STRING"
},
"k2": {
"field_type": "STRING",
"index_type": "STRING"
}
},
"is_nested": true
}
},
"is_nested": true
}The parameters in the schema are described as follows:
Parameter | Required | Description |
field_type | Yes | The basic type of the field. This parameter cannot be empty. Types such as STRING, INTEGER, and FLOAT are supported. |
index_type | No | The index type. The pack type is not supported. |
analyzer | No | The analyzer. |
multi_value | No | Specifies whether the field is a multi-value field. The default value is false. For a non-Nested field, this value is ignored and is uniformly set to true. |
is_nested | No | Specifies whether to enable independent storage. The default value is false. If this parameter is set to true, the query conditions must all be satisfied within the same nested object (Nested semantics), which prevents incorrect matches across objects. |
Source data
Full data
JSON format
{ "className": "c1", "students": [{ "age": 6, "name": "s1", "tags": [{ "k1": "v1", "k2": "v2" }, { "k1": "v11", "k2": "v22" }] }] }HA3 format
CMD=add^_ className=c1^_ students=[{"age":6,"name":"s1","tags":[{"k1":"v1","k2":"v2"},{"k1":"v11","k2":"v22"}]}]^_ ^^Real-time data
[ { "cmd": "add", "fields": { "className": "c1", "students": "[{\"age\":6,\"name\":\"s1\",\"tags\":[{\"k1\":\"v1\",\"k2\":\"v2\"},{\"k1\":\"v11\",\"k2\":\"v22\"}]}]" } } ]
Push data
The value of an OBJECT field is in JSON format. When you push real-time data by using an API, the value of an OBJECT field must be passed as a JSON string.
Example of pushing data
Assume that you have a class table that contains a regular field className and an OBJECT field students. The students field contains the age, name, and tags subfields. The following example shows how to push one record:
[
{
"cmd": "add",
"fields": {
"className": "c1",
"students": "[{\"age\":6,\"name\":\"s1\",\"tags\":[{\"k1\":\"v1\",\"k2\":\"v2\"},{\"k1\":\"v11\",\"k2\":\"v22\"}]}]"
}
}
]When pushing in real time, the value of an OBJECT field must be escaped into a JSON string. For example, in the preceding example, the value of the students field is the string form of a JSON array.
The preceding example is simplified and contains only a regular field and an OBJECT field. In actual pushes, you must include all required fields defined in the table (such as vector fields).
Nested query
After you configure an OBJECT field and push data, you can reference the subfields inside the OBJECT in queries. The reference path of a subfield uses underscores (_) as connectors, starting from the OBJECT field name and concatenating the subfield names of each level in order.
In OpenSearch Vector Search Edition, the query parameter is used for conditional filtering in vector retrieval requests and must be used together with other parameters of a vector query (such as vector and topK). This topic only shows how to reference OBJECT subfields in the query parameter.
Query syntax
The reference format for a field inside an OBJECT is: OBJECT field name_subfield name. For multiple levels of nesting, concatenate them with underscores in order: OBJECT field name_subfield 1_subfield 2.
For example, for the OBJECT field students, the query path of its subfield age is students_age, and the query path of the nested subfield tags.k1 is students_tags_k1.
Nested query behavior
When is_nested is set to true in the schema, the query conditions must all be satisfied within the same nested object. This means:
If an OBJECT field contains multiple objects (an array), the match for each query condition must come from the same array element.
Field values from different array elements are not cross-matched.
Query examples
Assume that the following data has been pushed, and the tags subfield of the OBJECT field students is configured with is_nested: true:
{
"className": "c1",
"students": [
{
"age": 6,
"name": "s1",
"tags": [
{"k1": "v1", "k2": "v2"},
{"k1": "v11", "k2": "v22"}
]
}
]
}Example 1: cross-object query (no results)
Query conditions: className is c1, students -> tags -> k1 is v1, and students -> tags -> k2 is v22.
query=className:c1 AND students_tags_k1:v1 AND students_tags_k2:v22Query result: no results. This is because k1=v1 and k2=v22 belong to different objects in the tags array (k1 of the first object is v1, and k2 of the second object is v22). Because Nested independent storage is enabled, the conditions are not within the same nested object and therefore do not match.
Example 2: same-object query (results returned)
Query conditions: className is c1, students -> tags -> k1 is v1, and students -> tags -> k2 is v2.
query=className:c1 AND students_tags_k1:v1 AND students_tags_k2:v2Query result: matching data is returned. This is because k1=v1 and k2=v2 belong to the same object in the tags array (the first object), so the conditions are satisfied simultaneously within the same nested object.
{
"className": "c1",
"students": "[{\"age\":6,\"name\":\"s1\",\"tags\":[{\"k1\":\"v1\",\"k2\":\"v2\"},{\"k1\":\"v11\",\"k2\":\"v22\"}]}]"
}Comparison with the non-independent storage mode
When is_nested is set to false (the default value) in the schema, the subfields of the OBJECT field use flattened storage. In this case, queries do not require the conditions to match within the same nested object, and field values in different objects may be cross-matched.
You can view the schema configuration of an OBJECT field and check whether it contains "is_nested": true to determine the current storage mode. If the schema does not contain this parameter or its value is false, flattened storage is used.
Item | Independent storage (is_nested: true) | Flattened storage (is_nested: false) |
Matching method | The conditions must all be satisfied within the same nested object | The conditions can be matched across objects |
Applicable scenarios | Scenarios that require exact matching of relationships inside a nested object, such as querying "k1 and k2 of the same tag" | Scenarios in which you only need to determine whether a field value exists and do not care which nested object the value belongs to |
Query example result |
|
|