ElasticSearch is one of the most popular search engines for ElasticSearch. This blog uses Docker to install ElasticSearch from scratch. Due to the length of the reasons, will be divided into two blog actual use of word segmentation and pinyin search function.

The preparatory work

Create a Spring Boot project

Add the dependencies we need to use, the other two are not necessary except elasticSearch, the Web is for easy debugging, if you have not used Lombok you need to install IDEA/Eclipse plugin.

<! -- elasticsearch -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<! -- Web easy debugging -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
</dependency>
Copy the code

Install the ElasticSearch

Because I’m running Windows10, I’ll use docker to install elasticsearch. If you don’t have Docker installed, try it. Docker Win10 installation teaching.

  1. First we visitdockerhubfindelasticsearch, download the version you need, I will download the latest version.
  2. Open the shell command and pull the mirror
  3. Elasticsearch (dockerHub Description)
// Shell command docker network create somenetwork docker run -d --name elasticsearch --net somenetwork -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:tagCopy the code

docker ps

127.0.0.1:9200

Install the elasticsearch – head

  1. Also use Docker installation, but accessdockerhubAnd found thatelasticsearch-head404 out, executedocker pull mobz/elasticsearch-headNo, but good thingdocker pull mobz/elasticsearch-head:5It can be pulled, installed and started.
// Shell command docker pull mobz/elasticsearch-head:5 docker run -d --name es_admin -p 9100:9100 mobz/elasticsearch-head:5 docker run -d --name es_admin -p 9100:9100 elasticsearch-head:5Copy the code

  1. accesshttp://127.0.0.1:9100/Elasticsearch is not connected to elasticSearch. You need to configure elasticSearch to support cross-domain access.
  2. Configure elasticSearch. yml to support cross-domain.
/ / docker exec shell command / / into the container - it elasticsearch/bin/bash / / modify the configuration file vi. / config/elasticsearch. Yml / / add the following configuration Http.coron. enabled: true http.coron. allow-origin: "*" // CTRL + C, :wq! Save the configuration // Exit // restart ElasticSearch docker restart ElasticSearchCopy the code

Try http://127.0.0.1:9100/ again, the connection is successful.

Access ElasticSearch using Spring Boot

Add the application.yml configuration

# application.yml
server:
  port: 8080

spring:
  application:
    name: elasticsearch-demo
  elasticsearch:
    rest:
      uris: http://127.0.0.1:9200
      connection-timeout: 1s
      read-timeout: 30s
Copy the code

Start the Spring Boot Project

Access data using ElasticSearchRepository

// UserEntity 
package com.example.elasticsearch.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Document(indexName = "user")
public class UserEntity {

    @Id
    private Long userId;

    private String userName;

    private Integer age;

    private Integer sex;

}

// UserRepository 
package com.example.elasticsearch.repository;

import com.example.elasticsearch.entity.UserEntity;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

public interface UserRepository extends ElasticsearchRepository<UserEntity.Long> {}// ElasticSearchService 
package com.example.elasticsearch.service;

import com.example.elasticsearch.entity.UserEntity;

import java.util.List;

public interface ElasticSearchService {

    void saveUser( UserEntity userEntity );

    void saveUser( List<UserEntity> userEntity );

    UserEntity findById(Long id);

}

// ElasticSearchServiceImpl 
package com.example.elasticsearch.service.impl;

import com.example.elasticsearch.entity.UserEntity;
import com.example.elasticsearch.repository.UserRepository;
import com.example.elasticsearch.service.ElasticSearchService;

import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

@Service
public class ElasticSearchServiceImpl implements ElasticSearchService {

    @Resource
    private UserRepository userRepository;

    @Override
    public void saveUser( UserEntity userEntity ) {
        userRepository.save(userEntity);
    }

    @Override
    public void saveUser( List<UserEntity> userEntity ) {
        userEntity.containsAll(userEntity);
    }

    @Override
    public UserEntity findById( Long id ) {
        returnuserRepository.findById(id).get(); }}// Test
package com.example.elasticsearch;

import com.example.elasticsearch.entity.UserEntity;
import com.example.elasticsearch.service.ElasticSearchService;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

@SpringBootTest
class ElasticsearchApplicationTests {

    @Resource
    private ElasticSearchService elasticSearchService;

    @Test
    void save(a) {
        elasticSearchService.saveUser(UserEntity.builder()
                .userId(1L).userName("David").age(18).sex(1)
                .build());
    }

    @Test
    void findById(a) {
        UserEntity byId = elasticSearchService.findById(1L); System.out.println(byId); }}Copy the code

After the data is inserted successfully, head cannot be viewed, because the head we use is 5, but es is 7. The parsing format is different, so we need to modify vendor.js file.

// Copy vendor.js to local docker cp es_admin:/usr/src/app/_site/vendor.js./ / modify lines 6886 and 7573 Application /x-www-form-urlencoded application/json; Charset = utf-8 // Copy vendor.js to es_admines_admin docker cp vendor.js es_admin:/usr/ SRC /app/_siteCopy the code

Refresh the page after the operation is complete.

conclusion

ElasticSearch is one of the most popular search engines for ElasticSearch. This blog uses Docker to install ElasticSearch from scratch. Behind will be divided into two blog actual use of word segmentation and pinyin search function. The use of Docker can be very convenient for us, docker container and image management is very simple to use, I will also use Docker to conduct MongoDB combat, and Linux learning, we can go to docker rookie tutorial to learn, play will play.