您的位置:首页 > 其它

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);

        
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: