All Products
Search
Document Center

ARRAY type

Last Updated: Sep 09, 2021

This topic describes the scenarios, data push, and search syntax 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 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],"string_array": ["abc","xyz"]},"cmd": "ADD"}]

For more information about the operations that can be called to upload data, see Developer Guide > Traffic-related API reference > API operations on applications > Process data.

Use SDKs

The following example describes how to use the SDK for PHP to push data. The processes of using the SDKs for other languages to push data are similar.

<?php
require('php_2.0.4/CloudsearchClient.php');
require('php_2.0.4/CloudsearchIndex.php');
require('php_2.0.4/CloudsearchDoc.php');

define('ACCESSKEYID', 'The AccessKey ID of your Alibaba Cloud account');
define('SECRET', 'The AccessKey secret of your Alibaba Cloud account');
define('APP_NAME', 'Your application name');
define('KEY_TYPE','aliyun'); # Fixed value

# Obtain the API endpoint of each application and set the debug parameter to true for debugging. To obtain the API endpoint, log on the OpenSearch console, click Applications in the left-side navigation pane, find the application that you want to manage on the Applications page, and then click Details in the Actions column. On the Application Details page, you can view the API endpoint information in the API Endpoint section.
$client = new CloudsearchClient(
    ACCESSKEYID,
    SECRET,
    array('host' => 'http://opensearch-cn-hangzhou.aliyuncs.com', 'debug' =>true),   
    KEY_TYPE
);   
$doc = new CloudsearchDoc(APP_NAME, $client);
$json = <<<EOF
[{"fields": { "id": "0","int_array": [14,85],"string_array": ["abc","xyz"]},"cmd": "ADD"}]
EOF;
echo $doc->add($json, 'The name of the table whose data you want to push');
echo $client->getRequest(); # Display the request string, provided that the debug parameter of CloudsearchClient is set to true.
?>

Configure data sources

When you configure a data source, you can parse source data in various formats. If a field of the ARRAY type is defined, you can associate the MultiValueSpliter plug-in with the field and specify a delimiter to separate the values of the field. For example, the tags mentioned in the "Scenarios" section of this topic are parsed in the following format: "Time-travel,Mystery,Romantic". In this case, set the delimiter to a comma (,), as shown in the following figure. The plug-in automatically converts the fields in a database to the values of the ARRAY type so that the values can be recognized by the search engine. For more information, see Data source plug-ins.1

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:'Startling by Each Step'&&filter=tags="Time-travel" to search for the novel named Startling by Each Step that contains the tag "Time-travel". Take note that the return values of an ARRAY-type search 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?

The TEXT type includes 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: OpenSearch provides the fieldlen function for you to obtain the number of elements.