java中创建索引的方法:首先把对象转换为JSON字符串;然后把json文档写入索引;最后使用Java代码新建一个Java项目,在其中写好创建索引代码调用就可以了。
import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.Date; import java.util.List; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import cn.com.bropen.entity.DataFactory; import static org.elasticsearch.common.xcontent.XContentFactory.*; public class ElasticSearchHandler { public static void main(String[] args) { try { /* 创建客户端 */ // client startup Client client = TransportClient.builder().build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); ListjsonData = DataFactory.getInitJsonData(); for (int i = 0; i < jsonData.size(); i++) { IndexResponse response = client.prepareIndex("blog", "article").setSource(jsonData.get(i)).get(); if (response.isCreated()) { System.out.println("创建成功!"); } } client.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
以上就是java中怎么创建索引的详细内容,更多请关注其它相关文章!