package com.xinghuo.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.xinghuo.entity.AppInfo;
import com.xinghuo.entity.AppPending;
import com.xinghuo.entity.QueryEntity;
import com.xinghuo.mapper.AppInfoMapper;
import io.searchbox.client.JestClient;
import io.searchbox.client.JestResult;
import io.searchbox.core.Bulk;
import io.searchbox.core.ClearScroll;
import io.searchbox.core.Delete;
import io.searchbox.core.Index;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import io.searchbox.core.SearchResult.Hit;
import io.searchbox.core.SearchScroll;
import io.searchbox.indices.CreateIndex;
import io.searchbox.indices.mapping.GetMapping;
import io.searchbox.params.Parameters;
@Service
public class JestService{
@Autowired
private JestClient jestClient;
@Autowired
private AppInfoMapper appInfoMapper;
public void createIndex() {
try {
//mapping json字符串,Elasticsearch7取消type
XContentBuilder mapping = XContentFactory.jsonBuilder()
.startObject()
.startObject("settings")
.field("number_of_shards", "5")
.field("number_of_replicas", "1")
.endObject()
.startObject("mappings")
.startObject("properties")
.startObject("uuid").field("type", "text").endObject()
.startObject("appId").field("type", "text").endObject()
.startObject("appSecretKey").field("type", "text").endObject()
.startObject("appName").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("appIntro").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("appClassify").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("buildUnit").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("respName").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("respCode").field("type", "text").endObject()
.startObject("respTelPhone").field("type", "text").endObject()
.startObject("maintainName").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("maintainTel").field("type", "text").endObject()
.startObject("createUser").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("supplierName").field("type", "text").field("analyzer", "ik_max_word").endObject()
.startObject("createTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()
.startObject("updateTime").field("type", "date").field("format", "yyyy-MM-dd HH:mm:ss").endObject()
.endObject()
.endObject()
.endObject();
System.out.println(mapping.string());
CreateIndex createIndex = new CreateIndex.Builder(AppInfo.INDEX).settings(mapping.string()).build();
JestResult jestResult = jestClient.execute(createIndex);
System.out.println(jestResult.getJsonString());
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 手动增加测试数据
* 实际中可以使用定时任务同步,或者logstash
*/
public void add() {
List<AppInfo> appInfoList = appInfoMapper.selectAppInfoAll();
Bulk.Builder bulk = new Bulk.Builder();
for (AppInfo appInfo : appInfoList) {
Index index = new Index.Builder(appInfo).index(AppInfo.INDEX).id(appInfo.getUuid()).build();
bulk.addAction(index);
}
try {
JestResult jestResult = jestClient.execute(bulk.build());
System.out.println(jestResult.getJsonString());
} catch (Exception e) {
e.printStackTrace();
}
}
public String search(String keyword) {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//全文搜索
sourceBuilder.query(QueryBuilders.queryStringQuery(keyword).analyzer("ik_max_word"));
sourceBuilder.sort("_score", SortOrder.DESC);
sourceBuilder.sort("updateTime", SortOrder.DESC);
List<String> indexList = new ArrayList<String>();
indexList.add(AppInfo.INDEX);
indexList.add(AppPending.INDEX);
Search search = new Search.Builder(sourceBuilder.toString()).addIndex(indexList).build();
try {
JestResult jestResult = jestClient.execute(search);
System.out.println(jestResult.getJsonString());
return jestResult.getJsonString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String searchHighlighe(String keyword) {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//全文搜索
sourceBuilder.query(QueryBuilders.queryStringQuery(keyword).analyzer("ik_max_word"));
sourceBuilder.sort("_score", SortOrder.DESC);
sourceBuilder.sort("updateTime", SortOrder.DESC);
//高亮
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("appIntroText");
highlightBuilder.field("appNameText");
highlightBuilder.requireFieldMatch(false); //如果多个字段设置为false
highlightBuilder.preTags("<em>").postTags("</em>"); //高亮标签
highlightBuilder.fragmentSize(100); //高亮长度
sourceBuilder.highlight(highlightBuilder);
Search search = new Search.Builder(sourceBuilder.toString()).addIndex(AppInfo.INDEX).build();
try {
SearchResult searchResult = jestClient.execute(search);
System.out.println(searchResult.getJsonString());
List<Hit<AppInfo, Void>> hits = searchResult.getHits(AppInfo.class);
for (Hit<AppInfo, Void> hit : hits) {
AppInfo appInfo = hit.source;
Map<String, List<String>> highlight = hit.highlight;
List<String> appIntroText = highlight.get("appIntroText");
if(appIntroText != null){
appInfo.setAppIntroText(appIntroText.get(0));
}
List<String> appNameText = highlight.get("appNameText");
if(appNameText != null){
appInfo.setAppNameText(appNameText.get(0));
}
}
System.out.println(JSON.toJSONString(hits));
return searchResult.getJsonString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String update(String uuid) {
AppInfo appInfo = appInfoMapper.selectAppInfoByUuid(uuid);
appInfo.setAppNameText("测试修改应用名称1");
// String script = "{" + " \"doc\" : {" + " \"appName\" : \"测试修改应用名\" }" + "}";
// String script = "{\"doc\":" + JSONObject.toJSONString(appInfo) + "}";
// Update update = new Update.Builder(script).index(AppInfo.INDEX).type(AppInfo.TYPE).id(appInfo.getUuid()).build();
Index index = new Index.Builder(appInfo).index(AppInfo.INDEX).type(AppInfo.TYPE).id(appInfo.getUuid()).build();
try {
JestResult jestResult = jestClient.execute(index);
System.out.println(jestResult.getJsonString());
return jestResult.getJsonString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void delete(String uuid) {
Delete delete = new Delete.Builder(uuid).index(AppInfo.INDEX).type(AppInfo.TYPE).build();
try {
JestResult jestResult = jestClient.execute(delete);
System.out.println(jestResult.getJsonString());
} catch (Exception e) {
e.printStackTrace();
}
}
public String getMap

涛哥是个大帅比
- 粉丝: 8196
最新资源
- 微信小程序(4).zip
- 测试方案评审过程.docx
- 中级职称考前复习之建筑结构专业PPT-ppt.ppt
- 2019造价部门年终工作总结范文5篇.docx
- 基于ElasticSearch的匹配搜索引擎搭建项目-全文检索-模糊匹配-相关性排序-分布式搜索-高性能查询-数据索引优化-实时搜索-多字段匹配-自定义评分-聚合分析-中文分词-停.zip
- odoo 微信小程序商城模块 (WeChat Mini Program for Odoo).zip
- 医院智能化弱电系统设计方案.doc
- 2019年天猫母婴洞察消费报告.pdf
- 渠道衬砌施工方案.doc
- 青蛙的故事——生活启迪.ppt
- 语言活动《分房子》.doc
- 微信小程序,仿Apple Music.zip
- 地铁工程安全质量和文明施工管理办法.doc
- [造价入门]2015年建设工程造价基础知识PPT讲义(名企编制-249页).ppt
- 中建某公司给排水管道内部培训.ppt
- PC微信小程序 wxapkg 解密.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


