use of opensearch

Alibaba Cloud Open Search OpenSearch is a large-scale distributed search engine platform independently developed by Alibaba. The platform carries Taobao, Tmall, 1688, Shenma Search, Koubei, Cainiao and other search services. Share Alibaba's mature search technology with developers .

 

opensearch is generally used for page search, and the number of QPS should not be too high (related to charges). It can be used for semantic queries. The data is stored in Alibaba Cloud, and the performance is acceptable

 

1. Open Alibaba Cloud membership, be familiar with API interface documentation, and download related jar packages

 

Second, the project configuration:

      1. Configuration in the applicationContext.xml file:

      

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>file:${catalina.home}/etc/db/jdbc.properties</value>
    <value>file:${catalina.home}/etc/aliyun/opensearch.properties</value>
   </list>
  </property>
 </bean>

    2. Add related configuration to tomcat:

 

    

#Save OpenSearch related configuration
opensearch.accessKeyId=here is the value of accessKeyId
opensearch.accessKeySecret=here is the value of accessKeySecret
opensearch.endpoint=http://opensearch.aliyuncs.com

   3. Configure beans in the project project:

 

    

<bean id="aliyunOpenSearch" class="***.aliyun.client.opensearch.OpenSearchImpl" scope="prototype">
  <property name="accessKeyId" value="${opensearch.accessKeyId}" />
  <property name="accessKeySecret" value="${opensearch.accessKeySecret}" />
  <property name="endpoint" value="${opensearch.endpoint}" />
 </bean>

 

 

Third, the specific use:

    1. Query

//Open the opensearch service
    ISearch search = SpringContextHolder.getBean("aliyunOpenSearch");

   // set instance name
   search.addIndex("aaa");

   // set data format
   search.setFormat("json");

   // request parameter splicing
   StringBuffer sb = new StringBuffer();
   sb.append("name:'").append(name).append("'AND age:'").append(age).append("'");

   //Inquire
   search.setQueryString(s);
   SearchResult result = search.search(); query without pagination
   SearchPageResult resultForPage = search.search(pageNo, pageSize); paging query

 

 

    2. Analysis of the results

   String content = result.getContent(); The format of

   content  is the previously defined json format, and its structure is as follows:

{"status":"",
 "request_id":"",
 "result":{"searchtime":,
              "total":,
              "num":,
              "viewtotal":,
              "items":[],
              "facet":[]        
             },
"errors":[],
"tracer":""
}

    The objects we need are in "items":[]

 

 

   3. Use json-related parsing classes (eg: Gson, fastjson ) to parse the content and it will be OK

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326557649&siteId=291194637
use
use