add:集成es搜索模块
This commit is contained in:
@@ -0,0 +1,328 @@
|
||||
package com.adc.da.search.controller;
|
||||
|
||||
import cn.afterturn.easypoi.excel.ExcelExportUtil;
|
||||
import cn.afterturn.easypoi.excel.entity.ExportParams;
|
||||
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
|
||||
import com.adc.da.base.web.BaseController;
|
||||
import com.adc.da.person.service.PersonCollectEOService;
|
||||
import com.adc.da.search.dto.*;
|
||||
import com.adc.da.search.entity.SarAdvanceSearchVO;
|
||||
import com.adc.da.search.entity.SeniorSearchInfoEO;
|
||||
import com.adc.da.search.service.ElasticsearchService;
|
||||
import com.adc.da.search.service.SearchCenterService;
|
||||
import com.adc.da.util.exception.AdcDaBaseException;
|
||||
import com.adc.da.util.http.PageInfo;
|
||||
import com.adc.da.util.http.ResponseMessage;
|
||||
import com.adc.da.util.http.Result;
|
||||
import com.adc.da.util.utils.IOUtils;
|
||||
import com.adc.da.util.utils.StringUtils;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.OutputStream;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static com.adc.da.sys.common.ReadExcelName.encodeFileName;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/${restPath}/search/searchCenter")
|
||||
@Api(description = "|SearchCenter|")
|
||||
public class SearchCenterController extends BaseController<Map<String, Object>> {
|
||||
|
||||
@Autowired
|
||||
private TransportClient client;
|
||||
@Autowired
|
||||
private SearchCenterService searchCenterService;
|
||||
|
||||
@Autowired
|
||||
private PersonCollectEOService personCollectEOService;
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchService elasticsearchService;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param searchInfoEO
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "|SearchCenter|查询")
|
||||
@PostMapping(value="/searchSarBykey")
|
||||
public ResponseMessage<PageInfo<Map<String, Object>>> searchSarBykey(SeniorSearchInfoEO searchInfoEO) throws Exception{
|
||||
List<Map<String, Object>> result = new ArrayList<>();
|
||||
if (StringUtils.isAlphanumericSpace(searchInfoEO.getSelectIndex())) {
|
||||
if(elasticsearchService.isIndexExist(searchInfoEO.getSelectIndex())){
|
||||
if("fulltextserch".equals(searchInfoEO.getSelectIndex()) || "fulltextserch"== searchInfoEO.getSelectIndex()){
|
||||
Pattern pattern = Pattern.compile("^[a-zA-Z]+\\s[0-9a-zA-Z\\.\\(\\(\\)\\)\\-\\s]+$");
|
||||
if (null != searchInfoEO.getSelectValue() && StringUtils.isNotBlank(searchInfoEO.getSelectValue())) {
|
||||
if (pattern.matcher(searchInfoEO.getSelectValue()).matches()) {
|
||||
result = searchCenterService.searchSarBykeyAndHighLightNumber(searchInfoEO);
|
||||
}else {
|
||||
result = searchCenterService.searchSarBykeyAndHighLight(searchInfoEO);
|
||||
}
|
||||
} else {
|
||||
result = searchCenterService.searchSarBykeyAndHighLight(searchInfoEO);
|
||||
}
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
String collectId = personCollectEOService.queryCollectByUserAndId(result.get(i).get("id").toString());
|
||||
result.get(i).put("collectId", collectId);
|
||||
}
|
||||
return Result.success(getPageInfo(searchInfoEO.getPager(), result));
|
||||
} else {
|
||||
result = searchCenterService.searchSarBykey(searchInfoEO);
|
||||
// 非动态消息的需要查询为我推荐
|
||||
for (int i = 0; i < result.size(); i++) {
|
||||
String collectId = personCollectEOService.queryCollectByUserAndId(result.get(i).get("id").toString());
|
||||
result.get(i).put("collectId", collectId);
|
||||
result.get(i).put("content", "");
|
||||
result.get(i).put("baseSearchContent", "");
|
||||
}
|
||||
return Result.success(getPageInfo(searchInfoEO.getPager(), result));
|
||||
}
|
||||
} else {
|
||||
return Result.error("未查到相关数据");
|
||||
}
|
||||
} else {
|
||||
return Result.error("数据错误");
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation(value = "|SearchCenter|查询用于导出数据")
|
||||
@PostMapping(value="/searchIdToImport")
|
||||
public ResponseMessage<String []> searchIdToImport(SeniorSearchInfoEO searchInfoEO){
|
||||
if (StringUtils.isAlphanumericSpace(searchInfoEO.getSelectIndex())) {
|
||||
if(elasticsearchService.isIndexExist(searchInfoEO.getSelectIndex())){
|
||||
String [] result = searchCenterService.searchIdToImport(searchInfoEO);
|
||||
return Result.success(result);
|
||||
}
|
||||
else {
|
||||
return Result.error("未查到相关数据");
|
||||
}
|
||||
} else {
|
||||
return Result.error("数据错误");
|
||||
}
|
||||
}
|
||||
|
||||
@ApiOperation(value = "|SearchCenter|查询")
|
||||
@PostMapping(value="/selectGroupdata")
|
||||
public ResponseMessage<Map<String, Object>> selectGroupdata(SeniorSearchInfoEO searchInfoEO){
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
result = searchCenterService.selectGroupdata(searchInfoEO);
|
||||
return Result.success(result);
|
||||
}
|
||||
|
||||
@ApiOperation(value = "|SarLawsInfoEO|导出搜索中心数据")
|
||||
@GetMapping("/exportSearchCenterDatas")
|
||||
public void exportSearchCenterDatas(String exportDatas,String searchType, HttpServletResponse response, HttpServletRequest request) throws Exception{
|
||||
OutputStream os = null;
|
||||
if ("bussstand".equals(searchType)){
|
||||
searchType = "stand";
|
||||
}else if ("enterprise".equals(searchType)){
|
||||
searchType = "bussstand";
|
||||
}
|
||||
Workbook workbook = null;
|
||||
try{
|
||||
response.setHeader("Content-Disposition",
|
||||
"attachment; filename=" + encodeFileName( "检索结果.xlsx", request));
|
||||
response.setContentType("application/force-download");
|
||||
ExportParams exportParams = new ExportParams();
|
||||
exportParams.setType(ExcelType.XSSF);
|
||||
//集合 sarAdvanceSearc 的字符串
|
||||
|
||||
String sarAdvanceSearcString = subString(exportDatas,"[{","}]");
|
||||
if (StringUtils.isNotBlank(sarAdvanceSearcString)){
|
||||
sarAdvanceSearcString = sarAdvanceSearcString.replace("\\\"","\"");
|
||||
}
|
||||
SeniorSearchInfoEO searchInfoEO = null;
|
||||
if (!"none".equals(sarAdvanceSearcString)) {
|
||||
exportDatas = exportDatas.replace("[{" + sarAdvanceSearcString + "}]", "");
|
||||
JSONObject jsonObject = JSONObject.parseObject(exportDatas);
|
||||
List<String> sarAdvanceSearchVO = new ArrayList<>(Arrays.asList(sarAdvanceSearcString.split("},")));
|
||||
List<SarAdvanceSearchVO> advanceSearchVOList = new ArrayList<>();
|
||||
int flag = 0;
|
||||
for (String sarAdvanceSearch : sarAdvanceSearchVO) {
|
||||
JSONObject jsonObjectSAS = null;
|
||||
if (flag == 0) {
|
||||
jsonObjectSAS = JSONObject.parseObject("{" + sarAdvanceSearch + "}");
|
||||
} else {
|
||||
jsonObjectSAS = JSONObject.parseObject(sarAdvanceSearch + "}");
|
||||
}
|
||||
SarAdvanceSearchVO sarAdvanceSearchEO = JSONObject.toJavaObject(jsonObjectSAS, SarAdvanceSearchVO.class);
|
||||
advanceSearchVOList.add(sarAdvanceSearchEO);
|
||||
flag++;
|
||||
}
|
||||
searchInfoEO = JSONObject.toJavaObject(jsonObject, SeniorSearchInfoEO.class);
|
||||
searchInfoEO.setAdvanceSearchVOList(advanceSearchVOList);
|
||||
}else {
|
||||
JSONObject jsonObject=JSONObject.parseObject(exportDatas);
|
||||
searchInfoEO = JSONObject.toJavaObject(jsonObject, SeniorSearchInfoEO.class);
|
||||
}
|
||||
if ("INLAND_STAND".equals(searchType)) {
|
||||
searchType = "stand"; //guonei
|
||||
}else if ("INLAND_LAWS".equals(searchType)) {
|
||||
searchType = "laws"; //zhengce
|
||||
}else if ("BUSINESS_STAND".equals(searchType)) {
|
||||
searchType = "bussstand"; //enterprise
|
||||
}else if ("DYNAMIC_INFORMATION".equals(searchType)) {
|
||||
searchType = "msgdyinfo";
|
||||
}else if ("items".equals(searchType)) {
|
||||
searchType = "items";
|
||||
}
|
||||
searchInfoEO.setSelectIndex(searchType);
|
||||
searchInfoEO.setSelectValue(searchInfoEO.getKeywords());
|
||||
//存放需要导出的数据
|
||||
List<Map<String, Object>> result = new ArrayList<>();
|
||||
result = searchCenterService.searchSarBykey(searchInfoEO);
|
||||
if("stand".equals(searchType)){
|
||||
List<SearchStandExportDto> dtoDatas = new ArrayList<>();
|
||||
for(int i=0;i<result.size();i++){
|
||||
SearchStandExportDto exportDto = new SearchStandExportDto();
|
||||
String numbershow = String.valueOf(result.get(i).get("numbershow"));
|
||||
exportDto.setNumbershow(numbershow);
|
||||
String nameshow = String.valueOf( result.get(i).get("nameshow"));
|
||||
exportDto.setNameshow(nameshow);
|
||||
String issueTime = String.valueOf(result.get(i).get("issue_time"));
|
||||
exportDto.setIssueTime(issueTime);
|
||||
// String putTime = changeDate(result.get(i).get("put_time"));
|
||||
// exportDto.setPutTime(putTime);
|
||||
exportDto.setStandstateshow(String.valueOf(result.get(i).get("standstateshow")));
|
||||
dtoDatas.add(exportDto);
|
||||
}
|
||||
//导出数据到Excel
|
||||
workbook = ExcelExportUtil.exportExcel(exportParams, SearchStandExportDto.class, dtoDatas);
|
||||
} else if("laws".equals(searchType)){
|
||||
List<SearchLawsExportDto> dtoDatas = new ArrayList<>();
|
||||
for(int i=0;i<result.size();i++){
|
||||
SearchLawsExportDto exportDto = new SearchLawsExportDto();
|
||||
String numbershow = String.valueOf(result.get(i).get("numbershow"));
|
||||
exportDto.setNumbershow(numbershow);
|
||||
String nameshow = String.valueOf(result.get(i).get("nameshow"));
|
||||
exportDto.setNameshow(nameshow);
|
||||
String issueTime = String.valueOf(result.get(i).get("issue_time"));
|
||||
exportDto.setIssueTime(issueTime);
|
||||
// String putTime = changeDate(result.get(i).get("put_time"));
|
||||
// exportDto.setPutTime(putTime);
|
||||
exportDto.setStandstateshow(String.valueOf(result.get(i).get("standstateshow")));
|
||||
dtoDatas.add(exportDto);
|
||||
}
|
||||
//导出数据到Excel
|
||||
workbook = ExcelExportUtil.exportExcel(exportParams, SearchLawsExportDto.class, dtoDatas);
|
||||
} else if("bussstand".equals(searchType)){
|
||||
List<SearchBussExportDto> dtoDatas = new ArrayList<>();
|
||||
for(int i=0;i<result.size();i++){
|
||||
SearchBussExportDto exportDto = new SearchBussExportDto();
|
||||
String numbershow = String.valueOf(result.get(i).get("numbershow"));
|
||||
exportDto.setNumbershow(numbershow);
|
||||
String nameshow = String.valueOf(result.get(i).get("nameshow"));
|
||||
exportDto.setNameshow(nameshow);
|
||||
if (StringUtils.isNotBlank(String.valueOf(result.get(i).get("issue_time"))) && !"null".equals(String.valueOf(result.get(i).get("issue_time")))) {
|
||||
String issueTime = "";
|
||||
try {
|
||||
issueTime = changeDate(String.valueOf(result.get(i).get("issue_time")));
|
||||
}catch (Exception ignored){
|
||||
issueTime = "";
|
||||
}
|
||||
exportDto.setIssueTime(issueTime);
|
||||
}else {
|
||||
exportDto.setIssueTime("");
|
||||
}
|
||||
// String putTime = changeDate(result.get(i).get("put_time"));
|
||||
// exportDto.setPutTime(putTime);
|
||||
exportDto.setStandstateshow((String) result.get(i).get("standstateshow"));
|
||||
dtoDatas.add(exportDto);
|
||||
}
|
||||
//导出数据到Excel
|
||||
workbook = ExcelExportUtil.exportExcel(exportParams, SearchBussExportDto.class, dtoDatas);
|
||||
// } else if("items".equals(searchType)){
|
||||
// List<SearchItemsExportDto> dtoDatas = new ArrayList<>();
|
||||
// for(int i=0;i<result.size();i++){
|
||||
// SearchItemsExportDto exportDto = new SearchItemsExportDto();
|
||||
// String newcarPutTime = changeDate(String.valueOf(result.get(i).get("newcarPutTime")));
|
||||
// exportDto.setNewcarPutTime(newcarPutTime);
|
||||
// String productPutTime = changeDate(String.valueOf(result.get(i).get("productPutTime")));
|
||||
// exportDto.setProductPutTime(productPutTime);
|
||||
// exportDto.setApplyArcticShow(String.valueOf(result.get(i).get("applyArcticShow")));
|
||||
// exportDto.setItemsName(String.valueOf(result.get(i).get("itemsName")));
|
||||
// exportDto.setItemsNum(String.valueOf(result.get(i).get("itemsNum")));
|
||||
// exportDto.setParts(String.valueOf(result.get(i).get("parts")));
|
||||
// dtoDatas.add(exportDto);
|
||||
// }
|
||||
//导出数据到Excel
|
||||
// workbook = ExcelExportUtil.exportExcel(exportParams, SearchItemsExportDto.class, dtoDatas);
|
||||
} else {
|
||||
List<SearchMsgExportDto> dtoDatas = new ArrayList<>();
|
||||
for(int i=0;i<result.size();i++){
|
||||
SearchMsgExportDto exportDto = new SearchMsgExportDto();
|
||||
exportDto.setModuleName(String.valueOf(result.get(i).get("moduleName")));
|
||||
exportDto.setTitle((String) result.get(i).get("title"));
|
||||
String issueTime = changeDate(String.valueOf(result.get(i).get("issue_time")));
|
||||
exportDto.setIssueTime(issueTime);
|
||||
dtoDatas.add(exportDto);
|
||||
}
|
||||
//导出数据到Excel
|
||||
workbook = ExcelExportUtil.exportExcel(exportParams, SearchMsgExportDto.class, dtoDatas);
|
||||
}
|
||||
|
||||
os = response.getOutputStream();
|
||||
workbook.write(os);
|
||||
os.flush();
|
||||
workbook.close();
|
||||
} catch (Exception e) {
|
||||
|
||||
throw new AdcDaBaseException("导出失败,请重试");
|
||||
} finally {
|
||||
IOUtils.closeQuietly(os);
|
||||
if (workbook != null) {
|
||||
workbook.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public String changeDate (Object obj) throws Exception {
|
||||
String dateStr = "";
|
||||
if (obj != null) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
Date issueDate = dateFormat.parse((String) obj);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTime(issueDate);
|
||||
calendar.add(Calendar.DATE, 1);
|
||||
issueDate = calendar.getTime();
|
||||
if (issueDate != null) {
|
||||
dateStr = dateFormat.format(issueDate);
|
||||
}
|
||||
}
|
||||
return dateStr;
|
||||
}
|
||||
|
||||
/**
|
||||
* 截取字符串str中指定字符 strStart、strEnd之间的字符串
|
||||
*
|
||||
* @param str
|
||||
* @param strStart
|
||||
* @param strEnd
|
||||
* @return
|
||||
*/
|
||||
public static String subString(String str, String strStart, String strEnd) {
|
||||
|
||||
/* 找出指定的2个字符在 该字符串里面的 位置 */
|
||||
int strStartIndex = str.indexOf(strStart);
|
||||
int strEndIndex = str.indexOf(strEnd);
|
||||
if (strStartIndex < 0 || strEndIndex < 0) {
|
||||
return "none";
|
||||
}
|
||||
/* 开始截取 */
|
||||
String result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
|
||||
return result;
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
package com.adc.da.search.controller;
|
||||
|
||||
import com.adc.da.base.web.BaseController;
|
||||
import com.adc.da.search.service.ElasticsearchService;
|
||||
import com.adc.da.util.http.ResponseMessage;
|
||||
import com.adc.da.util.http.Result;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/${restPath}/search/standLawsSearch")
|
||||
@Api(description = "|StandLawsSearch|")
|
||||
public class StandLawsSearchController extends BaseController<Map<String, Object>> {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(StandLawsSearchController.class);
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchService elasticsearchService;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param
|
||||
* @return
|
||||
*/
|
||||
@ApiOperation(value = "|BussnessStand|创建索引")
|
||||
@PostMapping(value="/createBussstandIndex")
|
||||
public ResponseMessage createBussstandIndex(){
|
||||
XContentBuilder mapping = null;
|
||||
try {
|
||||
mapping = XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.startObject("properties") //设置之定义字段
|
||||
.startObject("apply_arctic")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("applyarcticshow")
|
||||
.field("type","text")
|
||||
.endObject()
|
||||
.startObject("energy_kind")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("id")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("issue_time")
|
||||
.field("type","date") //设置Date类型
|
||||
// .field("format","yyyy-MM-dd HH:mm:ss") //设置Date的格式
|
||||
.endObject()
|
||||
.startObject("nameshow")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("numbershow")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("put_time")
|
||||
.field("type","date") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("replace_stand_num")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("replaced_stand_num")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("standstateshow")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("statecode")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("type")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("valid_flag")
|
||||
.field("type","long") //设置数据类型
|
||||
.endObject()
|
||||
.startObject("content")
|
||||
.field("type","text") //设置数据类型
|
||||
.endObject()
|
||||
.endObject()
|
||||
.endObject();
|
||||
} catch (IOException e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
}
|
||||
boolean restule = elasticsearchService.createIndex("newbussstand",mapping);
|
||||
if (restule) {
|
||||
return Result.success("创建成功");
|
||||
} else {
|
||||
return Result.error("数据错误");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package com.adc.da.search.controller;
|
||||
|
||||
import com.adc.da.search.entity.PartProductEO;
|
||||
import com.adc.da.search.service.ElasticsearchService;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import io.swagger.annotations.Api;
|
||||
import org.elasticsearch.action.index.IndexResponse;
|
||||
import org.elasticsearch.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.client.transport.TransportClient;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.RangeQueryBuilder;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.adc.da.search.util.ElasticsearchConstant.SUCCESS_DELETED;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/${restPath}")
|
||||
@Api(description = "|testElasticsearch|")
|
||||
public class TestController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TestController.class);
|
||||
|
||||
@Autowired
|
||||
private TransportClient client;
|
||||
@Autowired
|
||||
private ElasticsearchService elasticsearchService;
|
||||
|
||||
|
||||
|
||||
@PostMapping("add/book/novel")
|
||||
@ResponseBody
|
||||
public ResponseEntity add(@RequestParam(name ="title") String title,
|
||||
@RequestParam(name ="author") String author,
|
||||
@RequestParam(name ="word_count") String wordCount,
|
||||
@RequestParam(name ="publish_date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date publishDate){
|
||||
try {
|
||||
XContentBuilder content =XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("title",title)
|
||||
.field("author",author)
|
||||
.field("word_count",wordCount)
|
||||
.field("publish_date",publishDate.getTime())
|
||||
.endObject();
|
||||
IndexResponse result = this.client.prepareIndex("book","novel").setSource(content).get();
|
||||
return new ResponseEntity(result.getId(),HttpStatus.OK);
|
||||
}
|
||||
catch (IOException e) {
|
||||
logger.error(e.getMessage(),e);
|
||||
return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@DeleteMapping("deleteOneDucumentTest")
|
||||
@ResponseBody
|
||||
public ResponseEntity delete(@RequestParam(name="id") String id) {
|
||||
String response = elasticsearchService.deleteDataById("book","novel",id);
|
||||
if(response.equals(SUCCESS_DELETED)){
|
||||
return new ResponseEntity(response, HttpStatus.OK);
|
||||
}
|
||||
else {
|
||||
return new ResponseEntity(response, HttpStatus.NOT_FOUND);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("query/book/novel")
|
||||
@ResponseBody
|
||||
public ResponseEntity query(
|
||||
@RequestParam(name="title",required = false) String title,
|
||||
@RequestParam(name="author",required = false) String author,
|
||||
@RequestParam(name="gt_word_count",defaultValue = "0",required = false) int gtWordCount,
|
||||
@RequestParam(name="lt_word_count",required = false) Integer ltWordCount
|
||||
){
|
||||
BoolQueryBuilder boolQueryBuilder =QueryBuilders.boolQuery();
|
||||
if(author!=null){
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery("author",author));
|
||||
}
|
||||
if(title!=null){
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery("title",title));
|
||||
}
|
||||
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("word_count").from(gtWordCount);
|
||||
|
||||
if(ltWordCount!=null && ltWordCount>0 ){
|
||||
rangeQueryBuilder.to(ltWordCount);
|
||||
}
|
||||
boolQueryBuilder.filter(rangeQueryBuilder);
|
||||
SearchRequestBuilder searchRequestBuilder =this.client.prepareSearch("book")
|
||||
.setTypes("novel")
|
||||
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
|
||||
.setQuery(boolQueryBuilder)
|
||||
.setFrom(0)
|
||||
.setSize(10);
|
||||
System.out.println(searchRequestBuilder);
|
||||
SearchResponse response =searchRequestBuilder.get();
|
||||
List<Map<String ,Object>> result = new ArrayList<Map<String ,Object>>();
|
||||
for (SearchHit hit:response.getHits()){
|
||||
result.add(hit.getSourceAsMap());
|
||||
}
|
||||
return new ResponseEntity(result,HttpStatus.OK);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@PostMapping("addIndexSerTest")
|
||||
@ResponseBody
|
||||
public String searchIndexTest() {
|
||||
PartProductEO productEO= new PartProductEO();
|
||||
productEO.setCertNumber("er");
|
||||
productEO.setCorpId("cid");
|
||||
productEO.setCorpModel("cmodel");
|
||||
String indexstate = elasticsearchService.addData((JSONObject) JSON.toJSON(productEO),"testone","two");
|
||||
return indexstate;
|
||||
}
|
||||
|
||||
@PostMapping("bulktest")
|
||||
@ResponseBody
|
||||
public String bulkTest() throws IOException {
|
||||
List<JSONObject> list = new ArrayList<JSONObject>();
|
||||
for(int i=0;i<100000;i++){
|
||||
PartProductEO productEO= new PartProductEO();
|
||||
productEO.setCertNumber("er"+i);
|
||||
productEO.setCorpId("cid"+i);
|
||||
productEO.setCorpModel("cmodel"+i);
|
||||
list.add(JSONObject.parseObject(JSONObject.toJSON(productEO).toString()));
|
||||
}
|
||||
String indexstate = elasticsearchService.addBulkData("ymq_index","two",list);
|
||||
return indexstate;
|
||||
}
|
||||
|
||||
@PostMapping("justtest")
|
||||
@ResponseBody
|
||||
public List<Map<String, Object>> justtest() throws IOException {
|
||||
String [] index={"bank","book"};
|
||||
String [] type={"_doc","_type"};
|
||||
String [] fields={"account_number", "balance"};
|
||||
List<Map<String, Object>> indexstate = elasticsearchService.searchListDataGroupComplex(index,type);
|
||||
return indexstate;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user