1.安装elasticsearchjava
下载elasticsearchgit
docker pull registry.docker-cn.com/library/elasticsearch
运行elasticsearchgithub
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -p 9200:9200 -p 9300:9300 --name elasticsearch01 5acf0e8da90b
看到以下界面,则成功web
2.数据模拟spring
2.1postman模拟发送数据docker
2.2postman模拟检索数据(get请求)apache
2.3此外,delete请求是删除元素,head请求用来检查是否存在json
3.demo实现app
3.1pom文件elasticsearch
<?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>com.zy</groupId> <artifactId>spring-boot-elasticsearch-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-elasticsearch-demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.14.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> <!--SpringBoot默认使用SpringData ElasticSearch模块进行操做--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>5.3.3</version> </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> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.20</version> </dependency> <!-- 解决java.lang.ClassNotFoundException: com.sun.jna.Native问题 --> <dependency> <groupId>com.sun.jna</groupId> <artifactId>jna</artifactId> <version>3.0.9</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.2yml文件
spring: elasticsearch: jest: uris: http://192.168.0.100:9200
3.3实体类
package com.zy.model; import io.searchbox.annotations.JestId; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor @Builder public class Article { @JestId private Integer id; private String author; private String title; private String content; }
3.4测试类
package com.zy; import com.zy.mapper.BookMapper; import com.zy.model.Article; import io.searchbox.client.JestClient; import io.searchbox.core.Index; import io.searchbox.core.Search; import io.searchbox.core.SearchResult; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.io.IOException; @RunWith(SpringRunner.class) @SpringBootTest public class SpringBootElasticsearchDemoApplicationTests { @Autowired JestClient jestClient; @Autowired BookMapper bookMapper; @Test // 执行成功后的访问方式 http://192.168.0.100:9200/book/news/1 public void contextLoads() { Article article = Article.builder() .id(1) .title("good news") .author("东野圭吾") .content("降价了") .build(); // 构建索引 Index index = new Index.Builder(article) .index("book") .type("news") .build(); // 执行索引 try { jestClient.execute(index); } catch (IOException e) { e.printStackTrace(); } } //测试搜索 @Test public void search(){ //查询表达式 String json ="{\n" + " \"query\" : {\n" + " \"match\" : {\n" + " \"content\" : \"了\"\n" + " }\n" + " }\n" + "}"; //更多操做:https://github.com/searchbox-io/Jest/tree/master/jest //构建搜索功能 Search search = new Search.Builder(json) .addIndex("book") .addType("news") .build(); //执行 try { SearchResult searchResult = jestClient.execute(search); System.out.println(searchResult.getJsonString()); } catch (IOException e) { e.printStackTrace(); } } }