Springboot Consolidated Elasticsearch implementations for multiple versions of compatibility __elasticsearch

Source: Internet
Author: User
Tags create index createindex iterable
Preface

In the previous learning springboot , MyBatis, Druid and Pagehelper were integrated and the operation of multiple data sources was implemented. This article mainly introduces and uses the current most fire search engine elastisearch, and springboot with the use. Elasticsearch Introduction

Elasticsearch is a lucene based search server that encapsulates Lucene and provides the REST API's operating interface Elasticsearch as a highly scalable open source Full-text search and analysis engine, it can be used to quickly store, search, and analyze large data.
Elasticsearch Main features: distributed, high availability, asynchronous writing, multiple APIs, document-oriented.
Elasticsearch Core concept: Near real-time, cluster, node (save data), index, fragment (will index fragment), copy (fragment can set multiple copies). It can quickly store, search, and analyze massive amounts of data.
Elasticsearch Use cases: Wikipedia, Stack Overflow, Github and so on. Springboot Integration Elasticsearch

Before using springboot to consolidate elasticsearch , we should understand the relationship between the corresponding versions of them.

Spring Boot Version (x) Spring Data elasticsearch Version (y) elasticsearch Version (z)
X <= 1.3.5 Y <= 1.3.4 Z <= 1.7.2*
X >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= Z < 5.0.0**

The springboot version We use here is 1.5.9, and theelasticsearch version is 2.3.5.

Using springboot to consolidate elasticsearchis typically encapsulated using springdata , and then the DAO layer interface inherits Elasticsearchrepository class, which implements a number of methods, such as the commonly used crud methods. the use of Springdata

First of all, before using, do the relevant preparation. MAVEN is configured as follows:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId> spring-boot-starter-web</artifactid>
             <version>1.5.9.RELEASE</version>
        </ dependency>
  <dependency>
            <groupId>org.springframework.boot</groupId>
            < Artifactid>spring-boot-starter-data-elasticsearch</artifactid>
             <version>1.5.9.release</ Version>
        </dependency>
configuration of application.properties
Spring.data.elasticsearch.repositories.enabled = True
spring.data.elasticsearch.cluster-nodes =127.0.0.1\ : 9300

Note: 9300 is the port of the Java client. 9200 is an interface that supports Restful HTTP.

More configuration:

Spring.data.elasticsearch.cluster-name elasticsearch cluster name. (Default value: Elasticsearch)
Spring.data.elasticsearch.cluster-nodes The cluster node address list, separated by commas. If not specified, a client node is started.
Spring.data.elasticsearch.propertie is used to configure additional properties for the client.
spring.data.elasticsearch.repositories.enabled Open Elasticsearch warehouse. (Default value: True. )
Code Writing

Entity Classes

@Document (IndexName = "Userindex", type = "user") public
class user implements serializable{
     /**
     * 
     */< C9/>private static final Long serialversionuid = 1L;
    /** number *
     /private Long ID;
     /** name *
     /private String name;

     /** Age *
     /private Integer;

     /** description *  
     /private String description;

     /** creation Time *
     /private String Createtm;

    Getter and setter slightly
}    

When using springdata , it needs to set IndexName and type in the entity class, which is equivalent to libraries and tables if compared to a traditional database. It is important to note that both indexname and type must be lowercase!!!

DAO Layer

Public interface Userdao extends Elasticsearchrepository<user, long>{
}

DAO layer is simpler here, just inherit elasticsearchrepository the class. The main methods are save, delete, and search. Where the Save method is equivalent to insert and update, none is added, there is overwrite. The Delete method is primarily to delete data and index libraries. As for search is query, including some commonly used queries, such as paging, weight and so on.

Service Layer

@Service public class Userserviceimpl implements UserService {@Autowired private Userdao Userdao;
        @Override public boolean Insert (user user) {Boolean falg=false;
            try{userdao.save (user);
        Falg=true;
        }catch (Exception e) {e.printstacktrace ();
    return falg; @Override public list<user> Search (String searchcontent) {Querystringquerybuilder builder = NE
          W Querystringquerybuilder (searchcontent);
          SYSTEM.OUT.PRINTLN ("query statement:" +builder);
          Iterable<user> SearchResult = Userdao.search (builder);
          Iterator<user> iterator = Searchresult.iterator ();
          List<user> list=new arraylist<user> ();
          while (Iterator.hasnext ()) {List.add (Iterator.next ());
    } return list;
   @Override public list<user> searchuser (integer pagenumber, integer pagesize,string searchcontent) {      Paging parameter pageable pageable = new Pagerequest (pagenumber, pageSize);
        Querystringquerybuilder builder = new Querystringquerybuilder (searchcontent);
        SearchQuery searchquery = new Nativesearchquerybuilder (). withpageable (pageable). Withquery (builder). Build ();
        SYSTEM.OUT.PRINTLN ("query statement:" + searchquery.getquery (). toString ());
        page<user> searchpageresults = Userdao.search (searchquery);
    return Searchpageresults.getcontent (); @Override public list<user> searchuserbyweight (String searchcontent) {//query Function based on weight Scorequerybuilder Functionscorequerybuilder = Querybuilders.functionscorequery (). Add (Querybuilders.boolqu Ery (). Should (Querybuilders.matchquery ("name", Searchcontent)), scorefunctionbuilders.weightfactorfunct
                        ION). Add (Querybuilders.boolquery (). Should (Querybuilders.matchquery ("description", searchcontent)), Scorefunctionbuilders.weightfactorfunction). Setminscore (2);
        SYSTEM.OUT.PRINTLN ("query statement:" + functionscorequerybuilder.tostring ());
        Iterable<user> SearchResult = Userdao.search (Functionscorequerybuilder);
        Iterator<user> iterator = Searchresult.iterator ();
        List<user> list=new arraylist<user> ();
        while (Iterator.hasnext ()) {List.add (Iterator.next ());
    } return list; }
}

Here I simply wrote a few methods, the main method is query. Queries include Full-text search, paging queries, and weight queries. One of the need to explain the weight of the query this block, the weight of the higher, the results of the query, if there is no other data set score, their default score is 1, if you do not want to query these statements, just use setminscore to set it to more than 1 can.

Code Test

Calling an interface to add data

New data:

POST http://localhost:8086/api/user

{"id": 1, "name": "John", "Age": "description": "Zhang San is a Java development engineer", "Createtm": " 2018-4-25 11:07:42 "}
{" id ": 2," name ":" Dick "," Age ":" description ":" Li Si is a test engineer "," Createtm ":" 1980-2-15 19:01:32 "}
{"id": 3, "name": "Harry", "Age": "description": "Harry is a maintenance engineer", "Createtm": "2016-8-21 06:11:32"}

make a full-text query
Request

Http://localhost:8086/api/user?searchContent= Engineer

Return

[{"id": 2, "name": "Dick", "Age": "description": "Li Si is a test engineer", "Createtm": "1980-2-15 19:01:32"},
{"id": 1, "name": "John", "Age": "description": "Zhang San is a Java development engineer", "Createtm": "2018-4-25 11:07:42"},
{"id": 3, "name" : "Harry", "Age": "description": "Harry is a maintenance engineer", "Createtm": "2016-8-21 06:11:32"}]

make a paging query
Request

Http://localhost:8086/api/user?pageNumber=0&pageSize=2&searchContent= Engineer

Return

[{"id": 2, "name": "Dick", "Age": "description": "Li Si is a test engineer"},{"id": 1, "name": "John", "Age": "description": " John is a Java Development engineer "}]

Query for Weights
Request

Http://localhost:8086/api/user2?searchContent= Dick

Return

[{"id": 2, "name": "Dick", "Age": "description": "Li Si is a test engineer", "Createtm": "1980-2-15 19:01:32"}]

Weight Query Printed statements:

Query statement: {{"Function_score": {"Functions": [{"filter": {"bool": {"
          should": {"
            match": {] 
   ' name ': {
                ' query ': ' Dick ',
                ' type ': ' Boolean '}}}}
      ,
      ' weight ': 10.0
    }, {"filter": {"bool": {"
          should": {"match": {"
            description": {"Query":
                "Dick", 
  
    "type": "Boolean"}}}}
      ,
      "weight": 100.0
    }],
    "Min_score": 2.0
  }
}

  

Note: In the test, since the Setminscore minimum weight is set to 2, irrelevant data is not displayed. If you want to show it, remove it from your code.

After the data has been added, you can enter it in the browser: http://localhost:9200/_plugin/head/
Then click on the basic query to see the added data. If you want to use a statement query, you can paste the console-printed query statement into the query interface for query!

Note: Here the Elasticsearch is installed on my windows, and the ES plugin head is installed, the specific installation steps at the end of the article.

In addition to Springdata, there are other ways to operate Elasticsearch .
For example, using native elasticsearch APIs, use the transportclient class implementation.
Or you can use the spring package to inject the bean in the service layer only.
Example:

@Autowired
 

However, these methods have their limitations, that is, with the elasticsearch version changes, the relevant Java API is also doing a constant adjustment, that is, elasticsearch service-side version of the change, The client's code may need to be rewritten.
So introduce a pretty good third-party tool Jestclient, it encapsulates the elasticsearch , filling the elasticsearch httprest interface client blank, It applies to versions above elasticsearch**2.x and does not require changes to the code because the **elasticsearch service-side version changes. jestclient

First add the following dependencies to Maven:

    <dependency>
        <groupId>io.searchbox</groupId> 
         <artifactid>jest</artifactid >
        <version>5.3.3</version>
    </dependency>

Then write the relevant test code.
The comments in the code should be complete, so there is no longer much to tell about the code.

Import java.util.ArrayList;
Import java.util.List;
Import Org.elasticsearch.index.query.QueryBuilders;
Import Org.elasticsearch.search.builder.SearchSourceBuilder;
Import Com.pancm.pojo.User;
Import io.searchbox.client.JestClient;
Import Io.searchbox.client.JestClientFactory;
Import Io.searchbox.client.JestResult;
Import Io.searchbox.client.config.HttpClientConfig;
Import Io.searchbox.core.Bulk;
Import Io.searchbox.core.BulkResult;
Import Io.searchbox.core.Delete;
Import Io.searchbox.core.DocumentResult;
Import Io.searchbox.core.Index;
Import Io.searchbox.core.Search;
Import Io.searchbox.indices.CreateIndex;
Import Io.searchbox.indices.DeleteIndex;
Import io.searchbox.indices.mapping.GetMapping;

Import io.searchbox.indices.mapping.PutMapping;  
        public class Jesttest {private static jestclient jestclient;  
private static String IndexName = "Userindex";  
        private static String IndexName = "Userindex2";  
      private static String TypeName = "user";  private static String elasticips= "http://192.169.2.98:9200";


        private static String elasticips= "http://127.0.0.1:9200";  
            public static void Main (string[] args) throws Exception {jestclient = Getjestclient ();
            Insertbatch ();
            Serach1 ();
            SERACH2 ();
            Serach3 ();  

        Jestclient.close ();   
            private static Jestclient getjestclient () {Jestclientfactory factory = new Jestclientfactory (); Factory.sethttpclientconfig (New Httpclientconfig.builder (elasticips). Conntimeout (60000). ReadTimeout (60000).  
            Multithreaded (true). Build ());  
        return Factory.getobject ();
            public static void Insertbatch () {list<object> objs = new arraylist<object> ();
            Objs.add (New User (1L, "John", 20, "Zhang San is a Java development engineer", "2018-4-25 11:07:42"));
            Objs.add (New User (2L, "Dick", 24, "Li Si is a test engineer", "1980-2-15 19:01:32")); OBjs.add (New User (3L, "Harry", 25, "Harry is an operational engineer", "2016-8-21 06:11:32"));
            Boolean result = false;
            try {result = Insertbatch (Jestclient,indexname, TYPENAME,OBJS);
            catch (Exception e) {e.printstacktrace ();
        } System.out.println ("New batch:" +result);
            }/** * Full-Text search/public static void Serach1 () {String query = "Engineer"; 
                 try {searchsourcebuilder Searchsourcebuilder = new Searchsourcebuilder (); 
                 Searchsourcebuilder.query (querybuilders.querystringquery (query)); 
                Paging setup searchsourcebuilder.from (0). Size (2);
                SYSTEM.OUT.PRINTLN ("full-Text Search query statement:" +searchsourcebuilder.tostring ());
            System.out.println ("Full-text Search returns results:" +search (Jestclient,indexname, TypeName, searchsourcebuilder.tostring ()));
     catch (Exception e) {e.printstacktrace ();       }/** * Exact search/public static void Serach2 () {try { 
                Searchsourcebuilder Searchsourcebuilder = new Searchsourcebuilder (); 
                Searchsourcebuilder.query (Querybuilders.termquery ("Age", 24));
                SYSTEM.OUT.PRINTLN ("Exact search query statement:" +searchsourcebuilder.tostring ());
            SYSTEM.OUT.PRINTLN ("Exact search returns results:" +search (Jestclient,indexname, TypeName, searchsourcebuilder.tostring ()));
            catch (Exception e) {e.printstacktrace (); }/** * Interval search/public static void Serach3 () {String createtm= "
            Createtm ";
            String from= "2016-8-21 06:11:32";

            String to= "2018-8-21 06:11:32"; 
                try {searchsourcebuilder Searchsourcebuilder = new Searchsourcebuilder (); 
      Searchsourcebuilder.query (Querybuilders.rangequery (Createtm). GTE (from)-LTE (to);          System.out.println ("Interval search statement:" +searchsourcebuilder.tostring ());
            System.out.println ("Interval search returns results:" +search (Jestclient,indexname, TypeName, searchsourcebuilder.tostring ());
            catch (Exception e) {e.printstacktrace ();
         /** * CREATE INDEX * @param indexname * @return * @throws Exception */Public Boolean createindex (Jestclient jestclient,string indexname) throws Exception {Jestre  
            Sult JR = Jestclient.execute (new Createindex.builder (IndexName). build ());  
        return jr.issucceeded ();
         /** * New Data * @param indexname * @param typeName * @param source * @return * @throws Exception/public boolean insert (Jestclient jestclient,string indexname, S Tring TypeName, String source) throws Exception {putmapping putmapping = new Putmapping.builder (iNdexname, TypeName, 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.