ElasticSearch6.x 基于SpringBoot 实现ElasticSearch的文档管理

前景提要:

        ElasticSearch6.x 搜索引擎服务支持四种方式导入文档对象:

        1、byte[]:字节数组(JSON格式文档手动转换为 byte[]或String)

        2、Map:键值对(使用Map,代表一个JSON文档)

        3、实体对象:JavaBean(使用Jackson 等第三方库把JavaBean转换为JSON)

        4、内置帮助类XContentFactory(使用内置帮助类XContentFactory的.jsonBuilder()方法)

 

SpringBoot 功能封装涉及ElasticSearch的文档管理方法约定如下:

createJACKSONDocument(String index, String type, byte[] bytes):新建文档,基于byte[]

createJSONDocument(String index, String type, String json):新建文档,基于json

createMapDocument(String index, String type, Map map):新建文档,基于map

createXContentBuilderDocument(String index, String type, XContentBuilder builder):新建文档,基于XContentBuilder

 

在上一篇文中说到:ElasticSearch6.x 基于SpringBoot 实现ElasticSearch连接功能封装,将约定的方法填充到ElasticSearchIndexUtil.java 工具类中。

@Component
public class ElasticSearchIndexUtil {
	// 引入 Ela 连接实列化对象
	@Autowired
	private TransportClient client;

    /**
	 * 功能描述:创建文档
	 * 
	 * @param index
	 *            索引名
	 * @param type
	 *            类型
	 * @param map
	 *            文档数据源
	 */
	public Result createMapDocument(String index, String type, Map map) {
		IndexResponse response = client.prepareIndex(index, type).setSource(map).get();
		return response.getResult();
	}

	/**
	 * 功能描述:创建文档
	 * 
	 * @param index
	 *            索引名
	 * @param type
	 *            类型
	 * @param json
	 *            文档数据源
	 */
	public Result createJSONDocument(String index, String type, String json) {
		IndexResponse response = client.prepareIndex(index, type).setSource(json).get();
		return response.getResult();

	}

	/**
	 * 功能描述:创建文档
	 * 
	 * @param index
	 *            索引名
	 * @param type
	 *            类型
	 * @param bytes
	 *            文档数据源
	 */
	public Result createJACKSONDocument(String index, String type, byte[] bytes) {
		IndexResponse response = client.prepareIndex(index, type).setSource(bytes).get();
		return response.getResult();
	}

	/**
	 * 功能描述:创建文档
	 * 
	 * @param index
	 *            索引名
	 * @param type
	 *            类型
	 * @param source
	 *            文档数据源
	 */
	public Result createXContentBuilderDocument(String index, String type, XContentBuilder builder) {
		IndexResponse response = client.prepareIndex(index, type).setSource(builder).get();
		return response.getResult();
	}


}

编写测试工具类Test.java ,测试相关封装的功能代码:

@RunWith(SpringRunner.class)
@SpringBootTest
// 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。
@WebAppConfiguration
public class Test {
	@Autowired
	private ElasticSearchIndexUtil util;

    @org.junit.Test
	public void createMapDocument() {
		Map<String, Object> source = new HashMap<String, Object>();
		source.put("user", "zzg");
		source.put("postDate", "2019-07-18");
		source.put("message", "trying out Elasticsearch");

		util.createMapDocument("book", "library", source);
	}

    /**其他索引功能方法请自行测试**/

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值