全部产品
阿里云办公

scroll查询Demo

更新时间:2018-12-04 10:26:30

V3.1 SDK Scroll搜索文档样例代码

  1. package com.aliyun.opensearch;
  2. import com.aliyun.opensearch.OpenSearchClient;
  3. import com.aliyun.opensearch.SearcherClient;
  4. import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
  5. import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
  6. import com.aliyun.opensearch.sdk.generated.OpenSearch;
  7. import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
  8. import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
  9. import com.aliyun.opensearch.sdk.generated.search.*;
  10. import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
  11. import com.aliyun.opensearch.search.SearchParamsBuilder;
  12. import java.nio.charset.Charset;
  13. public class testScroll {
  14. //受引擎性能影响,Scroll不支持Aggregate,Distinct,Rank子句,支持单字段sort子句
  15. private static String appName = "替换为opensearch应用名";
  16. private static String accesskey = "替换accesskey";
  17. private static String secret = "替换secret";
  18. private static String host = "替换应用的API访问地址";
  19. public static void main(String[] args) {
  20. //查看文件和默认编码格式
  21. System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
  22. System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));
  23. //创建并构造OpenSearch对象
  24. OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
  25. //创建OpenSearchClient对象,并以OpenSearch对象作为构造参数
  26. OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
  27. //创建SearcherClient对象,并以OpenSearchClient对象作为构造参数
  28. SearcherClient searcherClient = new SearcherClient(serviceClient);
  29. //定义Config对象,用于设定config子句参数,分页,数据返回格式,应用名等等
  30. Config config = new Config(Lists.newArrayList(appName));
  31. //config.setStart(start) scroll该参数不起作用,默认为0
  32. config.setHits(5);//设置每页为5条记录
  33. //设置返回格式为FULLJSON,目前scroll只支持返回JSON和FULLJSON格式
  34. config.setSearchFormat(SearchFormat.FULLJSON);
  35. // 设置搜索结果返回应用中哪些字段
  36. config.setFetchFields(Lists.newArrayList("id", "name", "phone", "int_arr", "literal_arr", "float_arr", "cate_id"));
  37. // 注意:config子句中的rerank_size参数,在Rank类对象中设置
  38. // 创建参数对象
  39. SearchParams searchParams = new SearchParams(config);
  40. // 设置查询子句,若需多个索引组合查询,需要setQuery处合并,否则若设置多个setQuery后面的会替换前面查询
  41. searchParams.setQuery("name:'opensearch'");
  42. // 设置查询过滤条件
  43. //searchParams.setFilter("cate_id<=3"); //此处也可改用后面的ParamsBuilder实现添加过滤条件
  44. //设置scroll方式查询
  45. DeepPaging deep =new DeepPaging();
  46. //不设置默认为1m表示1分钟,该参数表示本次返回的scrollid的有效期
  47. deep.setScrollExpire("3m");
  48. //添加DeepPaging对象参数
  49. searchParams.setDeepPaging(deep);
  50. // SearchParams的工具类,提供了更为便捷的操作
  51. SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams);
  52. // 设置查询过滤条件
  53. // paramsBuilder.addFilter("cate_id<=0", "AND");
  54. // 执行返回查询结果
  55. SearchResult searchResult;
  56. try {
  57. searchResult = searcherClient.execute(paramsBuilder);
  58. String result = searchResult.getResult();
  59. JSONObject obj = new JSONObject(result);
  60. // 假设数据库中只有25条,且每页展示5条,则第6次打印出来items数据为空
  61. for(int i=1;i<=6;i++){
  62. //第一次执行不返回数据,主要返回scroll_id值,第二次调用查询时设置scroll_id
  63. deep.setScrollId(new JSONObject(obj.get("result").toString()).get("scroll_id").toString());
  64. deep.setScrollExpire("3m");//不设置默认为1m表示1分钟,如不想使用默认值,每次重新调用前必须要重新设置下
  65. searchResult = searcherClient.execute(paramsBuilder);
  66. result = searchResult.getResult();
  67. obj = new JSONObject(result);
  68. // 输出查询结果
  69. System.out.println("第"+i+"次执行:"+obj.get("result"));
  70. try {
  71. Thread.sleep(1000);
  72. } catch (InterruptedException e) {
  73. e.printStackTrace();
  74. }//休眠1秒,防止qps请求过高报错
  75. }
  76. } catch (OpenSearchException e) {
  77. e.printStackTrace();
  78. } catch (OpenSearchClientException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }