ES deployment and program development

Background description:

       The system search promotion activity is switched from the previous method of database fuzzy matching to the method of search engine. The selected content is mainly ElasticSearch. This document mainly records the use of ES for single-node deployment and the development of ES search code. Upload some code for your reference.

 

System environment:

        Deployment environment: CentOS 6.7 + JDK 1.7 + ElasticSearch 2.3.5

        Development environment: Window 10 + JDK 1.7 + IDEA 2017

 

Environment preparation:

        Integrate POM dependencies:

      
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>2.0.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch</groupId>
    <artifactId>elasticsearch</artifactId>
    <version>2.3.5</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.7.1</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.7.1</version>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.17</version>
</dependency>

 

 

      Add Spring configuration:

 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
       xsi:schemaLocation="
      http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
      http://www.springframework.org/schema/tx
      http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
      http://www.springframework.org/schema/data/elasticsearch
      http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd
      ">

    <!-- Enable configuration scan-->
    <context:component-scan base-package="com.jiangzh"></context:component-scan>

    <!-- Just configure the node information of an ES, and enable the sniffing function, the cluster will be automatically discovered-->
    <elasticsearch:transport-client id="client"
                                    cluster-name="my-application"
                                    client-transport-sniff="true"
                                    cluster-nodes="192.168.4.109:9300" />

    <!-- Configure the template provided by Spring-data to es -->
    <bean name="elasticsearchTemplate"
          class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
        <constructor-arg name="client" ref="client" />
    </bean>

</beans>

 

 

     Create data entities, one-to-one correspondence with data fields, for quick query use:

  

public class AppPOJO {

    private int res_pk;
    private String res_kaipk;
    private String res_name;
    // Omit getter and setter methods

}

 

    Create a VO entity, convert the data entity into a business entity according to actual needs, and return:

   

@Document ( indexName = "testidex" , type = "estype" )
 public class AppModel {
     @Id
 private Integer appId ; private String resName ; 
     // omit other properties and getter and setter methods
}

 

 

    

 Develop a search function:

   

       1. Data layer:

       Provides basic data support, including basic CRUD operations:

       Core implementation of new features:

          

String pid = et.index(new IndexQueryBuilder()
        // Get the index of the data operation
        .withIndexName (ESConst.ESIndexEnum. APPSEARCH .getName ())
        // Get the Type of the data operation
        .withType(ESConst.ESTypeEnum.APPSEARCH.getName())
        // Set the data ID, if not set, it will be automatically generated
        .withId("" + pojo.getRes_pk())
        // specific new data
        .withSource(jsonData)
        .build()
);

  

        2. Modify the core implementation of the function

 

IndexRequest indexRequest = new IndexRequest();
indexRequest.source(jsonData);
UpdateQuery updateQuery = new UpdateQueryBuilder()
        .withIndexName(ESConst.ESTypeEnum.APPSEARCH.getName())
        .withType(ESConst.ESTypeEnum.APPSEARCH.getName())
        .withId(""+pojo.getRes_pk())
        .withIndexRequest(indexRequest)
        .withClass(AppPOJO.class)
        .build();
et.update(updateQuery);

 

    3. Delete the core implementation of the function

   

DeleteQuery deleteQuery = new DeleteQuery();
deleteQuery.setQuery(QueryBuilders.termQuery("appId", ""+appUid));
deleteQuery.setIndex(ESConst.ESIndexEnum.APPSEARCH.getName());
deleteQuery.setType(ESConst.ESTypeEnum.APPSEARCH.getName());
et.delete(deleteQuery);

 

 

  4. Query according to the primary key

StringQuery query = new StringQuery(
        QueryBuilders.termQuery("appId",appUid).toString()
);
AppPOJO appPOJO = et.queryForObject(query,AppPOJO.class);

  It must be noted here that when using this API, the entity Model must be annotated and marked, see the code for details

 

 

5. Query all data for homepage display, the core code is as follows:

// Use the Scan method to scan, which can improve the query speed
// set query conditions
SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withIndices (ESConst.ESIndexEnum. APPSEARCH .getName ())
        .withTypes(ESConst.ESTypeEnum.APPSEARCH.getName())
        .withQuery (QueryBuilders. matchAllQuery ())
        .withFilter(QueryBuilders.termQuery("res_online","y"))
        .withPageable(new PageRequest(0,1)).build();

// Generate scan number by query condition
String scrollId = et.scan(searchQuery, 1000, false);

// data matching by scan number
Page page = et.scroll(scrollId, 5000L,AppPOJO.class);
List<AppPOJO> content = null;
if(page.hasContent()){
    content = page.getContent();
}else{
    break;
}

 

 

6. Obtain the total data of the query, which is mainly used for paging on the front page

SearchQuery searchQuery = new NativeSearchQueryBuilder()
        .withIndices (ESConst.ESIndexEnum. APPSEARCH .getName ())
        .withTypes(ESConst.ESTypeEnum.APPSEARCH.getName())
        .withQuery(QueryBuilders.multiMatchQuery(condition
                ,"column1","column2"))
        .build();
long count = et.count(searchQuery);

 

Above, the basic APIs of all the data layers have been completed, and the rest of the business codes are different because everyone's business is different, so I won't share them one by one. You can make up your own mind.

 

Finally, the above are all core codes. I will send the engineering test code to the attachment to share with you.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326250118&siteId=291194637