您的位置:首页 > 编程语言 > Java开发

elasticsearch?idea中如何创建springbootelasticsearch项目?

2019-07-23 21:16 375 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/Meet_again007/article/details/97035153

图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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: