全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网
开放搜索

下拉提示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.*;
  9. import com.aliyun.opensearch.sdk.generated.search.general.SearchResult;
  10. import com.aliyun.opensearch.search.SearchParamsBuilder;
  11. import com.aliyun.opensearch.search.SearchResultDebug;
  12. import java.nio.charset.Charset;
  13. public class testSuggest {
  14. private static String appName = "替换为opensearch应用名";
  15. private static String accesskey = "替换accesskey";
  16. private static String secret = "替换secret";
  17. private static String host = "替换应用的API访问地址";
  18. public static void main(String[] args) {
  19. //查看文件和默认编码格式
  20. System.out.println(String.format("file.encoding: %s", System.getProperty("file.encoding")));
  21. System.out.println(String.format("defaultCharset: %s", Charset.defaultCharset().name()));
  22. //创建并构造OpenSearch对象
  23. OpenSearch openSearch = new OpenSearch(accesskey, secret, host);
  24. //创建OpenSearchClient对象,并以OpenSearch对象作为构造参数
  25. OpenSearchClient serviceClient = new OpenSearchClient(openSearch);
  26. //创建SearcherClient对象,并以OpenSearchClient对象作为构造参数
  27. SearcherClient searcherClient = new SearcherClient(serviceClient);
  28. //定义Config对象,用于设定config子句参数,分页或数据返回格式,指定应用名等等
  29. Config config = new Config(Lists.newArrayList(appName));
  30. config.setStart(0);
  31. config.setHits(5);
  32. //设置返回格式为json,3.1.2 sdk只支持返回xml和json格式,暂不支持返回fulljson类型,3.1.3版本支持fulljson
  33. config.setSearchFormat(SearchFormat.JSON);
  34. // 创建参数对象
  35. SearchParams searchParams = new SearchParams(config);
  36. // 设置查询子句,若需多个索引组合查询,需要setQuery处合并,否则若设置多个setQuery后面的会替换前面查询
  37. Suggest sug = new Suggest();
  38. sug.setSuggestName("suggest");
  39. searchParams.setSuggest(sug);//searchParams对象添加下拉对象
  40. searchParams.setQuery("nihao");//在控制台中下拉列表中如果能搜出"nihao",这里也能够搜出
  41. // SearchParams的工具类,提供了更为便捷的操作
  42. SearchParamsBuilder paramsBuilder = SearchParamsBuilder.create(searchParams);
  43. // 执行返回查询结果
  44. SearchResult searchResult;
  45. try {
  46. searchResult = searcherClient.execute(paramsBuilder);
  47. String result = searchResult.getResult();
  48. JSONObject obj = new JSONObject(result);
  49. //判断执行返回是否成功,如果报错执行下面代码,否者输出正确查询内容
  50. if(obj.has("errors")){
  51. JSONArray jsonarr = JSONArray.parseArray(obj.get("errors").toString());
  52. System.out.println("error code:"+jsonarr.getJSONObject(0).get("code"));
  53. System.out.println("error message:"+jsonarr.getJSONObject(0).get("message"));
  54. }else{
  55. // 输出查询结果
  56. System.out.println("output:"+obj.toString());
  57. //个别用户可能需要debug请求地址信息
  58. SearchResultDebug srd = searcherClient.executeDebug(searchParams);
  59. System.out.println(srd.getRequestUrl().toString());
  60. }
  61. } catch (OpenSearchException e) {
  62. e.printStackTrace();
  63. } catch (OpenSearchClientException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. }
本文导读目录