编程知识 cdmana.com

SpringBoot-Elasticsearch

Spring Boot Integrate Elasticsearch Don't talk nonsense , Let's go straight to work

install

Download a elasticsearch, What we have here is elasticsearch-6.4.3.tar.gz

Upload files to the server /usr/local/elasticsearch/elasticsearch-6.4.3

 decompression 
$ tar -zxvf elasticsearch-6.4.3.tar.gz
 Create a user role to run 
$ useradd root-es
 Switching users 
$ su root-es
 start-up   Enter into bin Under the table of contents 
$ ./elasticsearch
 Request inspection 
$ curl 127.0.0.1:9200
{
  "name" : "TZwBB7T",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "7EQkN-jPQyGp672BIG2rbQ",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

This shows success , But by ip+port But there is no way to visit :

$ curl 192.168.1.105:9200
curl: (7) Failed connect to 192.168.1.105:9200;  Connection refused 

It's hard , It turns out that it needs to be modified config Configuration in

$ vim elasticsearch.yml
network.host: 0.0.0.0

 Restart 
$ ./elasticsearch

An error again ERROR: bootstrap checks failed

 edit /etc/security/limits.conf, Add the following ;
* soft nofile 65536
* hard nofile 65536 

 edit  /etc/sysctl.conf, Add the following :
vm.max_map_count=655360
 After the save , perform :
sysctl -p
 Restart , success .

 Request inspection 
$ curl 192.168.1.105:9200
{
  "name" : "TZwBB7T",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "7EQkN-jPQyGp672BIG2rbQ",
  "version" : {
    "number" : "6.4.3",
    "build_flavor" : "default",
    "build_type" : "tar",
    "build_hash" : "fe40335",
    "build_date" : "2018-10-30T23:17:19.084789Z",
    "build_snapshot" : false,
    "lucene_version" : "7.4.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

Springboot Integrate elasticsearch

rely on

plugins {
    id 'org.springframework.boot' version '2.1.11.RELEASE'
    id 'io.spring.dependency-management' version '1.0.10.RELEASE'
    id 'java'
}

group = 'top.lzmvlog'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    //  Configure the image warehouse 
    maven { url 'https://maven.aliyun.com/repository/public/' }
    //  Give priority to local warehouse search 
    mavenLocal()
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-elasticsearch'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'

test {
    useJUnitPlatform()
}

Corresponding version

!!! What we need to pay attention to here is the integrated version problem ,elasticsearch-6.4.3 My integration here is SpringBoot 2.1.11.RELEASE, The specific version corresponds to :

Spring Data Release Train

Spring Data Elasticsearch

Elasticsearch

Spring Boot

2020.0.0[1]

4.1.x[1]

7.9.3

2.3.x[1]

Neumann

4.0.x

7.6.2

2.3.x

Moore

3.2.x

6.8.12

2.2.x

Lovelace

3.1.x

6.2.2

2.1.x

Kay[2]

3.0.x[2]

5.5.0

2.0.x[2]

Ingalls[2]

2.1.x[2]

2.4.0

1.5.x[2]

To configure

server:
  port: 9099
  data:
    elasticsearch:
      cluster-name: elasticsearch
      # elasticsearch  node 
      cluster-nodes: 127.0.0.1:9300

Model

@Data
@Document(indexName = "student", type = "_doc")
public class Student implements Serializable {

    /**
     * id
     */
    private Integer id;

    /**
     *  name 
     */
    private String name;

    /**
     *  Age 
     */
    private Integer age;

}

Mapping notes

  • @Document: Apply at the class level , To indicate that the class is a candidate object mapped to the database . The most important attribute is :
    • indexName: The name of the index used to store this entity . It can contain SpEL Template expression , for example "log-#{T(java.time.LocalDate).now().toString()}"
    • type: Mapping type . If not set , Then use the simple name of the lowercase class .( From version 4.0 Not recommended at first )
    • shards: Number of slices of the index .
    • replicas: Number of copies of index .
    • refreshIntervall: Refresh interval of index . Used for index creation . The default value is *“ 1s”*.
    • indexStoreType: The index storage type of the index . Used for index creation . The default value is *“ fs”*.
    • createIndex: Mark whether to create an index in the repository boot . The default value is true. Please see the Automatically create an index using the corresponding mapping
    • versionType: Configuration of version management . The default value is EXTERNAL.
  • @Id: Apply at the field level , To mark the fields used for identification purposes .
  • @Transient: By default , When storing or retrieving documents , All fields are mapped to the document , This comment does not include this field .
  • @PersistenceConstructor: Marks the given constructor to use when instantiating an object from the database , Even protected packages . Constructor parameters are mapped by name to the retrieved Document Key value in .
  • @Field: Apply and define field properties at the field level , Most attributes map to their respective properties Elasticsearch mapping Definition ( The following list is incomplete , Please review the comments Javadoc For complete reference ):
    • name: Field name , Because it will Elasticsearch The document indicates , If not set , Then use Java Field name .
    • type: Field type , It can be one of the following types :* Text , keyword , long integer , Integers , Short integer , byte , Double precision , floating-point ,Half_Float,Scaled_Float, date ,Date_Nanos, Boolean value , Binary system ,Integer_Range,Float_Range,Long_Range,Double_Range,Date_Range,Ip_ , nesting ,Ip,TokenCount, filter , Flattening ,Search_As_You_Type. see also Elasticsearch Mapping type
    • format and date Type of pattern Definition . Must be defined for date type .format
    • store: Mark whether the original field value should be stored in Elasticsearch in , The default value is false.
    • analyzer,searchAnalyzer,normalizer Used to specify custom analysis and normalization .
  • @GeoPoint: Mark the field as geo_point data type . If the field is GeoPoint Class , You can omit .

Caching services

import org.springframework.stereotype.Repository;

@Repository
public interface CacheRepository<S, I extends Number> extendsElasticsearchRepository<Student, Integer>{
}

Cache controller

@RestController
public class CacheController {

    private final CacheRepository<Student, Integer> cacheRepository;

    StudentController(CacheRepository<Student, Integer> cacheRepository) {
        this.cacheRepository = cacheRepository;
    }

    /**
     *  The new cache 
     *
     * @param student
     * @return
     */
    @PostMapping("save")
    public R save(@RequestBody Student student) {
        //  cache   When the storage keys are consistent   Override the value of the reason   Can be used to modify 
        cacheRepository.save(student);
        return new R(HttpStatus.OK.value());
    }
    
    /**
     *  according to id The query cache 
     *
     * @return
     */
    @GetMapping("selectById")
    public Optional<Student> selectById(Integer id) {
        return cacheRepository.findById(id);
    }

    /**
     *  Query all 
     *
     * @return
     */
    @GetMapping("select")
    public Iterable<Student> select() {
        return cacheRepository.findAll();
    }
    
    /**
     *  Delete all 
     *
     * @return
     */
    @GetMapping("delete")
    public R delete() {
        cacheRepository.deleteAll();
        return new R(HttpStatus.OK.value());
    }

    /**
     *  Delete 
     *
     * @return
     */
    @GetMapping("deleteById")
    public R deleteById(Integer id) {
        cacheRepository.deleteById(id);
        return new R(HttpStatus.OK.value());
    }
}

Inquire about

About Elasticsearch Query for , There are many different ways , Let's introduce some query methods for getting started

Query by method name

 Inquire about 
1、 Create a query from the method name 
  keyword :AND
         OR
         Between
         LessThan  Small 
         GreaterTha
         Like  Fuzzy 
         IgnoreCase
         OrderBy  Sort 
         Asc  positive sequence 
         Desc  flashback 

Caching services

/**
 *  Fuzzy query all student names 
 *
 * @param name  name 
 * @return
 */
List<Student> findAllByNameLike(String name);

controller

/**
 *  Fuzzy query by name 
 *
 * @return
 */
@GetMapping("selectAllByName")
public List<Student> selectAllByName(String name) {
    return cacheRepository.findAllByNameLike(name);
}

Property expression query

 if  Student  contain  UserName  Field 
 example :
List<Student> findByUserName(String userName);
Resulting:
UserName May be split into  user 、name  Two fields , Certain probability !!!
 Underscore characters are considered reserved characters , So we strongly recommend that you follow the standard Java Naming conventions ( namely , Do not use underscores in attribute names , And use hump case ).
 namely :List<Student> findBy_UserName(String userName);

Special parameter processing

    Pageable,Slice and Sort
 notes :
Pageable  Can be used to return paging information 
Slice  Can be used to return a collection object  Slice<Student>  And pass  hasNext()  Method to determine whether there is the next data   For the next query 
Sort  Sort 
Sort sort = Sort.by("id").ascending().and(Sort.by("age").descending());
TypedSort<Student> student = Sort.sort(Student.class);
Sort sort = student.by(Student::getId).ascending().and(student.by(Student::getAge).descending());
TypedSort.by(…)  adopt ( Usually ) Use CGlib Use the runtime agent , This is using Graal VM Native Tools such as may interfere with native image compilation .
 /**
  *  Query all 
  *
  * @return
  */
@GetMapping("select")
public Iterable<Student> select() {
    //  Presentation does not support  lambdas  expression 
    //  Flashback search 
    Sort sort = new Sort(Sort.Direction.DESC, "id");
    return cacheRepository.findAll(sort);
}

/**
 *  Query all 
 *
 * @return
 */
@GetMapping("selectPage")
public Page<Student> selectPage(Pageable pageable) {
    return cacheRepository.findAll(pageable);
}

Custom cache implementation

Realize in Spring Data Repositories Define your own abstraction on top of the function .

Annotations for intermediate repository interfaces @NoRepositoryBean. Make sure you add comments to all repository interfaces ,Spring Data You should not create an instance for it at run time .

@NoRepositoryBean
public interface CacheRepository<S, I extends Number> extends ElasticsearchRepository<S, I> {

    /**
     *  Interface class annotation :
     * @NoRepositoryBean
     *  Make sure you add comments to all repository interfaces ,Spring Data You should not create an instance for it at run time .
     *
     * Resulting:
     * springboot It is not automatically created  bean
     *
     * Parameter 0 of constructor in top.lzmvlog.elasticsearchdemo.controller.StudentController required a bean of type
     * 'top.lzmvlog.elasticsearchdemo.repository.CacheRepository' that could not be found.
     */

    /**
     *  Fuzzy query all student names 
     *
     * @param name  name 
     * @return
     */
    List<Student> findAllByNameLike(String name);
}
@Repository
public interface StudentRepository extends CacheRepository<Student, Integer> {

    /**
     *  Fuzzy query all student names 
     *
     * @param name  name 
     * @return
     */
    @Override
    List<Student> findAllByNameLike(String name);
}
@RestController
public class StudentController {

    private final CacheRepository<Student, Integer> cacheRepository;

    StudentController(CacheRepository<Student, Integer> cacheRepository) {
        this.cacheRepository = cacheRepository;
    }
    
    /**
     *  Fuzzy query by name 
     *
     * @return
     */
    @GetMapping("selectAllByName")
    public List<Student> selectAllByName(String name) {
        return cacheRepository.findAllByNameLike(name);
    }
}

Elasticsearch And Sping data Jpa Almost , If used together, I'm afraid it will cover the circle .

Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .

版权声明
本文为[Shao Jie]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/08/20210809183600768o.html

Scroll to Top