Skip to content

Commit

Permalink
✨ spring-boot-demo-elasticsearch 完成
Browse files Browse the repository at this point in the history
  • Loading branch information
xkcoding committed Dec 20, 2018
1 parent 056630a commit b1432f8
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 53 deletions.
112 changes: 64 additions & 48 deletions spring-boot-demo-elasticsearch/pom.xml
Original file line number Diff line number Diff line change
@@ -1,53 +1,69 @@
<?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>

<artifactId>spring-boot-demo-elasticsearch</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-elasticsearch</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</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-web</artifactId>
</dependency>

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

<build>
<finalName>spring-boot-demo-elasticsearch</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-boot-demo-elasticsearch</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-demo-elasticsearch</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.xkcoding</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>1.0.0-SNAPSHOT</version>
</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</artifactId>
</dependency>

<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>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>

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

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
@SpringBootApplication
public class SpringBootDemoElasticsearchApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootDemoElasticsearchApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(SpringBootDemoElasticsearchApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.xkcoding.elasticsearch.constants;

/**
* <p>
* ES常量池
* </p>
*
* @package: com.xkcoding.elasticsearch.constants
* @description: ES常量池
* @author: yangkai.shen
* @date: Created in 2018-12-20 17:30
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
public interface EsConsts {
/**
* 索引名称
*/
String INDEX_NAME = "person";

/**
* 类型名称
*/
String TYPE_NAME = "person";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.xkcoding.elasticsearch.model;

import com.xkcoding.elasticsearch.constants.EsConsts;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;

/**
* <p>
* 用户实体类
* </p>
*
* @package: com.xkcoding.elasticsearch.model
* @description: 用户实体类
* @author: yangkai.shen
* @date: Created in 2018-12-20 17:29
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
@Document(indexName = EsConsts.INDEX_NAME, type = EsConsts.TYPE_NAME, shards = 1, replicas = 0)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
/**
* 主键
*/
@Id
private Long id;

/**
* 名字
*/
@Field(type = FieldType.Keyword)
private String name;

/**
* 国家
*/
@Field(type = FieldType.Keyword)
private String country;

/**
* 年龄
*/
@Field(type = FieldType.Integer)
private Integer age;

/**
* 生日
*/
@Field(type = FieldType.Date)
private Date birthday;

/**
* 介绍
*/
@Field(type = FieldType.Text, analyzer = "ik_smart")
private String remark;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.xkcoding.elasticsearch.repository;

import com.xkcoding.elasticsearch.model.Person;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import java.util.List;

/**
* <p>
* 用户持久层
* </p>
*
* @package: com.xkcoding.elasticsearch.repository
* @description: 用户持久层
* @author: yangkai.shen
* @date: Created in 2018-12-20 19:00
* @copyright: Copyright (c) 2018
* @version: V1.0
* @modified: yangkai.shen
*/
public interface PersonRepository extends ElasticsearchRepository<Person, Long> {

/**
* 根据年龄区间查询
*
* @param min 最小值
* @param max 最大值
* @return 满足条件的用户列表
*/
List<Person> findByAgeBetween(Integer min, Integer max);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 127.0.0.1:9300
#cluster-name: elasticsearch
cluster-name: metadata_es
cluster-nodes: 192.168.241.19:9300
Loading

0 comments on commit b1432f8

Please sign in to comment.