Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6561216
  • 博文数量: 1005
  • 博客积分: 8199
  • 博客等级: 中将
  • 技术积分: 13071
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-25 20:19
个人简介

脚踏实地、勇往直前!

文章分类

全部博文(1005)

文章存档

2020年(2)

2019年(93)

2018年(208)

2017年(81)

2016年(49)

2015年(50)

2014年(170)

2013年(52)

2012年(177)

2011年(93)

2010年(30)

分类: Java

2018-12-01 22:52:56


1.创建maven项目
pom.xml文件内容如下

  1. <project xmlns="" xmlns:xsi=""
  2.     xsi:schemaLocation=" ">
  3.     <modelVersion>4.0.0</modelVersion>
  4.     <groupId>estest</groupId>
  5.     <artifactId>estest</artifactId>
  6.     <version>0.0.1-SNAPSHOT</version>
  7.     <dependencies>
  8.         <dependency>
  9.             <groupId>org.elasticsearch.client</groupId>
  10.             <artifactId>rest</artifactId>
  11.             <version>5.5.3</version>
  12.         </dependency>

  13.         <dependency>
  14.             <groupId>org.elasticsearch</groupId>
  15.             <artifactId>elasticsearch</artifactId>
  16.             <version>6.5.1</version>
  17.         </dependency>
  18.         <dependency>
  19.             <groupId>junit</groupId>
  20.             <artifactId>junit</artifactId>
  21.             <version>4.12</version>
  22.             <scope>test</scope>
  23.         </dependency>

  24.     </dependencies>

  25. </project>

2.java代码如下

  1. package com.hxl;

  2. import java.io.IOException;
  3. import java.util.Collections;
  4.  
  5. import org.apache.http.HttpEntity;
  6. import org.apache.http.HttpHost;
  7. import org.apache.http.auth.AuthScope;
  8. import org.apache.http.auth.UsernamePasswordCredentials;
  9. import org.apache.http.client.CredentialsProvider;
  10. import org.apache.http.entity.ContentType;
  11. import org.apache.http.impl.client.BasicCredentialsProvider;
  12. import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
  13. import org.apache.http.nio.entity.NStringEntity;
  14. import org.apache.http.util.EntityUtils;

  15. import org.elasticsearch.client.Response;
  16. import org.elasticsearch.client.RestClient;
  17. import org.elasticsearch.client.RestClientBuilder;
  18. import org.elasticsearch.index.query.QueryBuilder;
  19. import org.elasticsearch.index.query.QueryBuilders;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22.  
  23. /**
  24.  * Elasticserach RestClient示例
  25.  * @author fendo
  26.  *
  27.  */
  28. public class Rest {
  29.  
  30.     private static RestClient restClient;
  31.     
  32.     
  33.     
  34.     public void getRestClient(){
  35.         
  36.         final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  37.         credentialsProvider.setCredentials(AuthScope.ANY,
  38.                 new UsernamePasswordCredentials("elastic", "changeme"));
  39.     
  40.         restClient = RestClient.builder(new HttpHost("192.168.56.91",9200,"http"))
  41.                 .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
  42.                     public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
  43.                         return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
  44.                     }
  45.                 }).build();
  46.         
  47.     }
  48.     
  49.     @Before
  50.     public void getRest(){
  51.         restClient = RestClient.builder(new HttpHost("192.168.56.91", 9200, "http")).build();
  52.     }
  53.     
  54.  
  55.  
  56.     /**
  57.      * 查看api信息
  58.      * @throws Exception
  59.      */
  60.     @Test
  61.     public void CatApi() throws Exception{
  62.         String method = "GET";
  63.         String endpoint = "/_cat";
  64.         Response response = restClient.performRequest(method,endpoint);
  65.         System.out.println(EntityUtils.toString(response.getEntity()));
  66.     }
  67.     
  68.     /**
  69.      * 创建索引
  70.      * @throws Exception
  71.      */
  72.     @Test
  73.     public void CreateIndex() throws Exception{
  74.         String method = "PUT";
  75.         String endpoint = "/db_dev";
  76.         Response response = restClient.performRequest(method,endpoint);
  77.         System.out.println(EntityUtils.toString(response.getEntity()));
  78.     }
  79.     
  80.     /**
  81.      * 创建文档
  82.      * @throws Exception
  83.      */
  84.     @Test
  85.     public void CreateDocument()throws Exception{
  86.  
  87.         String method = "PUT";
  88.         String endpoint = "/db_dev/tb_test/1";
  89.         HttpEntity entity = new NStringEntity(
  90.                 "{\n" +
  91.                         " \"user\" : \"sdrtest\",\n" +
  92.                         " \"post_date\" : \"2018-12-01T14:12:12\",\n" +
  93.                         " \"message\" : \"sdr_test_Elasticsearch\"\n" +
  94.                         "}", ContentType.APPLICATION_JSON);
  95.  
  96.         Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
  97.         System.out.println(EntityUtils.toString(response.getEntity()));
  98.     }
  99.     
  100.     /**
  101.      * 获取文档
  102.      * @throws Exception
  103.      */
  104.     @Test
  105.     public void getDocument()throws Exception{
  106.         String method = "GET";
  107.         String endpoint = "/db_dev/tb_test/1";
  108.         Response response = restClient.performRequest(method,endpoint);
  109.         System.out.println(EntityUtils.toString(response.getEntity()));
  110.     }
  111.     
  112.     
  113.     /**
  114.      * 查询所有数据
  115.      * @throws Exception
  116.      */
  117.     @Test
  118.     public void QueryAll() throws Exception {
  119.         String method = "POST";
  120.         String endpoint = "/db_dev/tb_test/_search";
  121.         HttpEntity entity = new NStringEntity("{\n" +
  122.                 " \"query\": {\n" +
  123.                 " \"match_all\": {}\n" +
  124.                 " }\n" +
  125.                 "}", ContentType.APPLICATION_JSON);
  126.  
  127.         Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
  128.         System.out.println(EntityUtils.toString(response.getEntity()));
  129.     }
  130.     
  131.     /**
  132.      * 根据ID获取
  133.      * @throws Exception
  134.      */
  135.     @Test
  136.     public void QueryByField() throws Exception {
  137.         String method = "POST";
  138.         String endpoint = "/db_dev/tb_test/_search";
  139.         HttpEntity entity = new NStringEntity("{\n" +
  140.                 " \"query\": {\n" +
  141.                 " \"match\": {\n" +
  142.                 " \"user\": \"threegene_update\"\n" +
  143.                 " }\n" +
  144.                 " }\n" +
  145.                 "}", ContentType.APPLICATION_JSON);
  146.  
  147.         Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
  148.         System.out.println(EntityUtils.toString(response.getEntity()));
  149.     }
  150.     
  151.     /**
  152.      * 更新数据
  153.      * @throws Exception
  154.      */
  155.     @Test
  156.     public void UpdateByScript() throws Exception {
  157.         String method = "POST";
  158.         String endpoint = "/db_dev/tb_test/1/_update";
  159.         HttpEntity entity = new NStringEntity("{\n" +
  160.                 " \"doc\": {\n" +
  161.                 " \"user\":\"threegene_update\"\n" +
  162.                 "    }\n" +
  163.                 "}", ContentType.APPLICATION_JSON);
  164.         Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
  165.         System.out.println(EntityUtils.toString(response.getEntity()));
  166.     }
  167.     
  168.     
  169.     @Test
  170.     public void GeoBoundingBox() throws IOException {
  171.         String method = "POST";
  172.         String endpoint = "/db_dev/tb_test/_search";
  173.         HttpEntity entity = new NStringEntity("{\n" +
  174.                 " \"query\": {\n" +
  175.                 " \"match_all\": {}\n" +
  176.                 " },\n" +
  177.                 " \"post_filter\": {\n" +
  178.                 " \"geo_bounding_box\": {\n" +
  179.                 " \"location\": {\n" +
  180.                 " \"top_left\": {\n" +
  181.                 " \"lat\": 39.990481,\n" +
  182.                 " \"lon\": 116.277144\n" +
  183.                 " },\n" +
  184.                 " \"bottom_right\": {\n" +
  185.                 " \"lat\": 39.927323,\n" +
  186.                 " \"lon\": 116.405638\n" +
  187.                 " }\n" +
  188.                 " }\n" +
  189.                 " }\n" +
  190.                 " }\n" +
  191.                 "}", ContentType.APPLICATION_JSON);
  192.         Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
  193.         System.out.println(EntityUtils.toString(response.getEntity()));
  194.     }
  195. }


阅读(3137) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~