一,es简介
Elasticsearch is a distributed, RESTful search and analytics engine capable of solving a growing number of use cases. (简单说:es是用来解决增量数据的一个分布式,使用Restful搜索和分析的引擎.)
二,es资料
官网:https://www.elastic.co/products/elasticsearch
ES中文指南: https://kibana.logstash.es/content/
三,使用Rest API简单CRUD实例
*ps:实例使用了https://my.oschina.net/liuchangng/blog/967788中的工具类,OkHttpUtil
import com.google.common.collect.Maps;
import com.lcj.okhttp.OkHttpUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.time.DateFormatUtils;
import java.util.Date;
import java.util.Map;
/**
* @author lcj
* @version 1.0
* @description 简单的使用rest api操作es
* @Create 2017-06-21
*/
public class EsRestApi {
/**
* 创建索引文档
*/
private static void createDocument(){
//http://192.168.0.128:9200/index/type/id
//method : put
String url = "http://192.168.0.128:9200/book/book/14";
Map<String,Object> book = Maps.newHashMap();
book.put("name","使用okhttp的put方式增加index");
book.put("author","lcj3");
book.put("price",50.23);
book.put("publishTime", DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
String result = OkHttpUtil.postOrPutOrDelete(url,book,null,OkHttpUtil.OkHttpMethod.PUT);
System.out.println(result);
}
/**
* 查询文档
*/
private static void getDocument(){
String url = "http://192.168.0.128:9200/book/book/14";
String result = OkHttpUtil.get(url,null,null);
System.out.println(result);
}
/**
* 更新文档
*/
private static void updateDocument(){
String url = "http://192.168.0.128:9200/book/book/14";
Map<String,Object> book = Maps.newHashMap();
book.put("author","lcj3");
book.put("price",5340.23);
String result = OkHttpUtil.postOrPutOrDelete(url,book,null,OkHttpUtil.OkHttpMethod.PUT);
System.out.println(result);
}
/**
* 删除文档
*/
private static void daleteDocument(){
String url = "http://192.168.0.128:9200/book/book/14";
String result = OkHttpUtil.postOrPutOrDelete(url,null,null,OkHttpUtil.OkHttpMethod.DELETE);
System.out.println(result);
}
public static void main(String[] args) {
//创建索引文档
//createDocument();
//查询文档
//getDocument();
//更新文档
//updateDocument();
//删除文档
daleteDocument();
}
}
网友评论