package com.fendo.RestClient;
import java.io.IOException;
import java.util.Collections;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.entity.ContentType;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Before;
import org.junit.Test;
/**
* Elasticserach RestClient示例
* @author fendo
*
*/
public class Rest {
private static RestClient restClient;
public void getRestClient(){
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials("elastic", "changeme"));
restClient = RestClient.builder(new HttpHost("localhost",9200,"http"))
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
}
}).build();
}
@Before
public void getRest(){
restClient = RestClient.builder(new HttpHost("localhost", 9200, "http")).build();
}
/**
* 查看api信息
* @throws Exception
*/
@Test
public void CatApi() throws Exception{
String method = "GET";
String endpoint = "/_cat";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 创建索引
* @throws Exception
*/
@Test
public void CreateIndex() throws Exception{
String method = "PUT";
String endpoint = "/test-index";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 创建文档
* @throws Exception
*/
@Test
public void CreateDocument()throws Exception{
String method = "PUT";
String endpoint = "/test-index/test/1";
HttpEntity entity = new NStringEntity(
"{\n" +
" \"user\" : \"kimchy\",\n" +
" \"post_date\" : \"2009-11-15T14:12:12\",\n" +
" \"message\" : \"trying out Elasticsearch\"\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 获取文档
* @throws Exception
*/
@Test
public void getDocument()throws Exception{
String method = "GET";
String endpoint = "/test-index/test/1";
Response response = restClient.performRequest(method,endpoint);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 查询所有数据
* @throws Exception
*/
@Test
public void QueryAll() throws Exception {
String method = "POST";
String endpoint = "/test-index/test/_search";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 根据ID获取
* @throws Exception
*/
@Test
public void QueryByField() throws Exception {
String method = "POST";
String endpoint = "/test-index/test/_search";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match\": {\n" +
" \"user\": \"kimchy\"\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
/**
* 更新数据
* @throws Exception
*/
@Test
public void UpdateByScript() throws Exception {
String method = "POST";
String endpoint = "/test-index/test/1/_update";
HttpEntity entity = new NStringEntity("{\n" +
" \"doc\": {\n" +
" \"user\":\"大美女\"\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
@Test
public void GeoBoundingBox() throws IOException {
String method = "POST";
String endpoint = "/attractions/restaurant/_search";
HttpEntity entity = new NStringEntity("{\n" +
" \"query\": {\n" +
" \"match_all\": {}\n" +
" },\n" +
" \"post_filter\": {\n" +
" \"geo_bounding_box\": {\n" +
" \"location\": {\n" +
" \"top_left\": {\n" +
" \"lat\": 39.990481,\n" +
" \"lon\": 116.277144\n" +
" },\n" +
" \"bottom_right\": {\n" +
" \"lat\": 39.927323,\n" +
" \"lon\": 116.405638\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}", ContentType.APPLICATION_JSON);
Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
System.out.println(EntityUtils.toString(response.getEntity()));
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Elasticsearch5.5.1 JAVA客户端RestClient示例
共14个文件
class:3个
prefs:3个
java:2个
5星 · 超过95%的资源 需积分: 50 190 下载量 8 浏览量
2017-09-05
17:09:47
上传
评论 2
收藏 14KB ZIP 举报
温馨提示
Elasticsearch5.5.1 JAVA客户端RestClient示例代码,详细介绍:http://blog.csdn.net/u011781521/article/details/77853571
资源推荐
资源详情
资源评论
收起资源包目录
RestClient.zip (14个子文件)
RestClient
.project 562B
src
test
java
com
fendo
RestClient
AppTest.java 686B
main
java
com
fendo
RestClient
Rest.java 7KB
target
classes
com
fendo
RestClient
Rest.class 5KB
Rest$1.class 1KB
META-INF
MANIFEST.MF 107B
maven
com.fendo
RestClient
pom.properties 244B
pom.xml 2KB
test-classes
com
fendo
RestClient
AppTest.class 627B
.settings
org.eclipse.m2e.core.prefs 90B
org.eclipse.jdt.core.prefs 736B
org.eclipse.core.resources.prefs 119B
pom.xml 2KB
.classpath 1024B
共 14 条
- 1
码农致富
- 粉丝: 3670
- 资源: 112
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
前往页