文档搜索 Demo

通过SDK 的四种调用方式(SQL QUERY、SQL串、Ha QUERY、Ha串)从向量检索版实例检索数据。

SQL QUERY 方式查询

import java.util.HashMap;
import java.util.Map;

import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;

/**
 * @author alibaba
 */
public class SearchDoc {

    public static void main(String[] args) throws Exception {

        Config config = new Config();
        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("<userName>");
        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("<password>");
        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("<instanceID>");

        //私网域名调用请填写endpoint,如需私网域名调用,请打开如下注释
        //config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
        //config.setHttpProxy("http://公网ip:端口");

        Client client = new Client(config);

        try {
            SearchRequestModel searchRequestModel = new SearchRequestModel();
            SearchQuery searchQuery = new SearchQuery();
            SQLQuery sqlQuery = new SQLQuery();
            sqlQuery.setQuery("select * from table_odps");
            Map<String, String> sqlQueryKvpairs = new HashMap<>();
            sqlQueryKvpairs.put("trace", "INFO");
            sqlQueryKvpairs.put("formatType", "full_json");
            sqlQuery.setKvpairs(sqlQueryKvpairs);
            searchQuery.setSql(client.buildSQLSearchQuery(sqlQuery));

            searchRequestModel.setQuery(searchQuery);
            SearchResponseModel searchResponseModel = client.Search(searchRequestModel);
            System.out.println("result:" + searchResponseModel.getBody());
        } catch (TeaException e) {
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

SQL 串方式查询

import java.util.Map;

import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.Config;
import com.aliyun.ha3engine.models.SearchQuery;
import com.aliyun.ha3engine.models.SearchRequestModel;
import com.aliyun.ha3engine.models.SearchResponseModel;
import com.aliyun.tea.TeaException;

/**
 * @author alibaba
 */
public class SearchDoc {

    public static void main(String[] args) throws Exception {

        Config config = new Config();
        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("<userName>");
        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("<password>");
        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("<instanceID>");

        //私网域名调用请填写endpoint,如需私网域名调用,请打开如下注释
        //config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
        //config.setHttpProxy("http://公网ip:端口");

        Client client = new Client(config);

        try {
            SearchRequestModel searchRequestModel = new SearchRequestModel();
            SearchQuery searchQuery = new SearchQuery();
            searchQuery.setSql("query=select * from table_odps&&kvpair=trace:INFO;formatType:full_json");
            searchRequestModel.setQuery(searchQuery);
            SearchResponseModel searchResponseModel = client.Search(searchRequestModel);
            System.out.println("result:" + searchResponseModel.getBody());
        } catch (TeaException e) {
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

Ha QUERY方式查询

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.*;
import com.aliyun.tea.TeaException;

/**
 * @author alibaba
 */
public class SearchDoc {

    public static void main(String[] args) throws Exception {

        Config config = new Config();
        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("<userName>");
        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("<password>");
        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("<instanceID>");

        //私网域名调用请填写endpoint,如需私网域名调用,请打开如下注释
        //config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
        //config.setHttpProxy("http://公网ip:端口");

        Client client = new Client(config);

        try {
            HaQuery haQuery = new HaQuery();
            // 设置查询子句,为索引及查询内容.
            haQuery.setQuery("id:8148508889615505646");
            // 设置请求集群名称.
            haQuery.setCluster("general");

            // 设置统计子句
            ArrayList<HaQueryAggregateClause> aggregateClauses = new ArrayList<>();

            HaQueryAggregateClause agg = new HaQueryAggregateClause();
            agg.setGroupKey("cate_id"); // 设置group_key
            agg.setAggFun("count()"); // 设置agg_fun
            agg.setAggFilter("cate_id=1"); // 设置agg_filter
            agg.setRange("0~10"); // 设置分段统计
            agg.setAggSamplerThresHold("5"); // 设置采样阈值
            agg.setAggSamplerStep("5"); // 设置采样步长
            agg.setMaxGroup("5"); // 设置最大返回组数
            // 添加Aggregate对象参数,支持设置一个或多个统计字段示例
            aggregateClauses.add(agg);

            haQuery.setAggregate(aggregateClauses);

            // 设置聚合打散子句
            ArrayList<HaQueryDistinctClause> DistinctClauses = new ArrayList<>();

            HaQueryDistinctClause dist = new HaQueryDistinctClause();
            dist.setDistKey("cate_id"); //设置dist_key
            dist.setDistCount("1"); //设置dist_count
            dist.setDistTimes("1"); //设置dist_times
            dist.setReserved("false"); //设置reserved
            dist.setUpdateTotalHit("false"); //设置update_total_hit
            dist.setDistFilter("cate_id<=3"); //设置过滤条件
            dist.setGrade("1.2"); //设置grade
            //添加Distinct对象参数, 支持一个或者多个. dist内容需为 unique.
            DistinctClauses.add(dist);
            haQuery.setDistinct(DistinctClauses);

            // 设置sort条件
            ArrayList<HaQuerySortClause> SortList = new ArrayList<>();
            HaQuerySortClause sorter = new HaQuerySortClause();
            //设置排序id字段
            sorter.setSortKey("id");
            // 设置排序配置:  +  为升序,  - 为降序.
            sorter.setSortOrder("+");
            // 支持设置一个或者多个排序配置.
            SortList.add(sorter);
            haQuery.setSort(SortList);

            //  定义Config对象,用于设定config子句参数,分页或数据返回格式等.
            HaQueryconfigClause Queryconfig = new HaQueryconfigClause();
            Queryconfig.setStart("1");
            Queryconfig.setHit("100");
            // Format目前支持返回 XML,JSON ,protobuf 等格式
            Queryconfig.setFormat("JSON");
            // 设置自定义 config 参数.
            Map<String, String> CustomConfig = new HashMap<>();
            CustomConfig.put("no_summary", "yes");
            CustomConfig.put("qrs_chain", "search");
            Queryconfig.setCustomConfig(CustomConfig);
            haQuery.setConfig(Queryconfig);

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

            // 设置Kvpairs子句参数
            Map<String, String> haKvpairs = new HashMap<>();
            haKvpairs.put("duniqfield", "cate_id");
            haQuery.setKvpairs(haKvpairs);

            // 设置自定义 query 子句.
            Map<String, String> CustomQuery = new HashMap<>();
            CustomQuery.put("searcher_cache", "use:no");
            haQuery.setCustomQuery(CustomQuery);

            SearchRequestModel searchRequestModel = new SearchRequestModel();
            SearchQuery searchQuery = new SearchQuery();
            searchQuery.setQuery(client.buildHaSearchQuery(haQuery));
            searchRequestModel.setQuery(searchQuery);
            SearchResponseModel responseModel = client.Search(searchRequestModel);
            System.out.println("result:" + responseModel.getBody());
        } catch (TeaException e) {
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

Ha 查询串方式查询

import java.util.Map;

import com.aliyun.ha3engine.Client;
import com.aliyun.ha3engine.models.Config;
import com.aliyun.ha3engine.models.SearchQuery;
import com.aliyun.ha3engine.models.SearchRequestModel;
import com.aliyun.ha3engine.models.SearchResponseModel;
import com.aliyun.tea.TeaException;

/**
 * @author alibaba
 */
public class SearchDoc {

    public static void main(String[] args) throws Exception {

        Config config = new Config();
        // 用户名,可在实例详情页>网络信息 查看
        config.setAccessUserName("<userName>");
        // 密码,可在实例详情页>网络信息 修改
        config.setAccessPassWord("<password>");
        //  实例名称,可在实例详情页左上角查看,例:ha-cn-i7*****605
        config.setInstanceId("<instanceID>");

        //私网域名调用请填写endpoint,如需私网域名调用,请打开如下注释
        //config.setEndpoint("ha-cn-******.ha.aliyuncs.com");
        //公网域名调用请填写endpoint,如需私网域名调用,请打开如下注释
        //config.setEndpoint("ha-cn-******.public.ha.aliyuncs.com");
        //公网ip调用填写httpProxy,如需要公网IP调用,请打开如下注释
        //config.setHttpProxy("http://公网ip:端口");

        Client client = new Client(config);

        try {
            SearchRequestModel haQueryRequestModel = new SearchRequestModel();
            SearchQuery haRawQuery = new SearchQuery();
            haRawQuery.setQuery("query=index_id:1&&config=start:0,hit:100,format:json&&cluster=general");
            haQueryRequestModel.setQuery(haRawQuery);
            SearchResponseModel responseModel = client.Search(haQueryRequestModel);
            System.out.println("result:" + responseModel.getBody());
        } catch (TeaException e) {
            System.out.println(e.getMessage());
            Map<String, Object> abc = e.getData();
            System.out.println(com.aliyun.teautil.Common.toJSONString(abc));
        }
    }
}

注意事项