知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.client.Client; IndexResponse response = client.prepareIndex("information", "news", "1") .setSource("{ \"title\": \"ElasticSearch 5.5.2\"}") .get();
使用 HashMap 插入
public void createData() { Map<String, Object> map = new HashMap<String, Object>(); map.put("name", "Neo Chen"); map.put("age", 30); map.put("book", new String[]{"Netkiller Linux 手札","Netkiller Java 手札"}); map.put("website", "http://www.netkiller.cn"); map.put("email", "netkiller@msn.com"); IndexResponse response = client.prepareIndex("book", "author", UUID.randomUUID().toString()) .setSource(map).get(); System.out.println("Status: " + response.status().getStatus() + "!id=" + response.getId()); }