031 Spring Data Elasticsearch study notes

Elasticsearch Java client has provided some of the less convenient place:

  • Many places require stitching Json string in java string concatenation and how awful you should know

  • They need their own storage to serialize objects into json

  • The results also need their own inquiries to deserialize objects

Therefore, we are not here to explain native Elasticsearch a client API.

But learning kit provided by Spring: Spring the Data elasticsearch .

1 Introduction

Spring Data Elasticsearch is a sub-module in the Spring Data project.

Spring Data's mission is to provide a familiar and consistent Spring-based programming model for data access, while still retaining the special characteristics of the underlying data store.

It makes use of data access technology, relational databases and non-relational databases, map-reduce framework and cloud-based data services become easier. It is an umbrella project that contains many subprojects specific to a given database. Behind these exciting technology projects, it is composed of many companies and developers cooperation development.

Spring Data's mission is to provide a unified programming interface to access a variety of data, whether relational databases (such as MySQL), or non-relational databases (such as Redis), or similar Elasticsearch such index database. Thereby simplifying the code developer, to improve development efficiency.

It contains many different data operation modules:

 

 

Spring Data Elasticsearch page: https://projects.spring.io/spring-data-elasticsearch/

feature:

  • Spring-based support @Configurationjava configuration or XML configuration

  • Class provides a convenient means for operating the ES . Including the realization of intelligent document into the automatic mapping between POJO.ElasticsearchTemplate

  • Spring's use of data conversion services to achieve feature-rich object map

  • Annotation-based metadata mapping mode, but can be extended to support more different data formats

  • Automatically generating a corresponding persistence layer interface implemented method, substantially without manual operations to write the code (similar MyBatis, it is automatically implemented according to the interface). Of course, also supports custom queries artificial

 2. Create a Demo Project

We use maven ( not recommended spring scaffolding, since the spring of scaffolding springboot version number has been updated, and can not choose) to create a new demo, learn Elasticsearch

pom-dependent:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>lucky.elasticsearch</groupId>
    <artifactId>lucky-elasticsearch</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Note: Version 2.1.7 springboot choice, this version maven local repository already exists.

After the completion of import dependence, visible:

 

application.yml configuration file:

Spring: 
  Data: 
    elasticsearch: 
      Cluster-name: leyou 
      Cluster Nodes-: 127.0.0.1:9300 # Port Number 9300 is connected es

Note: Cluster-name, Cluster-Nodes configuration file is installed by elasticsearch decision

Profile when viewed elasticsearch installation can be seen: https: //www.cnblogs.com/luckyplj/p/11582656.html

Create a guide springboot of class:

package lucky.elasticsearch;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * springboot的引导类
 */
@SpringBootApplication
public class ElasticSearchApplication {
    public static void main(String[] args) {
        SpringApplication.run(ElasticSearchApplication.class);
    }
}

3. The entity class and Footnotes

First, we are ready to entity classes:

Package Penalty for lucky.elasticsearch.domain; 

public  class Item { 
    Long the above mentioned id; 
    String title; // title 
    String category; // Categories 
    String Brand; // brand 
    Double. price; // price 
    String ImagesRF Royalty Free; // pictures address 

    public Long getId () {
         return ID; 
    } 

    public  void the setId (Long ID) {
         the this .id = ID; 
    } 

    public String the getTitle () {
         return title; 
    } 

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getImages() {
        return images;
    }

    public void setImages(String images) {
        this.images = images;
    }
}

 

Guess you like

Origin www.cnblogs.com/luckyplj/p/11596935.html