springboot integrated elastic search 7.2.0, head plug-in

Keywords: ElasticSearch Java Spring SpringBoot

1. Download Elasticsearch

Download address on official website https://www.elastic.co/cn/downloads/elasticsearch The author downloaded 7.2.0 here. If you want to download the previous version, you can click the following figure to download:

2. Start elastic search

1 > Enter bin directory and click elasticsearch.bat to start Elasticsearch
Visit http://127.0.0.1:9200/ You can see the following information:

3. Introduce dependency package into springboot pom.xm

    <dependency>
        <groupId>org.elasticsearch</groupId>
        <artifactId>elasticsearch</artifactId>
        <version>7.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.elasticsearch.client</groupId>
        <artifactId>transport</artifactId>
        <version>7.2.0</version>
    </dependency>

4. Configure the property file in application.yml

spring:
  	es:
      host: 127.0.0.1
      port: 9300

5. Inject the load profile and TransportClient

import lombok.Data;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Data
@Component
@ConfigurationProperties(prefix = "spring.es")
public class ElasticsearchConfig {
    private String host;
    private Integer port;

    @Bean
    public TransportClient client() throws UnknownHostException {
        // Set configuration information of es node
        Settings settings = Settings.builder()
                .put("cluster.name", "myClusterName")
                .build();
        // Instantiate the client object of es
        TransportClient client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new TransportAddress(InetAddress.getByName(host), port));

        return client;
    }
}

6. Add (index) view interface implementation (get)

  import io.swagger.annotations.Api;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.Date;
@RestController
@RequestMapping("/es")
public class ElasticsearchController {
    @Autowired
    private TransportClient client;
    /**
     * Add book data         
     * @param title       Titles of books
     * @param author      Book author
     * @param wordCount   Word number of books
     * @param publishDate Issue time
     * @return
     */
    @PostMapping("/add/book/novel")
    public ResponseEntity add(@RequestParam("title") String title,
                              @RequestParam("author") String author,
                              @RequestParam("word_count") int wordCount,
                              @RequestParam("publish_date")
                              @DateTimeFormat(pattern = "yyy-MM-dd HH:mm:ss")
                                      Date publishDate)  {
        try {
            // build the parameter into a json object
            XContentBuilder content = XContentFactory.jsonBuilder()
                    .startObject()
                    .field("title", title)
                    .field("author", author)
                    .field("word_count", wordCount)
                    .field("publish_date", publishDate.getTime())
                    .endObject();
            IndexResponse response = client.prepareIndex("books", "book","1")
                    .setSource(content)
                    .get();
            return new ResponseEntity(response.getId(), HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }finally {
            client.close();
        }
    }
     /**
     * Press id to query
     * @param id
     * @return
     */
    @GetMapping("/get/book/novel")
    public ResponseEntity searchById(@RequestParam("id") String id) {
        if (id.isEmpty()) {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        // Query data to es by index, type and id
        GetResponse response = client.prepareGet("books", "book", id).get();
        if (!response.isExists()) {
            return new ResponseEntity(HttpStatus.NOT_FOUND);
        }
        // Return the queried data
        return new ResponseEntity(response.getSource(), HttpStatus.OK);
    }
}

The author integrates swagger here, so the results are as follows:

Modification and deletion: please refer to official Api: Official Api address: https://www.elastic.co/guide/en/elasticsearch/client/java-api/7.3/index.html

7,head plug-in

Reference resources: https://www.cnblogs.com/hts-technology/p/8477258.html
Finally, the author successfully integrates the following figures:

Posted by horsleyaa on Tue, 29 Oct 2019 12:33:10 -0700