全部产品
开放搜索

scroll查询Demo

更新时间:2017-09-07 17:40:39   分享:   

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

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