elasticsearch 存在三种Java客户端。
org.elasticsearch.client
elasticsearch-rest-high-level-client
6.5.3
插入单条数据:
HttpHost : url地址封装
RestClientBuilder: rest客户端构建器
RestHighLevelClient: rest高级客户端
IndexRequest: 新增或修改请求
IndexResponse:新增或修改的响应结
package cn.laixueit.demo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ElasticSearchDemo {
public static void main(String[] args) throws IOException {
// 1 建立连接
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder clientBuilder = RestClient.builder(http);
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
// 2 发送数据
IndexRequest indexRequest = new IndexRequest("sku","doc","3");
Map skuMap = new HashMap();
skuMap.put("name","华为p30pro");
skuMap.put("brandName","华为");
skuMap.put("categoryName","手机");
skuMap.put("price",1010221);
skuMap.put("createTime","2019-05-01");
skuMap.put("saleNum",101021);
skuMap.put("commentNum",10102321);
Map spec=new HashMap();
spec.put("网络制式","移动4G");
spec.put("屏幕尺寸","5");
skuMap.put("spec",spec);
indexRequest.source(skuMap);
// 3 返回响应
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
int status = response.status().getStatus();
System.out.println(status);
}
}
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UBcZX0VM-1652341978453)(_v_images/20200618183844673_13211.png)]
如果ID不存在则新增,如果ID存在则修改。
批处理请求:
BulkRequest: 批量请求(用于增删改操作)
BulkResponse:批量请求(用于增删改操作)
修改测试:源代码修改字段属性继续提交
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-mc3woJSY-1652341978454)(_v_images/20200618184053908_1248.png)]
package cn.laixueit.demo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ElasticSearchDemo {
public static void main(String[] args) throws IOException {
// 1 建立连接
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder clientBuilder = RestClient.builder(http);
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
// 2 发送数据
BulkRequest bulkRequest = new BulkRequest();
IndexRequest indexRequest = new IndexRequest("sku","doc","5");
Map skuMap = new HashMap();
skuMap.put("name","华为p5");
skuMap.put("brandName","华为");
skuMap.put("categoryName","手机");
skuMap.put("price",1010221);
skuMap.put("createTime",new Date());
skuMap.put("saleNum",101021);
skuMap.put("commentNum",10102321);
Map spec=new HashMap();
spec.put("网络制式","移动4G");
spec.put("屏幕尺寸","5");
skuMap.put("spec",spec);
indexRequest.source(skuMap);
IndexRequest indexRequest2 = new IndexRequest("sku","doc","6");
Map skuMap2 = new HashMap();
skuMap2.put("name","华为p6 ");
skuMap2.put("brandName","华为");
skuMap2.put("categoryName","手机");
skuMap2.put("price",1010221);
skuMap2.put("createTime",new Date());
skuMap2.put("saleNum",101021);
skuMap2.put("commentNum",10102321);
Map spec2=new HashMap();
spec2.put("网络制式","移动4G");
spec2.put("屏幕尺寸","5");
skuMap2.put("spec",spec2);
indexRequest2.source(skuMap2);
bulkRequest.add(indexRequest);
bulkRequest.add(indexRequest2);
// 3 返回响应
// IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
int status = bulkResponse.status().getStatus();
System.out.println(status);
client.close();
}
}
SearchRequest: 查询请求对象
SearchResponse:查询响应对象
SearchSourceBuilder:查询源构建器
MatchQueryBuilder:匹配查询构建器
package cn.laixueit.demo;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Test2 {
public static void main(String[] args) throws IOException {
// 1 建立连接
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder clientBuilder = RestClient.builder(http);
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
// 2 发送数据
SearchRequest searchRequest = new SearchRequest("sku");
searchRequest.types("doc");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder queryBuilder = QueryBuilders.matchQuery("name", "小米");
SearchSourceBuilder query = sourceBuilder.query(queryBuilder);
searchRequest.source(sourceBuilder);
// 3 返回响应
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
Arrays.stream(hits).forEach((r)->{
System.out.println(r.getSourceAsString());
});
client.close();
}
}
BoolQueryBuilder:布尔查询构建器
TermQueryBuilder:词条查询构建器
QueryBuilders:查询构建器工厂
package cn.laixueit.demo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.Arrays;
/**
* BoolQueryBuilder:布尔查询构建器
* TermQueryBuilder:词条查询构建器
* QueryBuilders:查询构建器工厂
*/
public class Test3 {
public static void main(String[] args) throws IOException {
// 1 建立连接
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder clientBuilder = RestClient.builder(http);
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
// 2 发送数据
SearchRequest searchRequest = new SearchRequest("sku");
searchRequest.types("doc");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "华为");
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");
boolQuery.must(matchQueryBuilder);
boolQuery.must(termQueryBuilder);
searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);
// 3 返回响应
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
Arrays.stream(hits).forEach((r)->{
System.out.println(r.getSourceAsString());
});
client.close();
}
}
package cn.laixueit.demo;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
import java.util.Arrays;
public class Test4 {
public static void main(String[] args) throws IOException {
// 1 建立连接
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder clientBuilder = RestClient.builder(http);
RestHighLevelClient client = new RestHighLevelClient(clientBuilder);
// 2 发送数据
SearchRequest searchRequest = new SearchRequest("sku");
searchRequest.types("doc");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");
boolQuery.filter(termQueryBuilder);
searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);
// 3 返回响应
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHit[] hits = response.getHits().getHits();
Arrays.stream(hits).forEach((r)->{
System.out.println(r.getSourceAsString());
});
client.close();
}
}