This topic describes the scenarios, data push methods, and search syntax of data of the ARRAY type.
Scenarios
The ARRAY type is a type of data structure that contains a group of elements of the same data type. You can perform independent searches based on each element in an array. For example, a novel contains the following tags: "Mystery", "Time-travel", and "Classical". You can find the novel by searching for "Mystery".
How do I push data of the ARRAY type?
OpenSearch provides multiple methods to push data of the ARRAY type. The following examples describe how to use different methods to push data.
Call API operations
Data of the ARRAY type must be uploaded in the JsonArray format. 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 operations that can be called to upload data, see Traffic API reference > Application-related API operations > Process data in Developer Guide.
Use SDKs
You can refer to the steps of pushing data by using OpenSearch SDK for Java in the Push demo topic. The processes of using the SDKs for other programming languages to push data are similar.
Configure data sources
When you configure a data source, you can parse the source data in various formats. If you have a field defined as an array type, select the MultiValueSplitter plugin for that field and specify a delimiter. For example, if a database field for tags contains "Time-travel,Mystery,Romance", you can set the delimiter to a comma (,). The plugin automatically converts the string from the database field into an array that the search engine can recognize. MultiValueSplitter plugin
How do I retrieve data of the ARRAY type and what effect can be achieved?
You can use query clauses or filter clauses to perform independent searches based on each element of an array. For example, you can use query=tags:'Time-travel' to search for the document that contains the following tags: Time-travel, Mystery, and Romantic. You can also use query=title:'Treading On Thin Ice'&&filter=tags="Time-travel" to search for the novel named Treading On Thin Ice that contains the tag "Time-travel".
Take note of the return values of an ARRAY-type search that are strings instead of arrays. The return values are separated by '\t'.
FAQ
Q1: Why is the TEXT_ARRAY type unavailable and what is the difference between the TEXT and STRING_ARRAY types?
A: The TEXT type includes the TEXT, SHORT_TEXT, NWS_TEXT, and MWS_TEXT types. Fields of the TEXT type involve the analysis process. The TEXT type supports fuzzy match and does not relate to arrays. The STRING_ARRAY type supports exact match of each element. Each element may consist of multiple terms. It is not required that all elements in a field of the STRING_ARRAY type are matched.
Q2: How do I obtain the number of elements in an array?
A: You can use the fieldlen function provided by OpenSearch to obtain the number of elements.
Q3: How do I specify data of the ARRAY type when I use Java code to push data?
You can use variables of the ARRAY type to specify data of the ARRAY type. Examples: iteral_array--->String [] and int_array--->int [].