图1

创建下图所示的东西

User 内容:
package com.zhiyou100.springbootelaticsearch.entity;
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;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "user",type = "aaa")
public class User {
@Id
@Field(type = FieldType.Integer)
private Integer id;
@Field(type = FieldType.Text)
private String account;
@Field(type = FieldType.Keyword)
private String password;
}
UserRepository内容:
package com.zhiyou100.springbootelaticsearch.Repository;
import com.zhiyou100.springbootelaticsearch.entity.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;
public interface UserRepository extends ElasticsearchRepository<User,Integer> {
//根据账号查询
List<User> findByAccount(String account);
}
application.properties:
spring.profiles.active=dev
application-dev.yml:
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.14.167:9300
UserRepositoryTest内容
package com.zhiyou100.springbootelaticsearch.Repository;
import com.zhiyou100.springbootelaticsearch.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTest {
@Autowired
private UserRepository userRepository;
@Test
public void addUser() {
//测试添加
User user = new User((int) (Math.random() * 200000 + 1), "昊天至尊", "0000");
userRepository.save(user);
System.out.println(user);
}
@Test
public void testFindAll(){
Iterable<User> users = userRepository.findAll();
users.forEach(System.out::println);
}
@Test
public void testFingByAccount(){
List<User> users = userRepository.findByAccount("昊天至尊");
users.forEach(System.out::println);
}
}
pox.xml:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.zhiyou100</groupId>
<artifactId>spring-boot-elaticsearch</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-elaticsearch</name>
<description>Demo project for Spring Boot</description>
<properties>
<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.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>