ARRAY type

Updated at:

This topic describes ARRAY use cases, data push methods, and search syntax.

When should I use an ARRAY type?

An ARRAY is a collection of elements of the same type. Each element can be queried independently. For example, a novel has tags "mystery", "time travel", and "classical". Searching for "mystery" should retrieve the novel.

How do I push ARRAY data?

OpenSearch supports multiple data push methods. The following sections describe each method.

Call API operations

Upload ARRAY data as a JsonArray. Example:

[
    {
        "fields": {
            "id": "0",
            "int_array": [
                14,
                85
            ],
            "float_array": [
                14.0,
                85.0
            ],
            "literal_array": [
                "abc",
                "xyz"
            ]
        },
        "cmd": "ADD"
    }
]

For the data upload API, see:Data processing

Use SDKs

For a Java SDK example, see Push demo. Other SDKs work similarly.

Configure data sources

Data source configuration supports parsing source data in multiple formats. For an ARRAY field, select the MultiValueSpliter plug-in and define a multi-value delimiter. For example, if the database tags field contains "time travel,mystery,romance", set the delimiter to a comma (,). The plug-in converts the source field to an ARRAY value understood by the engine. For MultiValueSpliter, see Data source plugin overview)

How do I query ARRAY fields, and what results can I obtain?

Each ARRAY element can be accessed independently in a query clause or filter clause. For the tags field above ("time travel,mystery,romance"), use query=tags:'time travel' to retrieve the document. You can also use query=title:'Scarlet Heart'&&filter=tags="time travel" to retrieve novels tagged "time travel" whose titles contain "Scarlet Heart".

Note that ARRAY values are returned as strings with elements separated by \t, rather than as arrays.

FAQ

Q1: Why is TEXT_ARRAY unavailable, and how does TEXT differ from STRING_ARRAY?

A: TEXT types, including text, short_text, nws_text, and mws_text, use tokenization and support fuzzy search, so there is no array concept. STRING_ARRAY instead matches each element exactly. An element may contain multiple words, but its entire value must match.

Q2: Can I get the number of elements in an ARRAY?

A: Use the fieldlen function (see filter clause) to obtain the element count.

Q3: How should I pass ARRAY values when pushing data with Java?

A: Use an array variable. For example, literal_array maps to String[] and int_array maps to int[].