Elasticsearch 接口介绍和java API实例

说明

  • 本博客是继上篇的扩展,整理介绍操作和官方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?prettyhttp://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 //使用了lombok,可以用手动引入private static final Logger log = LoggerFactory.getLogger(ESRestUtil.class);
public final class ESRestUtil {
	 
	    private RestHighLevelClient client;
	    
	    
	    public ESRestUtil() {
	        RestHighLevelClient client = new RestHighLevelClient(
	                RestClient.builder(
	                        //集群节点
//	                        new HttpHost("localhost", 9200, "http"),
	                        new HttpHost("localhost", 9200, "http")));
	        this.client = client;
	    }
	    
	    /**
	     * 关闭连接
	     */
	    public void shutdown(){
	        if(client!=null){
	            try {
	                client.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }
	    
	    /**
	     * 创建index
	     * @param index 创建index
	     * @param numberShared 分片数
	     * @param numberReplicas 备份数
	     * @return
	     */
	    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;
	    	
	    }
	    
	    //TODO 创建索引和规则
	    
	    /**
	     * 添加数据和修改
	     * 文档地址:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.10/java-rest-high-document-index.html
	     * @throws IOException
	     */
	    public boolean  addDataOrUpload(String index,String id,String jsonString)  {
	    	//指定索引
	    	IndexRequest request = new IndexRequest(index);
	    	//指定id
	    	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;
	    	//异步添加
//	    	ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>() {
//	    	    @Override
//	    	    public void onResponse(IndexResponse indexResponse) {
//	    	        
//	    	    }
//
//	    	    @Override
//	    	    public void onFailure(Exception e) {
//	    	        
//	    	    }
//	    	};
//	    	
//	    	client.indexAsync(request, RequestOptions.DEFAULT, listener);
	    }
	    
	 
	    /**
	     * 获取指定index和id的 数据
	     * @param index
	     * @param id
	     * @return
	     */
	    public Map<String, Object> getStringById(String index,String id){
	    	log.info("Search Index:{}  And Id:{}",index,id);
	        GetRequest getRequest = new GetRequest(index,id);  //index 和id
	        GetResponse response = null;
	        try {
	            response = this.client.get(getRequest, RequestOptions.DEFAULT);
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	        
	        //按字段Key获取对应的值
	        //DocumentField field = response.getField("content");
	        //获取全部的map,除了正常的值之外,会存在key是@version和@timestamp的值
	        Map<String, Object> source =  response.getSource();
	        
	        log.info("Result Is:"+JsonUtil.toJson(source));
	       return source;
	    }
	 
	    /**
	     * 根据指定的内容,模糊查询或精确查询所有Doc。
	     * @param content
	     * @return
	     */
	    public List<Map<String,Object>> getArticle(String index,String key,String content){
//	        QueryBuilder qb= QueryBuilders.boolQuery().must(QueryBuilders.termQuery("title","JAVA开发工程师")).must(QueryBuilders.termQuery("age",30)) ;
//	        //精确查询
	        QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery(key, content);
//	        //模糊查询
//	        QueryBuilder matchQueryBuilder = QueryBuilders.matchPhraseQuery("content", content);
	        
	        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//	        sourceBuilder.query(QueryBuilders.termQuery("content", content));
	        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;
	    }
	    
	    /**
	     * 模糊查询
	     * @param index
	     * @param key
	     * @param value
	     * @return
	     */
	    public SearchHits search(String index,String key,String value){
	        QueryBuilder matchQueryBuilder = QueryBuilders.matchPhraseQuery(key, value);
	        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
	        
//	        sourceBuilder.query(QueryBuilders.termQuery("content", content));
	        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;
	    }
	 
	    /**
	     * 删除数据
	     * @param titleName
	     * @return
	     */
	    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);
	        //deleteAsync(searchHits);
	        System.out.println("Exxcute Done" );
	        return deleteNum;
	 
	    }
	 
	    /**
	     * 删除数据
	     * @param index
	     * @param id
	     */
	    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;
	    	
	    }
	    
	    /**
	     * 正常删除
	     * @param searchHits
	     */
	    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();
	            }
	        }
	    }
	 
	    /**
	     * 异步删除
	     * @param searchHits
	     */
	    @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都有两种形式,同步和异步,系统执行效率要求高的可考虑异步接口,同样业务流程随之增大。
  • 说点废话,有用共勉,无用忽略。
    • 程序员个人成长,第一步掌握一门编程语言,掌握的标准是,能用该语言完成任何需求,不考虑执行效率。第二步,实现基础上优化代码,大幅提升效率。第三步,考虑功能分析和架构、功能设计,评估不同编程语言的特性和技术可控性、扩展性,能为项目搭建和开发提供有效的技术架构和流程方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值