全部产品
开放搜索

查询分析Demo

更新时间:2017-06-07 13:26:11   分享:   

V3.1.3 SDK 查询分析搜索样例代码

  1. package com.aliyun.opensearch;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.aliyun.opensearch.sdk.dependencies.com.google.common.collect.Lists;
  4. import com.aliyun.opensearch.sdk.dependencies.org.json.JSONObject;
  5. import com.aliyun.opensearch.sdk.generated.OpenSearch;
  6. import com.aliyun.opensearch.sdk.generated.commons.OpenSearchClientException;
  7. import com.aliyun.opensearch.sdk.generated.commons.OpenSearchException;
  8. import com.aliyun.opensearch.sdk.generated.search.Config;
  9. import com.aliyun.opensearch.sdk.generated.search.SearchFormat;
  10. import com.aliyun.opensearch.sdk.generated.search.SearchParams;
  11. import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
  12. import com.aliyun.opensearch.search.SearchParamsBuilder;
  13. import com.aliyun.opensearch.search.SearchResultDebug;
  14. import java.nio.charset.Charset;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. public class testQueryProcessor {
  18. private static String appName = "替换为opensearch应用名";
  19. private static String accesskey = "替换accesskey";
  20. private static String secret = "替换secret";
  21. private static String host = "替换应用的API访问地址";
  22. public static void main(String[] args) {
  23. //查看文件和默认编码格式
  24. System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
  25. System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));
  26. //创建并构造OpenSearch对象
  27. OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
  28. //创建OpenSearchClient对象,并以OpenSearch对象作为构造参数
  29. OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
  30. //创建SearcherClient对象,并以OpenSearchClient对象作为构造参数
  31. SearcherClient searcherClient = new SearcherClient(serviceClient);
  32. //定义Config对象,用于设定config子句参数,分页或数据返回格式,指定应用名等等
  33. Config config = new Config(Lists.newArrayList(appName));
  34. config.setStart(0);
  35. config.setHits(5);
  36. //设置返回格式为json,3.1.2 sdk只支持返回xml和json格式,暂不支持返回fulljson类型,3.1.3版本支持fulljson
  37. config.setSearchFormat(SearchFormat.FULLJSON);
  38. // 创建参数对象
  39. SearchParams searchParams = new SearchParams(config);
  40. // 设置查询子句,若需多个索引组合查询,需要setQuery处合并,否则若设置多个setQuery后面的会替换前面查询
  41. searchParams.setQuery("name:'kfc'");//通过kfc会搜索出“肯德基”的文档
  42. List<String> qpname = new ArrayList<String>();
  43. qpname.add("qp");
  44. searchParams.setQueryProcessorNames(qpname);
  45. // SearchParams的工具类,提供了更为便捷的操作
  46. SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams);
  47. // 执行返回查询结果
  48. SearchResult searchResult;
  49. try {
  50. searchResult = searcherClient.execute(paramsBuilder);
  51. String result = searchResult.getResult();
  52. JSONObject obj = new JSONObject(result);
  53. //判断执行返回是否成功,如果报错执行下面代码,否者输出正确查询内容
  54. if(obj.get("errors").toString().length()>2){
  55. JSONArray jsonarr = JSONArray.parseArray(obj.get("errors").toString());
  56. System.out.println("error code:"+jsonarr.getJSONObject(0).get("code"));
  57. System.out.println("error message:"+jsonarr.getJSONObject(0).get("message"));
  58. }else{
  59. // 输出查询结果
  60. System.out.println("output:"+obj.toString());
  61. //个别用户可能需要debug请求地址信息
  62. SearchResultDebug srd = searcherClient.executeDebug(searchParams);
  63. System.out.println(srd.getRequestUrl().toString());
  64. }
  65. } catch (OpenSearchException e) {
  66. e.printStackTrace();
  67. } catch (OpenSearchClientException e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. }
本文导读目录
本文导读目录
以上内容是否对您有帮助?