Elasticsearch restful api
2016-09-27 00:21
435 查看
之前看了,通过Elasticsearch的客户端调用,发现这样的方式繁琐所,那个客户端可能存在着版本的问题,所以显示要是能屏蔽掉这种差异性就是一个好的方案。发现用restful
api 完美解决了这个问题。而且调用起来比较简单。
先介绍一下这些api:
创建索引:xpost : http://ip:端口/索引名/?pretty
返回值为:{ "acknowledged" : true } 表示创建成功,如果创建过:{ "error" : "IndexAlreadyExistsException[[索引名] already exists]","status" : 400} 就报错
查询:http://ip:端口/索引名/_search?q=*&pretty json参数:{"query":{"match_all":{}}}这个是查询全部。其他的自己查了。这个适合复杂查询,简单查询用:、
http://ip:端口/索引名/_search?字段名=字段值,这样就可以查询到合适的结果。如果不加字段酒是查询全部。
删除索引:delete http://ip:端口/索引名/?pretty 这样就可以删了:返回值为:{ "acknowledged" : true} , 如果索引不存在,返回{ "error" : "IndexMissingException[[索引名] missing]","status" : 404}
给索引添加数据:put http://ip:端口/索引名/type/索引id 把数据放上,就可以把数据添加到 elasticsearch中。返回值为{"_index":"索引名","_type":"type值","_id":"索引id","_version":1,"created":true} 可以通过判断created的值,就可以判断出是否创建成功。创建失败就是false.,也即是更新操作了。
删除 数据:delete http://ip:端口/索引名/type/索引id/ 返回值:{"found":true,"_index":"索引名","_type":"type值","_id":"索引id","_version":2}, found为true表示删除成功,false表示该数据不存在。
下面就是一些例子:
public static void main(String[] args) throws ClientProtocolException, IOException {
long timestart=System.currentTimeMillis();
httpClient = new DefaultHttpClient();
String url="http://localhost:9200/user/_search?id=1";
method = new HttpPost(url);
method.setHeader("Accept", "application/json");
Map map=new HashMap<String,Object>();
map.put("match_all",new HashMap<String,Object>() );
Map newmap=new HashMap<String,Object>();
newmap.put("query", map);
ObjectMapper mapper = new ObjectMapper();
String result=mapper.writeValueAsString(newmap);
System.out.println(result);
method.setEntity(new StringEntity(result));
//搜索
HttpResponse response = httpClient.execute(method);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Method failed:" + response.getStatusLine());
}
String body = EntityUtils.toString(response.getEntity());
System.out.println("body:======="+body);
System.out.println("time="+(System.currentTimeMillis()-timestart));
method = new HttpPost("http://localhost:9200/shop/?pretty");
method.setHeader("Accept", "application/json");
//创建索引
response = httpClient.execute(method);
String out=EntityUtils.toString( response.getEntity());
ObjectMapper obm=new ObjectMapper();
Map mapout=new HashMap<String,Object>();
mapout= obm.readValue(out, Map.class);
Set<String> keyset=mapout.keySet();
for (String string : keyset) {
System.out.println("key====="+string);
}
System.out.println("out======++++"+out);
//删除索引
HttpDelete methoddelete = new HttpDelete("http://localhost:9200/shop/?pretty");
response= httpClient.execute(methoddelete);
result=EntityUtils.toString( response.getEntity());
System.out.println("delete==========="+result);
String input="{\"Id\":1,\"Name\":\"ipad5\",\"prodDesc\":\"比你想的更强大\",\"show\":3}";
String puturl="http://localhost:9200/user/employee/4";
//添加数据
HttpPut put=new HttpPut(puturl);
put.setEntity(new StringEntity(input));
response= httpClient.execute(put);
String in=EntityUtils.toString(response.getEntity());
System.out.println("in=============="+in);
}
api 完美解决了这个问题。而且调用起来比较简单。
先介绍一下这些api:
创建索引:xpost : http://ip:端口/索引名/?pretty
返回值为:{ "acknowledged" : true } 表示创建成功,如果创建过:{ "error" : "IndexAlreadyExistsException[[索引名] already exists]","status" : 400} 就报错
查询:http://ip:端口/索引名/_search?q=*&pretty json参数:{"query":{"match_all":{}}}这个是查询全部。其他的自己查了。这个适合复杂查询,简单查询用:、
http://ip:端口/索引名/_search?字段名=字段值,这样就可以查询到合适的结果。如果不加字段酒是查询全部。
删除索引:delete http://ip:端口/索引名/?pretty 这样就可以删了:返回值为:{ "acknowledged" : true} , 如果索引不存在,返回{ "error" : "IndexMissingException[[索引名] missing]","status" : 404}
给索引添加数据:put http://ip:端口/索引名/type/索引id 把数据放上,就可以把数据添加到 elasticsearch中。返回值为{"_index":"索引名","_type":"type值","_id":"索引id","_version":1,"created":true} 可以通过判断created的值,就可以判断出是否创建成功。创建失败就是false.,也即是更新操作了。
删除 数据:delete http://ip:端口/索引名/type/索引id/ 返回值:{"found":true,"_index":"索引名","_type":"type值","_id":"索引id","_version":2}, found为true表示删除成功,false表示该数据不存在。
下面就是一些例子:
public static void main(String[] args) throws ClientProtocolException, IOException {
long timestart=System.currentTimeMillis();
httpClient = new DefaultHttpClient();
String url="http://localhost:9200/user/_search?id=1";
method = new HttpPost(url);
method.setHeader("Accept", "application/json");
Map map=new HashMap<String,Object>();
map.put("match_all",new HashMap<String,Object>() );
Map newmap=new HashMap<String,Object>();
newmap.put("query", map);
ObjectMapper mapper = new ObjectMapper();
String result=mapper.writeValueAsString(newmap);
System.out.println(result);
method.setEntity(new StringEntity(result));
//搜索
HttpResponse response = httpClient.execute(method);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
System.out.println("Method failed:" + response.getStatusLine());
}
String body = EntityUtils.toString(response.getEntity());
System.out.println("body:======="+body);
System.out.println("time="+(System.currentTimeMillis()-timestart));
method = new HttpPost("http://localhost:9200/shop/?pretty");
method.setHeader("Accept", "application/json");
//创建索引
response = httpClient.execute(method);
String out=EntityUtils.toString( response.getEntity());
ObjectMapper obm=new ObjectMapper();
Map mapout=new HashMap<String,Object>();
mapout= obm.readValue(out, Map.class);
Set<String> keyset=mapout.keySet();
for (String string : keyset) {
System.out.println("key====="+string);
}
System.out.println("out======++++"+out);
//删除索引
HttpDelete methoddelete = new HttpDelete("http://localhost:9200/shop/?pretty");
response= httpClient.execute(methoddelete);
result=EntityUtils.toString( response.getEntity());
System.out.println("delete==========="+result);
String input="{\"Id\":1,\"Name\":\"ipad5\",\"prodDesc\":\"比你想的更强大\",\"show\":3}";
String puturl="http://localhost:9200/user/employee/4";
//添加数据
HttpPut put=new HttpPut(puturl);
put.setEntity(new StringEntity(input));
response= httpClient.execute(put);
String in=EntityUtils.toString(response.getEntity());
System.out.println("in=============="+in);
}
相关文章推荐
- 关于《网站规划与网页设计》的笔记
- 数据库信息批量导入到对象(c#)
- OBJECT ARX 修改选中实体的颜色 选择集的使用
- Apache伺服Django
- 在C#项目中需要用double类型操作MSSQL float类型数据(附C#数据类型和SQL数据类型对照)
- 【基础部分】之ISCSI的相关配置
- [原创]java WEB学习笔记65:Struts2 学习之路--Struts的CRUD操作( 查看 / 删除/ 添加) ModelDriven拦截器 paramter 拦截器
- Clang解析C++程序(2)
- EQueue 2.3.2版本发布(支持高可用)
- 计算机网络知识
- JsonHelper(C#)
- hive优化策略与架构优化
- Python--第4天 一大波金融Library来袭之scipy篇
- 搜狗输入法安装 ubuntu 14.04.5
- 第五章——指针与数组
- isConnected failed: ECONNREFUSED (Connection refused)
- C++ map
- HttpHelper代码(C#)
- 龙书D3D11章节习题答案(第十章)
- 1002,大数求和