Hybrid text and vector query

Updated at:

Query syntax

URL

  • curl -X POST /search or curl -X POST /vector-service/search

  • The preceding URL omits factors such as request header parameters and encoding.

  • The preceding URL omits the host address used to access the application.

  • For all query parameters appended to the preceding URL, see the definitions, usage, and examples in the "Query parameters" section below.

Request protocol

HTTP

Request method

POST

Supported format

JSON

Signature mechanism

You can use the following method to calculate the signature (authorization).

Parameter

Type

Description

accessUserName

string

The username. You can view it in the Network Information section of the instance details page.

accessPassWord

string

The password. You can change it in the Network Information 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);
    }
}

Correct format of the returned authorization value:

cm9vdDp******mdhbA==

When you set the authorization parameter in an HTTP request, add the Basic prefix.

Example (added to the header):

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

Query parameters

Parameter

Type

Required

Default value

Description

tableName

string

Yes

None

The name of the table to query.

knn

Object

No

None

The knn query parameters.

knn.vector

list[float]

Yes

None

The dense vector data to query.

knn.topk

int

No

None

The number of results to return.

knn.filter

String

No

""

The filter condition expression.

knn.weight

Float

No

1.0

The weight of the knn query results. The value of score * weight is used as the ranking score of this path.

text

Object

No

None

The text query parameters.

text.queryString

String

Yes

None

Ha3 query syntax. Nested AND and OR operations across multiple text indexes are supported.

text.queryParams

Map<String, String>

No

{}

The query parameters:

  • default_op: specifies the default operator used to connect the terms after the query is tokenized in this query, AND or OR. Default value: AND.

  • no_token_indexes: specifies indexes whose queries are not tokenized. Processes other than tokenization, such as normalization and stop word removal, are still performed. Separate multiple indexes with ;.

  • remove_stopwords: true or false, which specifies whether to remove stop words. Stop words are configured in the analyzer. Default value: true.

text.filter

String

No

""

The filter condition expression.

text.weight

Float

No

1.0

The weight of the text query results. The value of score * weight is used as the ranking score of this path.

text.terminateAfter

Integer

No

0

The maximum number of documents that meet the conditions to search for in each shard. After this number is reached, the query ends early and the index is no longer queried. Default value: 0, which indicates no limit.

size

Integer

No

100

The number of results to return.

from

Integer

No

0

Returns documents starting from the position specified by from in the result set.

outputFields

List[String]

No

[]

The fields to return in the results.

order

String

No

DESC

The sort order of the results:

  • DESC: descending order

  • ASC: ascending order

rank

Object

No

{}

The method used to fuse the results of the two paths. The following two strategies are supported:

  • Default strategy: The scores of documents with the same primary key in the results of the two paths are added based on their weights. The results are sorted by the weighted scores.

  • rrf: uses RRF to fuse the results of the two paths.

Usage notes

  • knn supports only a single-vector query. The parameters are the same as those of the query API.

  • Distance score conversion

    To fuse the knn and text scores, the knn distance scores are converted.

    # Euclidean distance
    score = 1.0 / (1.0 + l2_distance^2)
    
    # Inner product distance
    score = (1.0 + ip_distance) / 2.0
  • The order parameter in the knn query does not take effect. The outer order parameter is used instead.

  • Default sorting

    • Configuration method: Do not configure rank, or set rank to an empty object {}.

      {
        "rank": {}
      }
    • The scores of documents with the same primary key in the results of the two paths are added based on their weights. The results are sorted by the weighted scores.

      score(i) = knn_score(i) * knn_weight + text_score(i) * text_weight

  • RRF

    • Configuration method:

      {
        "rank": {
          "rrf": {
            "rankConstant": 60
          }
        }
      }
      
      # rankConstant is optional. Default value: 60.
    • Formula

      score = 0.0
      if d in result(q):
          score += 1.0 / (rankConstant + rank(result(q), d))
      return score
      
      # rankConstant is the ranking constant, which determines how much the documents in a single result set influence the final result. A larger value gives lower-ranked documents more influence. You can set it to an integer greater than or equal to 1. Default value: 60.
      # rank(result(q), d) is the position of the document in the results of a query path, starting from 1.

Query example

{
    "tableName": "test",
    "text": {
        "queryString": "title:'Alibaba'",
        "queryParams": {
            "default_op": "OR"
        },
        "filter": "count > 0",
        "terminateAfter": 100000
    },
    "knn": {
        "vector": [0.1, 0.2, 0.3, 0.4, 0.5],
        "namespace": "1",
        "topK": 100
    },
    "order": "DESC",
    "size": 10,
    "rank": {
        "rrf": {
            "rankConstant": 1
        }
    },
    "outputFields": ["title"]
}

Response

{
  "totalTime": 8.522,
  "coveredPercent": 1.0,
  "totalCount": 5,
  "result": [
    {
      "__source__": 2,
      "score": 0.833333,
      "namespace": 1,
      "id": 2,
      "fields": {
        "title": "a b c"
      }
    },
    {
      "__source__": 3,
      "score": 0.666666,
      "namespace": 1,
      "id": 1,
      "fields": {
        "title": "a b"
      }
    },
    {
      "__source__": 3,
      "score": 0.333333,
      "namespace": 2,
      "id": 5,
      "fields": {
        "title": "c"
      }
    },
    {
      "__source__": 2,
      "score": 0.25,
      "namespace": 2,
      "id": 4,
      "fields": {
        "title": "b c"
      }
    },
    {
      "__source__": 2,
      "score": 0.2,
      "namespace": 1,
      "id": 0,
      "fields": {
        "title": "a"
      }
    }
  ]
}

result parameters:

Field

Type

Description

id

The type configured in the schema

The primary key value.

namespace

The type configured in the schema

The namespace of the vector index queried by knn. This field is returned only if a namespace is configured.

vector

List[Float]

The vector values of the vector index queried by knn. You must set "includeVector": true in knn.

score

Float

The final ranking score.

fields

Map<String, FieldType>

The content of the returned fields, in a kvmap structure.

totalTime

Integer

The time consumed by the engine, in ms.

totalCount

Integer

The number of returned results.

__source__

Integer

The query from which the data was retrieved.

1: The data was retrieved by the knn query.

2: The data was retrieved by the text query.

3: The data was retrieved by both the knn and text queries.

coveredPercent

Float

The proportion of shards that returned results normally. For example, 1.0 indicates that all shards returned data normally.