OpenSearch provides diverse search syntax to meet your search requirements in a variety of scenarios.
URL
sql?query=SELECT brand, COUNT(*) FROM phone GROUP BY (brand)&&kvpair=trace:INFO;formatType:json
The sample URL omits information such as the request headers and the encoding method.
The sample URL also omits the endpoint that is used to connect to an OpenSearch application.
For more information about the definitions, usage, and example values of all the request parameters that are concatenated in the preceding URL, see the "Request parameters" section of this topic.
Protocol
HTTP
Request method
GET
Supported format
JSON
Request signature
You can calculate the request signature by using the parameters described in the following table. The request signature is stored in the authorization header.
Parameter | Type | Description |
accessUserName | String | The username. You can view the username in the API Endpoint section of the Instance Details page. |
accessPassWord | String | The password. You can modify the password in the API Endpoint section of the Instance Details page. |
import com.aliyun.darabonba.encode.Encoder;
import com.aliyun.darabonbastring.Client;
public class GenerateAuthorization {
public static void main(String[] args) throws Exception {
String accessUserName = "username";
String accessPassWord = "password";
String realmStr = "" + accessUserName + ":" + accessPassWord + "";
String authorization = Encoder.base64EncodeToString(Client.toBytes(realmStr, "UTF-8"));
System.out.println(authorization);
}
}
Valid format for the value of the authorization header:
cm9vdDp******mdhbA==
Note: You must add the Basic prefix when you specify the authorization header in an HTTP request.
Example:
authorization: Basic cm9vdDp******mdhbA==
Request parameters
Parameter | Type | Required | Description |
query clause | String | Yes | For more information, see query clause. |
kvpairs clause | String | No | For more information, see kvpair clause. |
Usage of request parameters
query: You can use query clauses to spell complete SQL statements.
Response parameters
Parameter | Type | Description |
total_time | String | The query ID. This parameter is used for troubleshooting. |
covered_percent | The coverage rate. | |
row_count | int |
Sample cURL request
curl --location --request GET 'http://ha-cn-*******.public.ha.aliyuncs.com/query?sql=query=select * from index_hdf%26%26kvpair=trace:INFO;format:full_json' \
--header 'authorization: Basic ******************' \
--header 'host: ha-cn-*******.public.ha.aliyuncs.com'
In this example, a public endpoint is used. For more information, see the "Network Information tab" section of the Instance details topic.
Sample search responses
Sample success response
{
"total_time": 4.557,
"covered_percent": 1.0,
"row_count": 1,
"format_type": "full_json",
"search_info": {},
"trace": [],
"sql_result": {
"data": [
[
1
]
],
"column_name": [
"id"
],
"column_type": [
"int64"
]
},
"error_info": {
"ErrorCode": 0,
"Error": "ERROR_NONE",
"Message": ""
}
}
Sample error response
{
"total_time": 15.96,
"covered_percent": 0.0,
"row_count": 0,
"format_type": "full_json",
"search_info": {},
"trace": [],
"sql_result": {
"data": [],
"column_name": [],
"column_type": []
},
"error_info": {
"ErrorCode": 8010,
"Error": "call sql plan service failed.",
"Message": "failed to get sql plan, error message is [IQUAN_EC_INTERNAL_ERROR] internal error : SQL validation failed. From line 1, column 15 to line 1, column 23: Object 'index_hdf' not found\nFrom line 1, column 15 to line 1, column 23: Object 'index_hdf' not found\norg.apache.flink.table.api.ValidationException: SQL validation failed. From line 1, column 15 to line 1, column 23: Object 'index_hdf' not found\n\tat org.apache.flink.table.planner.calcite.FlinkPlannerImpl.org$apache$flink$table$planner$calcite$FlinkPlannerImpl$$validate(FlinkPlannerImpl.scala:149)\n\tat org.apache.flink.table.planner.calcite.FlinkPlannerImpl.validate(FlinkPlannerImpl.scala:108)\n\tat org.apache.flink.table.planner.delegation.ParserImpl.validate(ParserImpl.java:91)\n\tat com.taobao.search.iquan.core.api.impl.SqlQueryableImpl$SqlWorkFlow.validateSql(SqlQueryableImpl.java:646)\n\tat com.taobao.search.iquan.core.api.impl.SqlQueryableImpl$SqlWorkFlow.process(SqlQueryableImpl.java:392)\n\tat com.taobao.search.iquan.core.api.impl.SqlQueryableImpl.select(SqlQueryableImpl.java:134)\n\tat com.taobao.search.iquan.client.common.service.SqlQueryService.sqlQuery(SqlQueryService.java:89)\n\tat com.taobao.search.iquan.client.IquanClient.sqlQuery(IquanClient.java:79)\nCaused by: org.apache.calcite.runtime.CalciteContextException: From line 1, column 15 to line 1, column 23: Object 'index_hdf' not found\n\tat sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)\n\tat sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\n\tat sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\n\tat java.lang.reflect.Constructor.newInstance(Constructor.java:422)\n\tat org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:467)\n\tat org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:883)\n\tat org.apache.calcite.sql.SqlUtil.newContextException(SqlUtil.java:868)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.newValidationError(SqlValidatorImpl.java:5032)\n\tat org.apache.calcite.sql.validate.IdentifierNamespace.resolveImpl(IdentifierNamespace.java:179)\n\tat org.apache.calcite.sql.validate.IdentifierNamespace.validateImpl(IdentifierNamespace.java:184)\n\tat org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:84)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateNamespace(SqlValidatorImpl.java:1067)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateQuery(SqlValidatorImpl.java:1041)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateFrom(SqlValidatorImpl.java:3202)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateFrom(SqlValidatorImpl.java:3187)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelect(SqlValidatorImpl.java:3450)\n\tat org.apache.calcite.sql.validate.SelectNamespace.validateImpl(SelectNamespace.java:60)\n\tat org.apache.calcite.sql.validate.AbstractNamespace.validate(AbstractNamespace.java:84)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateNamespace(SqlValidatorImpl.java:1067)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateQuery(SqlValidatorImpl.java:1041)\n\tat org.apache.calcite.sql.SqlSelect.validate(SqlSelect.java:232)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validateScopedExpression(SqlValidatorImpl.java:1016)\n\tat org.apache.calcite.sql.validate.SqlValidatorImpl.validate(SqlValidatorImpl.java:724)\n\tat org.apache.flink.table.planner.calcite.FlinkPlannerImpl.org$apache$flink$table$planner$calcite$FlinkPlannerImpl$$validate(FlinkPlannerImpl.scala:144)\n\t... 7 more\nCaused by: org.apache.calcite.sql.validate.SqlValidatorException: Object 'index_hdf' not found\n\tat sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)\n\tat sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)\n\tat sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)\n\tat java.lang.reflect.Constructor.newInstance(Constructor.java:422)\n\tat org.apache.calcite.runtime.Resources$ExInstWithCause.ex(Resources.java:467)\n\tat org.apache.calcite.runtime.Resources$ExInst.ex(Resources.java:560)\n\t... 26 more\n"
}
}
Usage notes
If a query exceeds 30 KB in length, call a RESTful API to query data. For more information, see Initiate search requests by using the RESTful API.