Perform searches using the RESTful API

更新时间:
复制 MD 格式

Searches an index and returns matching documents based on the query conditions you specify.

Endpoint

POST /{indexName}/search
The endpoint shown above omits the host address and request headers. For the full host address, see Network information.
AttributeValue
ProtocolHTTP
MethodPOST
FormatJSON

Authentication

The authorization header uses HTTP Basic authentication. Encode your credentials as Base64(accessUserName:accessPassWord) and prefix the result with Basic.

Credentials

ParameterTypeDescription
accessUserNameStringThe username. Find it on the Instance Details > Network Information page.
accessPassWordStringThe password. Modify it on the Instance Details > Network Information page.

Generate the authorization value

The following Java example generates a Base64-encoded authorization value from your credentials:

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);
    }
}

The output is a Base64-encoded string:

cm9vdDp******mdhbA==

Set the authorization request header with the Basic prefix:

authorization: Basic cm9vdDp******mdhbA==

Request body parameters

The request body contains a query field that accepts a search string composed of one or more clauses.

ParameterTypeRequiredDescription
queryStringYesThe search string. Cannot be empty. Supports the query clause, config clause, sort clause, filter clause, aggs clause, distinct clause, and kvpairs clause.

Supported clauses

ClauseRequiredDescription
query clauseYesSets the search conditions.
config clauseNoSets the response format and the number of documents to return.
filter clauseNoSets filter conditions to narrow search results.
sort clauseNoSets the sort order for returned documents.
aggregate clauseNoSets aggregation statistics.
distinct clauseNoDeduplicates results by extracting documents for each user, ensuring results from each user are represented.
kvpairs clauseNoDefines custom parameters for the variable parts of sort expressions.

Request body example

{
  "query": "index_id: 0",
  "config": {
    "format": "json"
  }
}

Response parameters

ParameterTypeDescription
resultJSONThe search results.
errorsStringThe error message.

result object fields

FieldDescription
searchtimeThe time, in seconds, that the engine took to process the request.
totalHitsThe estimated total number of documents matching the query. This is an estimate when the result set is large. Does not account for the config clause.
itemsThe retrieved documents. Each item contains a fields object with the document content.
sortExprValuesThe sort expression score for each document.
variableValueThe result of a custom parameter, such as a calculated distance. Returned only when the format parameter in the config clause is set to xml or fulljson. Not returned in json format by default.
facetAggregation results returned by the aggregate clause.
In json and fulljson formats, values in array fields are separated by \t. In xml format, values are separated by spaces.

Examples

curl request

curl --location --request POST 'http://ha-cn-*********.public.ha.aliyuncs.com/index_hdfs/search' \
--header 'authorization: Basic *******************' \
--header 'host: ha-cn-*********.public.ha.aliyuncs.com' \
--header 'Content-Type: application/json' \
--data-raw '{
  "query": "index_id: 1",
  "config": {
    "format": "json"
  }
}'
The endpoint in this example uses the public network domain. For endpoint options, see Network information.

Successful response

{
    "result": {
        "searchtime": 0.010385,
        "numHits": 1,
        "totalHits": 1,
        "coveredPercent": 100.0,
        "items": [
            {
                "fields": {
                    "id": "1",
                    "name": "aliyun",
                    "age": "20"
                },
                "properties": {},
                "attributes": {},
                "variableValues": {},
                "sortExprValues": [
                    "10000"
                ]
            }
        ],
        "facet": []
    },
    "errors": []
}

Error response

{
    "result": {
        "searchtime": 0.000094,
        "numHits": 0,
        "totalHits": 0,
        "coveredPercent": 0.0,
        "facet": []
    },
    "errors": [
        {
            "code": 1013,
            "message": "QueryClause: index not exist. Index name:index_id"
        }
    ]
}