Skip to content

Commit 72a0423

Browse files
committed
springboot-elasticsearch-ik init
1 parent 379e153 commit 72a0423

File tree

7 files changed

+395
-0
lines changed

7 files changed

+395
-0
lines changed

springboot-es/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-es/pom.xml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.tellsea</groupId>
12+
<artifactId>springboot-es</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-es</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.springframework.boot</groupId>
28+
<artifactId>spring-boot-starter-web</artifactId>
29+
</dependency>
30+
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-test</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
</dependency>
41+
</dependencies>
42+
43+
<build>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-maven-plugin</artifactId>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.tellsea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootEsApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootEsApplication.class, args);
11+
}
12+
13+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cn.tellsea.bean;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import org.springframework.data.annotation.Id;
7+
import org.springframework.data.elasticsearch.annotations.Document;
8+
import org.springframework.data.elasticsearch.annotations.Field;
9+
import org.springframework.data.elasticsearch.annotations.FieldType;
10+
11+
@Data
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Document(indexName = "item", type = "docs", shards = 1, replicas = 0)
15+
public class Item {
16+
@Id
17+
private Long id;
18+
19+
@Field(type = FieldType.Text, analyzer = "ik_max_word")
20+
private String title; //标题
21+
22+
@Field(type = FieldType.Keyword)
23+
private String category;// 分类
24+
25+
@Field(type = FieldType.Keyword)
26+
private String brand; // 品牌
27+
28+
@Field(type = FieldType.Double)
29+
private Double price; // 价格
30+
31+
@Field(index = false, type = FieldType.Keyword)
32+
private String images; // 图片地址
33+
}
34+
/**
35+
* Spring Data通过注解来声明字段的映射属性,有下面的三个注解:
36+
*
37+
* - `@Document` 作用在类,标记实体类为文档对象,一般有两个属性
38+
* - indexName:对应索引库名称
39+
* - type:对应在索引库中的类型
40+
* - shards:分片数量,默认5
41+
* - replicas:副本数量,默认1
42+
* - `@Id` 作用在成员变量,标记一个字段作为id主键
43+
* - `@Field` 作用在成员变量,标记为文档的字段,并指定字段映射属性:
44+
* - type:字段类型,是是枚举:FieldType
45+
* - index:是否索引,布尔类型,默认是true
46+
* - store:是否存储,布尔类型,默认是false
47+
* - analyzer:分词器名称
48+
* */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cn.tellsea.repository;
2+
3+
import cn.tellsea.bean.Item;
4+
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
5+
6+
import java.util.List;
7+
8+
public interface ItemRepository extends ElasticsearchRepository<Item, Long> {
9+
10+
/**
11+
* 根据价格区间查询
12+
*
13+
* @param price1
14+
* @param price2
15+
* @return
16+
*/
17+
List<Item> findByPriceBetween(double price1, double price2);
18+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
spring:
2+
data:
3+
elasticsearch:
4+
cluster-name: elasticsearch
5+
cluster-nodes: 127.0.0.1:9300

0 commit comments

Comments
 (0)