- 浏览: 79096 次
文章分类
最新评论
如何实现java操作ES(jestclient)
ES原始的操作代码:
创建索引
put http://127.168.30.48:44186/app_info_index
获取索引定义信息
GET http://127.168.30.48:44186/app_info_index/_mapping
创建typeName,并定义字段属性
POST http://127.168.30.48:44186/app_info_index/se_app_info/_mapping?pretty
{"se_app_info":{"properties":{"addTicket":{"type":"text"},"appDistributePlat":{"type":"text"},"appFeeInfo":{"type":"text"},"appId":{"type":"text"},"appInfoExpandInfo":{"type":"text"},"appKey":{"type":"text"},"appName":{"type":"text"},"appType":{"type":"text"},"appTypeProperty":{"type":"text"},"areaLevel":{"type":"text"},"categoryId":{"type":"text"},"chargeMode":{"type":"text"},"comefrom":{"type":"text"},"createTime":{"type":"long"},"delStatus":{"type":"text"},"description":{"type":"text"},"orgId":{"type":"text"},"platformcode":{"type":"text"},"providerId":{"type":"text"},"providerName":{"type":"text"},"schoolId":{"type":"text"},"schoolName":{"type":"text"},"showPlat":{"type":"text"},"status":{"type":"text"},"thumbnailimage":{"type":"text"},"updateTime":{"type":"long"},"userIdentity":{"type":"text"},"windowOption":{"type":"text"}}}}
查看分词器分词规则
http://127.168.30.48:44186/_analyze
{
"analyzer": "standard",
"text": "teacher and student"
}
java首先应用对应的jar
<!--spring整合elasticsearch包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
</dependency>
</dependencies>
下面附上jestclient操作ES代码
package com.edu.whty.opendspportal.es;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.client.JestResultHandler;
import io.searchbox.core.Bulk;
import io.searchbox.core.BulkResult;
import io.searchbox.core.Delete;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;
import io.searchbox.indices.ClearCache;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.DeleteIndex;
import io.searchbox.indices.IndicesExists;
import io.searchbox.indices.Optimize;
import io.searchbox.indices.mapping.GetMapping;
import io.searchbox.indices.mapping.PutMapping;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.edu.whty.esBean.app.SeAppInfoEsBean;
import com.edu.whty.esBean.app.SeAppInfoExpandEsBean;
import com.edu.whty.fee.SeAppFeeInfo;
import com.edu.whty.opendspportal.OpenDspportalApplicationTests;
import com.edu.whty.service.appManage.AppService;
import com.edu.whty.utils.SystemParamUtil;
public class EsTest extends OpenDspportalApplicationTests{
private static String indexName = "app_info_index";
private static String typeName = "se_app_info";
@Autowired
private JestClient jestClient;
@Autowired
private AppService appService;
//创建索引
@Test
public void createIndex(){
try{
// IndicesExists indexExist = new IndicesExists.Builder(indexName).build();
// JestResult result = jestClient.execute(indexExist);
// System.out.println(result.toString());
// Object indexFound = result.getValue("found");
// if(indexFound != null && indexFound.toString().equals("false")){
//第一步 JAVA jestClient创建索引
JestResult jr = jestClient.execute(new CreateIndex.Builder(indexName).build());
System.out.println("ES创建索引:"+jr);
// }
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 删除索引
* @return
* @throws Exception
*/
@Test
public void deleteIndex(){
try{
JestResult jr = jestClient.execute(new DeleteIndex.Builder(indexName).build());
System.out.println(jr.isSucceeded());
}catch(Exception e){
e.printStackTrace();
}
}
//新增一笔信息
@Test
public void addOneInfo(){
try{
String appId="AP0000001";
SeAppInfoEsBean seAppInfoEsBean = appService.findSeAppInfoByAppIdTwo(appId);
seAppInfoEsBean.setCreateTime(seAppInfoEsBean.getCreateTimeDate().getTime());
seAppInfoEsBean.setUpdateTime(seAppInfoEsBean.getUpdateTimeDate().getTime());
String oldImgUrl = SystemParamUtil.getParamValue("oldImgUrl");//这个用来处理没有http的图片地址
String thumbnailimage = seAppInfoEsBean.getThumbnailimage();
if(StringUtils.isNotBlank(thumbnailimage) && thumbnailimage.indexOf("http")<0){
thumbnailimage = oldImgUrl+thumbnailimage;
seAppInfoEsBean.setThumbnailimage(thumbnailimage);
}
if(seAppInfoEsBean.getStatus().equals("10") || seAppInfoEsBean.getStatus().equals("12")){
seAppInfoEsBean.setStatus("1");
}else{
seAppInfoEsBean.setStatus("0");
}
//获取应用拓展信息
List<SeAppInfoExpandEsBean> appExpandList = appService.findSeAppInfoExpandByAppIdTwo(appId);
if(appExpandList!=null && appExpandList.size()>0){
for(SeAppInfoExpandEsBean expand:appExpandList){
String img_one = expand.getImgOne();
if(StringUtils.isNotBlank(img_one) && img_one.indexOf("http")<0){
img_one = oldImgUrl+img_one;
expand.setImgOne(img_one);
}
String img_two = expand.getImgTwo();
if(StringUtils.isNotBlank(img_two) && img_two.indexOf("http")<0){
img_two = oldImgUrl+img_two;
expand.setImgTwo(img_two);
}
String img_three = expand.getImgThree();
if(StringUtils.isNotBlank(img_three) && img_three.indexOf("http")<0){
img_three = oldImgUrl+img_three;
expand.setImgThree(img_three);
}
String img_four = expand.getImgFour();
if(StringUtils.isNotBlank(img_four) && img_four.indexOf("http")<0){
img_four = oldImgUrl+img_four;
expand.setImgFour(img_four);
}
String img_five = expand.getImgFive();
if(StringUtils.isNotBlank(img_five) && img_five.indexOf("http")<0){
img_five = oldImgUrl+img_five;
expand.setImgFive(img_five);
}
}
String appInfoExpandInfo = JSONArray.toJSONString(appExpandList);
seAppInfoEsBean.setAppInfoExpandInfo(appInfoExpandInfo);
}else{
seAppInfoEsBean.setAppInfoExpandInfo("");
}
//获取应用的费用信息
SeAppFeeInfo appFeeInfo=appService.findAppFeeInfoByAppId(appId);
if(appFeeInfo!=null){
String appFeeInfoStr = JSONObject.toJSONString(appFeeInfo);
seAppInfoEsBean.setAppFeeInfo(appFeeInfoStr);
}else{
seAppInfoEsBean.setAppFeeInfo("");
}
//获取应用分发平台信息
List<Map<String,String>> disPlatList = appService.findDistributePlatInfo(appId);
if(disPlatList!=null && disPlatList.size()>0){
String appDistributePlat = JSONArray.toJSONString(disPlatList);
seAppInfoEsBean.setAppDistributePlat(appDistributePlat);
}else{
seAppInfoEsBean.setAppDistributePlat("");
}
// System.out.println(JSONObject.toJSONString(seAppInfoEsBean));
Index index = new Index.Builder(seAppInfoEsBean).id(seAppInfoEsBean.getAppId()).index(indexName).type(typeName).build();
JestResult jr = jestClient.execute(index);
System.out.println("ES插入一笔记录"+jr.toString());
}catch(Exception e){
e.printStackTrace();
}
}
//查询索引下对应字段属性信息
@Test
public void getIndexMapping(){
try{
GetMapping getMapping = new GetMapping.Builder().addIndex(indexName).addType(typeName).build();
JestResult jr =jestClient.execute(getMapping);
System.out.println(jr.getJsonString());
}catch(Exception e){
e.printStackTrace();
}
}
//批量插入
@Test
public void batchInsert(){
try{
List<String> appIdList = new ArrayList<String>();
appIdList.add("0368D135CDC4998CCA2E18F78CA1B47C");
appIdList.add("08EEF178C00B6AE4C8600A698C3A3FF6");
appIdList.add("10B6191CD532");
appIdList.add("1617799e092549bcbdc84571d5d9c53f");
appIdList.add("1753FB1642C0A621FA63ED14CECA50A7");
appIdList.add("18199AF19243B58581B9456A04BB80BC");
appIdList.add("1ACA9A600575D0CB33180A5540F6BE90");
appIdList.add("1FDA0E5D4A068FC90F3817C391DDCC0B");
appIdList.add("2AD0F15C3DA27F31EDD1408219D1E638");
appIdList.add("2CEE34C2EA2867C10780F1FEC47FEC03");
appIdList.add("2DFBB1DC1D595D49910E7BDEEE458017");
Bulk.Builder bulk = new Bulk.Builder().defaultIndex(indexName).defaultType(typeName);
for(String appId:appIdList){
SeAppInfoEsBean seAppInfoAll = appService.findSeAppInfoByAppIdTwo(appId);
String oldImgUrl = SystemParamUtil.getParamValue("oldImgUrl");//这个用来处理没有http的图片地址
String thumbnailimage = seAppInfoAll.getThumbnailimage();
if(StringUtils.isNotBlank(thumbnailimage) && thumbnailimage.indexOf("http")<0){
thumbnailimage = oldImgUrl+thumbnailimage;
seAppInfoAll.setThumbnailimage(thumbnailimage);
}
if(seAppInfoAll.getStatus().equals("10") || seAppInfoAll.getStatus().equals("12")){
seAppInfoAll.setStatus("1");
}else{
seAppInfoAll.setStatus("0");
}
//获取应用拓展信息
List<SeAppInfoExpandEsBean> appExpandList = appService.findSeAppInfoExpandByAppIdTwo(appId);
if(appExpandList!=null && appExpandList.size()>0){
for(SeAppInfoExpandEsBean expand:appExpandList){
String img_one = expand.getImgOne();
if(StringUtils.isNotBlank(img_one) && img_one.indexOf("http")<0){
img_one = oldImgUrl+img_one;
expand.setImgOne(img_one);
}
String img_two = expand.getImgTwo();
if(StringUtils.isNotBlank(img_two) && img_two.indexOf("http")<0){
img_two = oldImgUrl+img_two;
expand.setImgTwo(img_two);
}
String img_three = expand.getImgThree();
if(StringUtils.isNotBlank(img_three) && img_three.indexOf("http")<0){
img_three = oldImgUrl+img_three;
expand.setImgThree(img_three);
}
String img_four = expand.getImgFour();
if(StringUtils.isNotBlank(img_four) && img_four.indexOf("http")<0){
img_four = oldImgUrl+img_four;
expand.setImgFour(img_four);
}
String img_five = expand.getImgFive();
if(StringUtils.isNotBlank(img_five) && img_five.indexOf("http")<0){
img_five = oldImgUrl+img_five;
expand.setImgFive(img_five);
}
}
String appInfoExpandInfo = JSONArray.toJSONString(appExpandList);
seAppInfoAll.setAppInfoExpandInfo(appInfoExpandInfo);
}else{
seAppInfoAll.setAppInfoExpandInfo("");
}
//获取应用的费用信息
SeAppFeeInfo appFeeInfo=appService.findAppFeeInfoByAppId(appId);
if(appFeeInfo!=null){
String appFeeInfoStr = JSONObject.toJSONString(appFeeInfo);
seAppInfoAll.setAppFeeInfo(appFeeInfoStr);
}else{
seAppInfoAll.setAppFeeInfo("");
}
//获取应用分发平台信息
List<Map<String,String>> disPlatList = appService.findDistributePlatInfo(appId);
if(disPlatList!=null && disPlatList.size()>0){
String appDistributePlat = JSONArray.toJSONString(disPlatList);
seAppInfoAll.setAppDistributePlat(appDistributePlat);
}else{
seAppInfoAll.setAppDistributePlat("");
}
Index index = new Index.Builder(seAppInfoAll).id(seAppInfoAll.getAppId()).build();
bulk.addAction(index);
}
BulkResult br = jestClient.execute(bulk.build());
System.out.println("批量插入操作:"+br.isSucceeded());
}catch(Exception e){
e.printStackTrace();
}
}
//分页查询
@Test
public void pageQuery(){
try{
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// searchSourceBuilder.from(0);
// searchSourceBuilder.size(10);
BoolQueryBuilder boolBuiler = QueryBuilders.boolQuery();//多条件查询
boolean bol = false;
if(bol){
searchSourceBuilder.query(QueryBuilders.matchAllQuery());//查询全部
}else{
// boolBuiler.must(QueryBuilders.termQuery("appId", "AP0000001"));//精确匹配
boolBuiler.must(QueryBuilders.matchQuery("appName", "一"));//模糊匹配
searchSourceBuilder.query(boolBuiler);//添加查询条件
}
String queryStr = searchSourceBuilder.toString();
System.out.println("ES查询条件:"+queryStr);
Search search=new Search.Builder(queryStr).addIndex(indexName).addType(typeName).build();
SearchResult result=jestClient.execute(search);
System.out.println(result.toString());
if(result.getErrorMessage()!=null && result.getErrorMessage().length()>0){//查询异常情况
System.out.println("Ng日志查询异常,报错信息:"+result.getErrorMessage());
}else{
List<Hit<SeAppInfoEsBean,Void>> list = result.getHits(SeAppInfoEsBean.class);
List<SeAppInfoEsBean> appList = new ArrayList<SeAppInfoEsBean>();
for(Hit<SeAppInfoEsBean,Void> hit:list){
String id = hit.id;
System.out.println("每条记录的索引ID:"+id);
appList.add(hit.source);
}
System.out.println("查询结果集大小:"+appList.size());
}
}catch(Exception e){
e.printStackTrace();
}
}
//按索引删除单笔数据
@Test
public void deleteData()throws Exception{
try{
String id = "AP0000001";
DocumentResult dr = jestClient.execute(new Delete.Builder(id).index(indexName).type(typeName).build());
System.out.println(dr.isSucceeded());
}catch(Exception e){
e.printStackTrace();
}
}
//索引优化
@Test
public void optimizeIndex() {
Optimize optimize = new Optimize.Builder().build();
jestClient.executeAsync(optimize, new JestResultHandler<JestResult>() {
public void completed(JestResult jestResult) {
System.out.println("optimizeIndex result:{}" + jestResult.isSucceeded());
}
public void failed(Exception e) {
e.printStackTrace();
}
});
}
//清理缓存
@Test
public void clearCache() {
try {
ClearCache clearCache = new ClearCache.Builder().build();
jestClient.execute(clearCache);
} catch (Exception e) {
e.printStackTrace();
}
}
}
转载于:https://my.oschina.net/u/4051450/blog/3072530
相关推荐
JestClient为Java开发者提供了便捷地操作Elasticsearch的途径,其简洁的API和高效的性能使得在Java环境中与Elasticsearch的交互变得轻松。通过理解和掌握以上知识点,你可以更加高效地利用Elasticsearch来构建和优化...
3. 执行操作:使用JestClient提供的接口执行各种Elasticsearch操作,如索引文档、搜索、更新、删除等。 4. 关闭资源:在完成操作后,记得关闭JestClient以释放资源。 Jest的优势在于其灵活性和性能。它可以很好地...
java操作ElasticSearch的工具类。需要添加依赖: <!-- ElasticSearch依赖 --> <groupId>io.searchbox <artifactId>jest <version>6.3.1 <!-- ...
Elasticsearch是一个强大的分布式搜索引擎,而Jest则是一个Java库,作为与Elasticsearch进行交互的客户端。让我们深入探讨这两个技术以及它们如何协同工作。 **Elasticsearch** Elasticsearch是一种基于Lucene的...
Elasticsearch5.5.1使用JAVA客户端Jest操作的一些示例代码,详细介绍请参考文章:http://blog.csdn.net/u011781521/article/details/77852861
在Java代码中,通过`Subject`对象和`LoginContext`来调用KerberosLoginModule进行登录操作。登录成功后,可以从`Subject`中获取到验证过的凭据,用于后续的授权和服务请求。 3. **示例代码** ```java import ...
通过学习和实践这些基本概念和操作,你将能熟练地使用Elasticsearch和Java Jest库构建自己的搜索引擎和分析系统。在实际项目中,还需要根据具体需求进行优化和调整,如集群配置、性能调优等。不断探索和学习,你将在...
Java与Elasticsearch(ES)交互是大数据处理和搜索引擎领域中的常见操作,这个"java_es交互工具类.rar"文件很可能是包含了一些Java编程中用于与Elasticsearch进行数据操作的工具类。在Java中,与Elasticsearch进行...
`Jest-master` 是 Elasticsearch Java REST 客户端的一种实现,它是一个轻量级的库,允许 Java 应用程序与 Elasticsearch 集群进行通信。Jest 使用了 Apache HttpClient 作为底层 HTTP 客户端,支持 JSON 序列化和反...
然后,你可以创建一个JestClient实例来连接Elasticsearch集群,并执行索引、搜索等操作。 2. **Java通过JDBC连接Hive** 要从Hive获取数据,我们可以使用Hive JDBC驱动。首先,确保你的项目中包含Hive JDBC驱动的...
在IT行业中,Es(Elasticsearch)是一种...总结起来,JEST与ES的集成使Java开发者能够方便地对Elasticsearch进行操作,实现高效的数据检索和管理。理解并熟练掌握这些基本操作是开发基于Elasticsearch的应用的基础。
在Java中,与Elasticsearch交互通常通过JEST或Transport Client库进行。这个类可能封装了初始化Elasticsearch客户端、构建查询语句、执行搜索请求以及处理返回结果的过程。查询功能可能包括了全文搜索、过滤、排序和...
2. **src/main/java**:源代码目录,可能包含一个或多个Java类,这些类展示了如何使用Jest客户端连接Elasticsearch集群,执行CRUD(创建、读取、更新、删除)操作以及复杂的查询。 3. **README.md**:可能包含项目的...
本文将深入探讨如何使用Elasticsearch(简称ES)与JavaWeb技术来实现关键词索引,并关联相关文章内容。Elasticsearch是一个分布式、RESTful风格的搜索和数据分析引擎,能够用于实时的全文搜索,同时提供分析和存储...
综上所述,这个项目提供了一种高效的方式来在Spring Boot应用中使用Elasticsearch,通过JPA简化数据操作,并通过Jest客户端实现了与ES的通信。同时,集群配置确保了应用可以在多节点环境中运行,提高了可用性和可...
Elasticsearch查询客户端是用于与ES服务器通信的软件,它们提供了多种语言的API,允许开发者以编程方式执行索引、搜索、更新和删除等操作。常见的Elasticsearch客户端包括: - **Jest**:一个轻量级的Java REST...
Jest是一个Java实现的Elasticsearch REST客户端,它提供了简单易用的API来调用Elasticsearch的RESTful API。使用Jest,开发者可以在Java应用中轻松集成Elasticsearch,执行各种操作,如: - **配置Jest客户端**:...
常见的客户端API包括Java REST Client、Transport Client(已被弃用)和Jest等。此部分会涵盖以下知识点: 1. **连接配置**:设置Elasticsearch集群的地址、端口和认证信息,建立客户端连接。 2. **API调用**:...
本项目是关于如何将Elasticsearch整合进SpringBoot应用中的实践,通过使用Jest客户端库来实现对Elasticsearch的操作,包括增、删、改、查等基本功能。 首先,我们需要理解Elasticsearch的核心概念。Elasticsearch由...