Array type description

Updated at:

This topic describes the use cases, data push methods, and search syntax of the ARRAY type to help you understand it.

When is the ARRAY type suitable?

The ARRAY type is a data type that groups multiple elements of the same type. Use it when you want each element to be queried separately during a search. For example, the tags field of a novel may contain "mystery", "time travel", and "classical". When you search for "mystery", you want the novel to be found.

How do I push data of the ARRAY type?

OpenSearch supports multiple methods to push data. The following sections describe how to push data by using each method.

API

Data of the ARRAY type must be uploaded as a JsonArray. Example:

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

For more information about the data upload operation, see Developer Guide -> V3 (Standard/Advanced) API Reference -> Application operations -> Data processing

SDK

For more information, see the Java SDK example: Push demo. The methods for other SDKs are similar.

Data source

Data source configuration allows you to parse data from data sources in multiple formats. If you define a field of the ARRAY type, you can select the MultiValueSpliter plug-in for the field and specify a multi-value delimiter. For example, if the tags field in the preceding example contains "time travel,mystery,romance" in the database table, set the multi-value delimiter to a comma (,). The plug-in automatically converts the database field into the ARRAY type that the engine recognizes. MultiValueSpliter plug-in

How do I retrieve data of the ARRAY type, and what results can I achieve?

Each element of the ARRAY type can be accessed separately, whether in a query clause or a filter clause. For example, for the tags field in the preceding example (content: time travel,mystery,romance), you can use query=tags:'time travel' to find the document. You can also use query=title:'Scarlet Heart'&&filter=tags="time travel" to find novels whose tags include "time travel" and whose names contain "Scarlet Heart".

Note that search results return ARRAY type values as strings rather than arrays, and elements are separated by '\t'.

FAQ

Q1: Why is there no text_array type? What is the difference between text and string_array?

A: The text types (including text, short_text, nws_text, and mws_text) involve tokenization and inherently support fuzzy search, so they have no concept of arrays. In contrast, string_array performs exact matching on each element. A single element may consist of multiple words or phrases, but this does not matter because the entire element must be matched.

Q2: Is there a way to obtain the number of elements in an ARRAY type field?

A: The system provides the fieldlen function, which you can use to obtain the number of elements.

Q3: How do I pass array type values when I push data in Java code?

A: Pass them as variables of array types. For example, literal_array--->String [], and int_array--->int [].