文档推送Demo

更新时间: 2023-06-30 11:12:24

Push 推送数据方式,主要是预先生成符合我们规定格式的待推送数据集合,最后在调用Push方法时,将这些数据集合一次性批量推送到应用中。

相关依赖

使用SDK上传文件所需填下如下的Maven依赖:

<dependency>
    <groupId>com.aliyun.opensearch</groupId>
    <artifactId>aliyun-sdk-opensearch</artifactId>
    <version>4.0.0</version>
</dependency>

Push Demo 样例代码

注意

  • 推送的数据集合必须要符合我们规定的数据集合格式才行,可参考应用控制台->上传文件->参考样例数据,文件中的数据集合格式。

  • 也可以在程序中通过JSONObject 及 JSONArray对象,预先拼接生成符合我们规定格式的数据集合,再调用Push方法一次性将这些数据集合批量推送到应用中。

  • 批量推送文档个数不能太大,不能超过我们规定限制,否则可能会导致推送报错。

add操作

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


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;


/**
 * 文档添加/更新demo
 */
public class testPushDemo {


    private static String appName = "替换为应用名";
    private static String accesskey = "替换accesskey";
    private static String secret = "替换secret";
    private static String host = "替换应用的API访问地址";
    private static String path = "/apps/%s/actions/knowledge-bulk";


    public static void main(String[] args) {


        String appPath = String.format(path, appName);


        //创建并构造OpenSearch对象
        OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
        //创建OpenSearchClient对象,并以OpenSearch对象作为构造参数
        OpenSearchClient openSearchClient = new OpenSearchClient(openSearch);


        //单个doc构建
        JSONObject oneRequest = new JSONObject();
        oneRequest.put("cmd", "ADD");
        JSONObject fields = new JSONObject();
        fields.put("id", "测试文档的id");
        fields.put("title", "测试文档的标题");
        fields.put("url", "测试文档对应来源的url链接");
        fields.put("content", "测试文档的内容");
        fields.put("category", "测试文档的类目");
        oneRequest.put("fields", fields);


        //可以同时添加多条数据
        JSONArray request = new JSONArray();
        request.add(oneRequest);


        Map<String, String> params = new HashMap<String, String>() {{
            put("format", "full_json");
            put("_POST_BODY", request.toJSONString());
        }};
        try {
            OpenSearchResult openSearchResult = openSearchClient.callAndDecodeResult(appPath, params, "POST");
            //打印返回结果
            System.out.println(openSearchResult.getResult());
        } catch (OpenSearchException e) {
            e.printStackTrace();
        } catch (OpenSearchClientException e) {
            e.printStackTrace();
        }
    }
}
说明

目前仅支持ADD操作,更新文档需要通过ADD指令,带入记录全字段内容,否则,由于ADD是覆盖操作,会造成部分未携带的字段内容为空。

delete操作

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import com.aliyun.opensearch.OpenSearchClient;
import com.aliyun.opensearch.sdk.generated.OpenSearch;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
import com.aliyun.opensearch.sdk.generated.commons.OpenSearchResult;


/**
 * 文档删除demo
 */
public class testDeleteDemo {


    private static String appName = "替换为应用名";
    private static String accesskey = "替换accesskey";
    private static String secret = "替换secret";
    private static String host = "替换应用的API访问地址";
    private static String path = "/apps/%s/actions/knowledge-bulk";


    public static void main(String[] args) {


        String appPath = String.format(path, appName);


        //创建并构造OpenSearch对象
        OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
        //创建OpenSearchClient对象,并以OpenSearch对象作为构造参数
        OpenSearchClient openSearchClient = new OpenSearchClient(openSearch);


        //单个删除doc构建
        JSONObject oneRequest = new JSONObject();
        oneRequest.put("cmd", "DELETE");
        JSONObject fields = new JSONObject();
        fields.put("id", "测试删除文档的id");
        oneRequest.put("fields", fields);


        //可以同时添加多条删除的数据
        JSONArray request = new JSONArray();
        request.add(oneRequest);


        Map<String, String> params = new HashMap<String, String>() {{
            put("format", "full_json");
            put("_POST_BODY", request.toJSONString());
        }};
        try {
            OpenSearchResult openSearchResult = openSearchClient.callAndDecodeResult(appPath, params, "POST");
            //打印返回结果
            System.out.println(openSearchResult.getResult());
        } catch (OpenSearchException e) {
            e.printStackTrace();
        } catch (OpenSearchClientException e) {
            e.printStackTrace();
        }
    }
}

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