Spring boot集成Elasticsearch之Jest

Jest介绍
Jest是Elasticsearch 的Java Http Rest 客户端。
ElasticSearch已经具备应用于Elasticsearch内部的Java API,但是Jest弥补了ES自有API缺少Elasticsearch Http Rest接口客户端的不足。
在pom.xml中添加依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--导入Jest依赖-->
<dependency>
	<groupId>io.searchbox</groupId>
	<artifactId>jest</artifactId>
	<version>6.3.1</version>
</dependency>

配置Jest属性

#配置jest
spring.elasticsearch.jest.uris=http://192.168.25.128:9200

更多的属性配置可以查看JestProperties类
在这里插入图片描述
JestClient的操作测试

  • 索引文档
public class SpringbootuniteelasticsearchApplicationTests {
	@Autowired
	private JestClient jestClient;
	@Test
	public void indexDocument() {
		/*
		* ElasticSerach使用JSON作为文档的序列化格式
		* new Index.Build(source) source:可以是JSON串、Map<Key,Value>、Pojo
		*/
		Book book = new Book();
		book.setId(2);
		book.setName("ElasticSearch权威指南");
		book.setPrice(79.99);
		book.setDescription("Elasticsearch是一个实时分布式搜索和分析引擎。它让你以前所未有的速度处理大数据成为可能");
		//索引数据
		Index index = new Index.Builder(book).
							index("books").
							type("book").
							id("2").//手动指定文档ID
							build();
		try {
			jestClient.execute(index);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

查看被索引到文档
在这里插入图片描述

  • 查询文档
    搜索查询可以是JSON字符串,也可以是由SearchSourceBuilder来组建查询条件
    查询结果可以被转换为List
@Test
	public void searchDocument() {
		//查询所有
		String queryJson = "{\n" +
				"   \"query\" : {\n" +
				"      \"match_all\" : {}\n" +
				"   }\n" +
				"}\n";
		//使用JSON串来查询
		Search search = new Search.Builder(queryJson)
						//可以添加多个index和type
						.addIndex("books")
						.addType("book")
						.build();
		try {
			SearchResult result = jestClient.execute(search);
			List<Book> books= result.getSourceAsObjectList(Book.class, false);
			books.stream().forEach(System.out::println);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

查询效果展示
在这里插入图片描述

@Test
	public void searchSourceBuilder() {
		//使用SearchSourceBuilder来组建查询条件
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
		//在description域上查询匹配Elasticsearch的所有文档
		searchSourceBuilder.query(QueryBuilders.matchQuery("description", "Elasticsearch"));
		Search search = new Search.Builder(searchSourceBuilder.toString())
				//可以添加多个index和type
				.addIndex("books")
				.addType("book")
				.build();
		try {
			SearchResult result = jestClient.execute(search);
			List<Book> books= result.getSourceAsObjectList(Book.class, false);
			books.stream().forEach(System.out::println);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
  • 批量操作
    Elasticsearch的批量API使得在一个API调用中执行许多索引/删除操作成为可能。这可以极大地提高索引速度。
@Autowired
	private BookMapper bookMapper;
	@Test
	public void bulkOperations() {
		//从数据库中查询所有的Book
		List<Book> books = bookMapper.selectByExample(null);
		List<Index> indexs = new ArrayList<>();
		for (Book book : books) {
			indexs.add(new Index.Builder(book).id(book.getId()+"").build());
		}
		Bulk bulk = new Bulk.Builder()
				.defaultIndex("books")
				.defaultType("book")
				//List of objects can be indexed via bulk api
				.addAction(indexs)
				.build();
		try {
			jestClient.execute(bulk);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

批量操作的效果
在这里插入图片描述

更多的Jest操作可以参考Jest官网文档

猜你喜欢

转载自blog.csdn.net/qq_27046951/article/details/82795297