文档搜索 Demo

通过SDK 调用向量检索版实例进行数据检索

代码示例

示例1: 直接使用 ha 查询串进行搜索

# -*- coding: utf-8 -*-


from alibabacloud_ha3engine import models, client
from alibabacloud_tea_util import models as util_models
from Tea.exceptions import TeaException, RetryError

def search():
    Config = models.Config(
        endpoint="<endpoint>",   # 实例访问域名,VPC环境使用API域名,公网环境使用公网域名
        instance_id="<instanceID>",  # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        protocol="http",
        access_user_name="<userName>",  # 用户名,可在实例详情页>网络信息 查看
        access_pass_word="<password>"  # 密码,可在实例详情页>网络信息 修改

    )

    # 如用户请求时间较长. 可通过此配置增加请求等待时间. 单位 ms
    # 此参数可在 search_with_options 方法中使用
    runtime = util_models.RuntimeOptions(
        connect_timeout=5000,
        read_timeout=10000,
        autoretry=False,
        ignore_ssl=False,
        max_idle_conns=50
    )

    # 初始化 Ha3Engine Client
    ha3EngineClient = client.Client(Config)

    try:
        optionsHeaders = {}
        query_str = "config=hit:4,format:json,fetch_summary_type:pk,qrs_chain:search&&query=id:<pk>&&cluster=general"
        haSearchQuery = models.SearchQuery(query=query_str)
        haSearchRequestModel = models.SearchRequestModel(optionsHeaders, haSearchQuery)
        hastrSearchResponseModel = ha3EngineClient.search(haSearchRequestModel)
        print(hastrSearchResponseModel)
      
    except TeaException as e:
        print(f"send request with TeaException : {e}")
    except RetryError as e:
        print(f"send request with Connection Exception  : {e}")

示例 2: 使用构造的查询进行 ha 查询

# -*- coding: utf-8 -*-


from alibabacloud_ha3engine import models, client
from alibabacloud_tea_util import models as util_models
from Tea.exceptions import TeaException, RetryError
def search():
    Config = models.Config(
        endpoint="<endpoint>",   # 实例访问域名,VPC环境使用API域名,公网环境使用公网域名
        instance_id="<instanceID>",  # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        protocol="http",
        access_user_name="<userName>",  # 用户名,可在实例详情页>网络信息 查看
        access_pass_word="<password>"  # 密码,可在实例详情页>网络信息 修改

    )

    # 初始化 Ha3Engine Client
    ha3EngineClient = client.Client(Config)
    optionsHeaders = {}

    try:
        # 设置聚合打散子句
        DistinctClauses = []

        # 设置统计子句配置对象
        aggregateClauses = []
        haQueryAggregateClause = models.HaQueryAggregateClause(
            group_key="cate_id",  # 设置group_key
            agg_fun="count()",  # 设置agg_fun
            range="0~10",  # 设置分段统计
            max_group="5",  # 设置最大返回组数
            agg_filter="cate_id=1",  # 设置agg_filter
            agg_sampler_thres_hold="5",  # 设置采样阈值
            agg_sampler_step="5",  # 设置采样步长
        )

        # 添加Aggregate对象参数, 支持设置一个或多个统计字段示例
        aggregateClauses.append(haQueryAggregateClause)

        # 定义Config对象,用于设定config子句参数,分页或数据返回格式等.
        CustomConfig = {
            "no_summary": "yes",
            "qrs_chain": "search"
        }
        haQueryconfig = models.HaQueryconfigClause(
            # 设置 查询起始位置
            start="1",

            # 设置查询页返回数量
            hit="10",

            # Format目前支持返回 XML,JSON, protobuf 等格式
            format="JSON",

            # 设置自定义 config 参数.
            custom_config=CustomConfig
        )

        # 设置sort条件
        haQuerySortClauseList = []
        haQuerySortClause = models.HaQuerySortClause(
            # 设置排序id字段
            sort_key="id",
            # 设置排序配置: +  为升序, - 为降序.
            sort_order="+"
        )
        haQuerySortClauseList.append(haQuerySortClause)

        # haQuery 中 Kvpairs 为 dict 类型.  具体 key 范围. 请参见开发文档.
        haKvpairs = {
            "uniqfield": "cate_id"
        }

        # 设置聚合打散子句
        DistinctClauses = []

        dist = models.HaQueryDistinctClause(
            # 设置dist_key
            dist_key="cate_id",
            # 设置dist_count
            dist_count="1",
            # 设置dist_times
            dist_times="1",
            # 设置reserved
            reserved="false",
            # 设置过滤条件
            dist_filter="cate_id<=3",
            # 设置update_total_hit
            update_total_hit="false",
            # 设置grade
            grade="1.2",
        )

        # 添加Distinct对象参数, 支持一个或者多个.dist内容需为 unique.
        DistinctClauses.append(dist)

        # 定义 自定义 query Config 子句.
        CustomQuery = {
            "searcher_cache": "use:no"
        }

        haQuery = models.HaQuery(

            # 设置查询子句,为索引及查询内容.
            query="id:8148508889615505646",

            # 设置请求集群名称.
            cluster="general",

            # 设置 config 子句参数
            config=haQueryconfig,

            # 设置查询过滤条件
            # 支持设置一个或多个 filter 参数.请使用 AND | OR 对多个 filter 进行连接.
            # 比较支持: '=' | '>' | '<' | '<=' | '>=' | '!='
            filter="id>100 AND id<=1000",

            # 设置 aggregate 子句参数
            aggregate=aggregateClauses,

            # 设置 Kvpairs 子句参数
            kvpairs=haKvpairs,

            # 设置 sort 子句参数
            sort=haQuerySortClauseList,

            # 设置 distinct 子句参数
            distinct=DistinctClauses,
            custom_query=CustomQuery

        )
        searchQuery = models.SearchQuery(query=ha3EngineClient.build_ha_search_query(haQuery))
        searchRequestModel = models.SearchRequestModel(optionsHeaders, searchQuery)
        # 使用默认 运行时参数进行请求
        haStructResponseModel = ha3EngineClient.search(searchRequestModel)
        print(haStructResponseModel)

    except TeaException as e:
        print(f"send request with TeaException : {e}")
    except RetryError as e:
        print(f"send request with Connection Exception  : {e}")

示例3: 直接使用 ha-sql 查询串进行搜索.

# -*- coding: utf-8 -*-


from alibabacloud_ha3engine import models, client
from alibabacloud_tea_util import models as util_models
from Tea.exceptions import TeaException, RetryError
def search():
    Config = models.Config(
        endpoint="<endpoint>",   # 实例访问域名,VPC环境使用API域名,公网环境使用公网域名
        instance_id="<instanceID>",  # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        protocol="http",
        access_user_name="<userName>",  # 用户名,可在实例详情页>网络信息 查看
        access_pass_word="<password>"  # 密码,可在实例详情页>网络信息 修改

    )

    # 初始化 Ha3Engine Client
    ha3EngineClient = client.Client(Config)
    optionsHeaders = {}

    try:
        sql_str = "select * from <indexTableName>&&kvpair=trace:INFO;formatType:json"
        sqlsearchQuery = models.SearchQuery(sql=sql_str)
        sqlSearchRequestModel = models.SearchRequestModel(optionsHeaders, sqlsearchQuery)
        sqlstrSearchResponseModel = ha3EngineClient.search(sqlSearchRequestModel)
        print(sqlstrSearchResponseModel)

    except TeaException as e:
        print(f"send request with TeaException : {e}")
    except RetryError as e:
        print(f"send request with Connection Exception  : {e}")

示例4 : 直接使用 ha-sql 结构化进行搜索

# -*- coding: utf-8 -*-


from alibabacloud_ha3engine import models, client
from alibabacloud_tea_util import models as util_models
from Tea.exceptions import TeaException, RetryError
def search():
    Config = models.Config(
        endpoint="<endpoint>",   # 实例访问域名,VPC环境使用API域名,公网环境使用公网域名
        instance_id="<instanceID>",  # 实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        protocol="http",
        access_user_name="<userName>",  # 用户名,可在实例详情页>网络信息 查看
        access_pass_word="<password>"  # 密码,可在实例详情页>网络信息 修改

    )

    # 初始化 Ha3Engine Client
    ha3EngineClient = client.Client(Config)
    optionsHeaders = {}

    try:
        sqlQueryKvpairs = {
            "trace": "INFO",
            "formatType": "full_json"
        }
        sqlQuery =models.SQLQuery(
            query  ="select * from odps",
            kvpairs=sqlQueryKvpairs
        )

        searchQuery = models.SearchQuery(sql=ha3EngineClient.build_sqlsearch_query(sqlQuery))
        searchRequestModel = models.SearchRequestModel(optionsHeaders, searchQuery)
        # 使用默认 运行时参数进行请求
        sqlStructResponseModel = ha3EngineClient.search(searchRequestModel)
        print(sqlStructResponseModel)

    except TeaException as e:
        print(f"send request with TeaException : {e}")
    except RetryError as e:
        print(f"send request with Connection Exception  : {e}")

注意事项