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