说明
- 本博客是继上篇的扩展,整理介绍操作和官方java API功能,并创建实例java maven项目,记性实际操作。
http
创建索引
- put方式请求:
http://127.0.0.1:9200/索引名
查看索引完整信息
- get请求:
http://172.0.0.1:9200/索引名?pretty
设置mapping
- put请求:
http://127.0.0.1:9200/索引名/_mappings
- 请求内容:
{
"properties" : {
"args" : {
"type" : "integer"
},
"createtime" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 20
}
}
},
"description" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "long"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 20
}
}
}
}
}
查看mapping
- 查看指定索引Mapping,get请求:
http://127.0.0.1:9200/索引名/_mapping?pretty
- 查看所有索引mapping,get请求:
http://127.0.0.1:9200/_all/_mapping?pretty
或http://127.0.0.1:9200/_mapping?pretty
- 查看指定索引中指定字段Mapping,get请求:
http://127.0.0.1:9200/索引名/_mapping/field/字段名?pretty
,多个索引和字段可逗号分隔 - pretty是让结果json格式化
删除索引
- delete请求:
http://127.0.0.1:9200/索引名
查看所有索引
- get请求:
http://127.0.0.1:9200/_cat/indices
查看索引所有数据
- post请求:
http://127.0.0.1:9200/索引名/_search
- 请求参数:
{
"query":{
"match_all":{}
}
}
搜索文档
- post请求:
http://127.0.0.1:9200/索引名/_search
- 请求内容,请求字段名为name,映射字段keyword是张三的数据
{
"query": {
"term": {
"name.keyword": "张三"
}
}
}
官方java API文档
- 以下都是我从官方一个个阅读整理的,全部基于RestHightLevelClient,TransportClient 8版本后删除。
文档详情
java API代码
查询类说明
- RangeQueryBuilder 范围查找 gt:大于 lt:小于 gte:大于等于 lte:小于等于
- WildcardQueryBuilder 模糊查询 使用*表示任意字符
- TermQueryBuilder 精确查询
- BoolQueryBuilder 布尔类型查询 用于多条件的拼接(可用使用以下三个方法进行拼接条件)
- must 必须的 文档必须满足匹配条件
- must_not 文档必须不匹配条件
- should 或者 文档只要满足一个或多个条件
导包
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.3.0</version>
</dependency>
java工具类
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.http.HttpHost;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public final class ESRestUtil {
private RestHighLevelClient client;
public ESRestUtil() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")));
this.client = client;
}
public void shutdown(){
if(client!=null){
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public boolean createIndex(String index,int numberShared,int numberReplicas) {
CreateIndexRequest request = new CreateIndexRequest(index);
request.settings(Settings.builder()
.put("index.number_of_shards", numberShared)
.put("index.number_of_replicas", numberReplicas)
);
request.alias(new Alias(index+"_alias").filter(QueryBuilders.termQuery("user", "kimchy")));
try {
client.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
log.error(e.toString(),e);
return false;
}
return true;
}
public boolean addDataOrUpload(String index,String id,String jsonString) {
IndexRequest request = new IndexRequest(index);
request.id(id);
request.source(jsonString, XContentType.JSON);
IndexResponse indexResponse;
try {
indexResponse = client.index(request, RequestOptions.DEFAULT);
} catch (IOException e) {
log.error("添加数据失败"+e.toString(),e);
return false;
}
if(indexResponse.getResult()==DocWriteResponse.Result.CREATED) {
log.info("First Create");
}else if(indexResponse.getResult() ==DocWriteResponse.Result.UPDATED ) {
log.info("Data Update");
}
return true;
}
public Map<String, Object> getStringById(String index,String id){
log.info("Search Index:{} And Id:{}",index,id);
GetRequest getRequest = new GetRequest(index,id);
GetResponse response = null;
try {
response = this.client.get(getRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
Map<String, Object> source = response.getSource();
log.info("Result Is:"+JsonUtil.toJson(source));
return source;
}
public List<Map<String,Object>> getArticle(String index,String key,String content){
QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(key, content);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(index);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse;
List<Map<String,Object>> list = new ArrayList<>();
try {
searchResponse = this.client.search(searchRequest,RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
for(SearchHit hit:searchHits.getHits()){
System.out.println( hit.getSourceAsString());
list.add(hit.getSourceAsMap());
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
public SearchHits search(String index,String key,String value){
QueryBuilder matchQueryBuilder = QueryBuilders.matchPhraseQuery(key, value);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(matchQueryBuilder);
sourceBuilder.from(0);
sourceBuilder.size(100);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices(index);
searchRequest.source(sourceBuilder);
SearchResponse searchResponse;
List<Map<String,Object>> list = new ArrayList<>();
SearchHits searchHits = null;
try {
searchResponse = this.client.search(searchRequest,RequestOptions.DEFAULT);
searchHits = searchResponse.getHits();
for(SearchHit hit:searchHits.getHits()){
System.out.println( hit.getSourceAsString());
list.add(hit.getSourceAsMap());
}
} catch (IOException e) {
e.printStackTrace();
}
return searchHits;
}
public long deleteArticle(String titleName){
long deleteNum = 0l;
SearchHits searchHits = search("article-2019.08.08.03","title",titleName);
System.out.println("Exxcute Start" );
deleteCommon(searchHits);
System.out.println("Exxcute Done" );
return deleteNum;
}
public boolean deleteData(String index,String id) {
DeleteRequest deleteRequest= new DeleteRequest(index,id);
try {
DeleteResponse deleteResponse = this.client.delete(deleteRequest,RequestOptions.DEFAULT);
System.out.println("Delete Done【"+deleteResponse.getId()+"】,Status:【" + deleteResponse.status() + "】");
return true;
} catch (IOException e) {
log.error(e.toString(),e);
}
return false;
}
private void deleteCommon (SearchHits searchHits){
DeleteRequest deleteRequest = new DeleteRequest();
for(SearchHit hit:searchHits.getHits()) {
deleteRequest = new DeleteRequest("article-2019.08.08.03",hit.getId());
try {
DeleteResponse deleteResponse = this.client.delete(deleteRequest,RequestOptions.DEFAULT);
System.out.println("Delete Done【"+deleteResponse.getId()+"】,Status:【" + deleteResponse.status() + "】");
} catch (IOException e) {
e.printStackTrace();
}
}
}
@SuppressWarnings("unused")
private void deleteAsync (SearchHits searchHits) {
DeleteRequest deleteRequest = new DeleteRequest();
for(SearchHit hit:searchHits.getHits()){
deleteRequest = new DeleteRequest("article-2019.08.08.03",hit.getId());
this.client.deleteAsync(deleteRequest, RequestOptions.DEFAULT, new ActionListener<DeleteResponse>() {
@Override
public void onResponse(DeleteResponse deleteResponse) {
RestStatus restStatus = deleteResponse.status();
int status = restStatus.getStatus();
deleteResponse.getId();
System.out.println("Delete Done【"+deleteResponse.getId()+"】,Status:【" + status + "】");
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
System.out.println("ERROR " + hit.getId());
}
});
}
}
}
总结
- 官方java文档中,所有API都有两种形式,同步和异步,系统执行效率要求高的可考虑异步接口,同样业务流程随之增大。
- 说点废话,有用共勉,无用忽略。
- 程序员个人成长,第一步掌握一门编程语言,掌握的标准是,能用该语言完成任何需求,不考虑执行效率。第二步,实现基础上优化代码,大幅提升效率。第三步,考虑功能分析和架构、功能设计,评估不同编程语言的特性和技术可控性、扩展性,能为项目搭建和开发提供有效的技术架构和流程方案。