底纹Demo

引入依赖

pip install alibabacloud_tea_util 
pip install alibabacloud_opensearch_util
pip install alibabacloud_credentials

底纹示例

# -*- coding: utf-8 -*-
import time
from typing import Dict, Any
from Tea.exceptions import TeaException
from Tea.request import TeaRequest
from alibabacloud_tea_util import models as util_models
from BaseRequest import Config, Client
class opensearch:
    def __init__(self, config: Config):
        self.Clients = Client(config=config)
        self.runtime = util_models.RuntimeOptions(
            connect_timeout=10000,
            read_timeout=10000,
            autoretry=False,
            ignore_ssl=False,
            max_idle_conns=50,
            max_attempts=3
        )
        self.header = {}
    def appAlgoSearch(self, app_name: str, algo_name: str, algo_type: str, query_params: dict) -> Dict[str, Any]:
        algo_type = algo_type if algo_type else "suggest"
        path_name = f'/v3/openapi/apps/{app_name}/suggest/{algo_name}/search'
        if algo_type != "suggest":
            if algo_type not in ["hint", "hot"]:
                raise Exception("algo_type should be hint or hot")
            path_name = f'/v3/openapi/apps/{app_name}/actions/{algo_type}'
        try:
            response = self.Clients._request(method="GET", pathname=path_name, query=query_params,headers = self.header,body=None, runtime=self.runtime)
            return response
        except TeaException as e:
            print(e)

if __name__ == "__main__":
    # 配置统一的请求入口 注意:host需要去掉http://
    endpoint = "<endpoint>"
    # 支持 protocol 配置 HTTPS/HTTP
    endpoint_protocol = "HTTP"
    # 用户识别信息
    access_key_id = "<access_key_id>"
    access_key_secret = "<access_key_secret>"
    # 支持 type 配置 sts/access_key 鉴权. 其中 type 默认为 access_key 鉴权. 使用 sts 可配置 RAM-STS 鉴权.
    # 备选参数为:  sts 或者 access_key
    auth_type = "sts"
    # 如果使用 RAM-STS 鉴权, 请配置 security_token, 可使用 阿里云 AssumeRole 获取 相关 STS 鉴权结构.
    security_token = "<security_token>"

    # 配置请求使用的通用信息.
    # 注意:security_token和type参数,如果不是子账号需要省略
    Configs = Config(endpoint=endpoint, access_key_id=access_key_id, access_key_secret=access_key_secret,
                     security_token=security_token, type=auth_type, protocol=endpoint_protocol)
    
    # 创建 opensearch 实例
    ops = opensearch(Configs)
    app_name = "app_name"
    
    # ---------------  热词底纹搜索 ---------------
    
    hint_algoQuery = {
        "sort_type": "default",
        "hit": 10,
        "user_id": "a7a0d37c824b659f36a5b9e3b819fcdd"
    }
    res3 = ops.appAlgoSearch(app_name=app_name, algo_name=None, algo_type="hint", query_params=hint_algoQuery)
    print(res3)
说明

参考:热搜底纹搜索

阿里云首页 智能开放搜索 OpenSearch 相关技术圈