package com.pgy.controller;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.io.IOException;
import java.net.InetAddress;
import java.util.List;
public class ElasticHandler {
private TransportClient client;
public ElasticHandler() throws Exception {
Settings settings = Settings.builder().build();
client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.2.80"), 9300));
prepareData(client);
client.close();
}
private void prepareData(TransportClient client) throws IOException {
client.prepareIndex("company","employee","1")
.setSource(XContentFactory.jsonBuilder().startObject()
.field("name","jack") .field("age",28)
.endObject()).get();
}
}
经测试 可以使用
查询结果:
![](https://i-blog.csdnimg.cn/blog_migrate/45c6b9321ece9ac02b891d04d962bdf7.png)
其他方法后续更新