add: 新增lucene日志检索
This commit is contained in:
@@ -409,16 +409,6 @@
|
||||
</dependency>
|
||||
|
||||
<!-- Lucence核心包 -->
|
||||
<!--<dependency>
|
||||
<groupId>com.alone</groupId>
|
||||
<artifactId>embedded-lucene-core</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alone</groupId>
|
||||
<artifactId>embedded-lucene-spring-boot-starter</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>-->
|
||||
<dependency>
|
||||
<groupId>org.apache.lucene</groupId>
|
||||
<artifactId>lucene-core</artifactId>
|
||||
@@ -450,7 +440,7 @@
|
||||
<dependency>
|
||||
<groupId>org.apache.lucene</groupId>
|
||||
<artifactId>lucene-queryparser</artifactId>
|
||||
<version>8.2.0</version>
|
||||
<version>${lucene.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
|
||||
@@ -172,11 +172,13 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device
|
||||
stop = this.itemProtocol.getStop();
|
||||
|
||||
if (home_relocation != last_home_relocation) {
|
||||
LuceneLogDto logDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("信号" + this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.device_code + "." + ItemProtocol.to_home_relocation + "变更从" + this.last_home_relocation + "->" + this.home_relocation)
|
||||
.build();
|
||||
luceneExecuteLogService.deviceExecuteLog(logDto);
|
||||
// LuceneLogDto logDto = LuceneLogDto.builder()
|
||||
// .device_code(device_code)
|
||||
// .content("信号" + this.getDevice().getOpc_server_code() + "." + this.getDevice().getOpc_plc_code() + "." + this.device_code + "." + ItemProtocol.to_home_relocation + "变更从" + this.last_home_relocation + "->" + this.home_relocation)
|
||||
// .build();
|
||||
// luceneExecuteLogService.deviceExecuteLog(logDto);
|
||||
luceneExecuteLogService.deviceExecuteLog(
|
||||
new LuceneLogDto(this.getDevice().getOpc_server_code(),this.getDevice().getOpc_plc_code(),this.device_code,ItemProtocol.to_home_relocation,this.last_home_relocation,this.home_relocation));
|
||||
}
|
||||
|
||||
if (stop != last_stop) {
|
||||
@@ -389,7 +391,7 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device
|
||||
LuceneLogDto resplogDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("返回参数:"+resp.body())
|
||||
.requestparam("返回参数:"+resp.body())
|
||||
.responseparam("返回参数:"+resp.body())
|
||||
.method("feedAgvTaskStatus")
|
||||
.status_code(String.valueOf(resp.getStatus()))
|
||||
.build();
|
||||
@@ -430,7 +432,7 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device
|
||||
LuceneLogDto resplogDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("返回参数:"+resp.body())
|
||||
.requestparam("返回参数:"+resp.body())
|
||||
.responseparam("返回参数:"+resp.body())
|
||||
.method("feedAgvTaskStatus")
|
||||
.status_code(String.valueOf(resp.getStatus()))
|
||||
.build();
|
||||
@@ -467,7 +469,7 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device
|
||||
LuceneLogDto resplogDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("返回参数:"+resp.body())
|
||||
.requestparam("返回参数:"+resp.body())
|
||||
.responseparam("返回参数:"+resp.body())
|
||||
.method("feedAgvTaskStatus")
|
||||
.status_code(String.valueOf(resp.getStatus()))
|
||||
.build();
|
||||
@@ -502,7 +504,7 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device
|
||||
LuceneLogDto resplogDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("返回参数:"+resp.body())
|
||||
.requestparam("返回参数:"+resp.body())
|
||||
.responseparam("返回参数:"+resp.body())
|
||||
.method("feedAgvTaskStatus")
|
||||
.status_code(String.valueOf(resp.getStatus()))
|
||||
.build();
|
||||
@@ -532,7 +534,7 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device
|
||||
LuceneLogDto resplogDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("返回参数:"+resp.body())
|
||||
.requestparam("返回参数:"+resp.body())
|
||||
.responseparam("返回参数:"+resp.body())
|
||||
.method("feedAgvTaskStatus")
|
||||
.status_code(String.valueOf(resp.getStatus()))
|
||||
.build();
|
||||
@@ -596,7 +598,7 @@ public class XgagvDeviceDriver extends AbstractOpcDeviceDriver implements Device
|
||||
LuceneLogDto resplogDto = LuceneLogDto.builder()
|
||||
.device_code(device_code)
|
||||
.content("返回参数:"+resp.body())
|
||||
.requestparam("返回参数:"+resp.body())
|
||||
.responseparam("返回参数:"+resp.body())
|
||||
.method("feedAgvTaskStatus")
|
||||
.status_code(String.valueOf(resp.getStatus()))
|
||||
.build();
|
||||
|
||||
@@ -53,6 +53,10 @@ public class LuceneIndexWriter {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
indexWriter.deleteAll();
|
||||
}
|
||||
|
||||
public static String getDate(String timeString) throws ParseException {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");//时间格式
|
||||
Date date = sdf.parse(timeString);
|
||||
@@ -60,9 +64,5 @@ public class LuceneIndexWriter {
|
||||
return timeString;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
indexWriter.deleteAll();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateTime;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.document.Document;
|
||||
import org.apache.lucene.index.DirectoryReader;
|
||||
@@ -22,6 +23,7 @@ import java.util.*;
|
||||
/**
|
||||
* lucene查询器
|
||||
*/
|
||||
@Slf4j
|
||||
public class Searcher {
|
||||
|
||||
public static Map<String, Object> search(String indexDir, String ext,Map whereJson) throws Exception {
|
||||
@@ -47,8 +49,6 @@ public class Searcher {
|
||||
Sort sort = new Sort(new SortField("logTime", SortField.Type.DOC,true));
|
||||
|
||||
TopDocs docs = null;
|
||||
//通过解析要查询的String,获取查询对象,q为传进来的待查的字符串
|
||||
String queryString = "";
|
||||
BooleanQuery.Builder booleanQueryBuilder = new BooleanQuery.Builder();
|
||||
//时间范围查询
|
||||
String startDate = (String) whereJson.get("begin_time");
|
||||
@@ -71,28 +71,24 @@ public class Searcher {
|
||||
Query termQuery = new TermQuery(new Term("device_code", (String) whereJson.get("device_code")));
|
||||
booleanQueryBuilder.add(termQuery,BooleanClause.Occur.MUST);
|
||||
}
|
||||
//日志类型
|
||||
// if (whereJson.get("blurry") != null) queryString += "fieldContent:"+(String) whereJson.get("blurry");
|
||||
if (whereJson.get("blurry") != null) queryString = (String) whereJson.get("blurry");
|
||||
// if (whereJson.get("device_code") != null) queryString += " +device_code:"+(String) whereJson.get("device_code");
|
||||
|
||||
if (whereJson.get("blurry") == null) {
|
||||
WildcardQuery query=new WildcardQuery(new Term("fieldContent","*"));
|
||||
booleanQueryBuilder.add(query, BooleanClause.Occur.MUST);
|
||||
docs = searcher.search(booleanQueryBuilder.build(), end,sort);
|
||||
}else {
|
||||
if (whereJson.get("method") != null){
|
||||
Query termQuery = new TermQuery(new Term("method", (String) whereJson.get("method")));
|
||||
booleanQueryBuilder.add(termQuery,BooleanClause.Occur.MUST);
|
||||
}
|
||||
if (whereJson.get("status_code") != null){
|
||||
Query termQuery = new TermQuery(new Term("status_code", (String) whereJson.get("status_code")));
|
||||
booleanQueryBuilder.add(termQuery,BooleanClause.Occur.MUST);
|
||||
}
|
||||
if (whereJson.get("blurry") != null) {
|
||||
Query query = queryParser.parse((String) whereJson.get("blurry"));
|
||||
booleanQueryBuilder.add(query, BooleanClause.Occur.MUST);
|
||||
docs = searcher.search(booleanQueryBuilder.build(), end,sort);
|
||||
}
|
||||
|
||||
//记录索引结束时间
|
||||
docs = searcher.search(booleanQueryBuilder.build(), end,sort);
|
||||
//记录索引时间
|
||||
long endTime = System.currentTimeMillis();
|
||||
System.out.println("匹配" + queryString + "共耗时" + (endTime-startTime) + "毫秒");
|
||||
System.out.println("查询到" + docs.totalHits.value + "条日志文件");
|
||||
//取出每条查询结果
|
||||
log.info("匹配{}共耗时{}毫秒",booleanQueryBuilder.build(),(endTime-startTime));
|
||||
log.info("查询到{}条日志文件", docs.totalHits.value);
|
||||
List<String> list = new ArrayList<>();
|
||||
//取出每条查询结果
|
||||
ScoreDoc[] scoreDocs = docs.scoreDocs;
|
||||
if (end > docs.totalHits.value) end = (int) docs.totalHits.value;
|
||||
JSONArray array = new JSONArray();
|
||||
@@ -104,16 +100,17 @@ public class Searcher {
|
||||
object.put("content",doc.get("fieldContent"));
|
||||
object.put("device_code",doc.get("device_code"));
|
||||
object.put("logTime",doc.get("logTime"));
|
||||
object.put("method",doc.get("method"));
|
||||
object.put("status_code",doc.get("status_code"));
|
||||
object.put("requestparam",doc.get("requestparam"));
|
||||
object.put("responseparam",doc.get("responseparam"));
|
||||
if(doc.get("fieldContent") != null) {
|
||||
array.add(object);
|
||||
}
|
||||
}
|
||||
// System.out.println("查询到" + array.size() + "条记录");
|
||||
for(Object logDto:array){
|
||||
String st = logDto.toString();
|
||||
System.out.println(st);
|
||||
log.info(logDto.toString());
|
||||
}
|
||||
|
||||
reader.close();
|
||||
JSONObject jo = new JSONObject();
|
||||
jo.put("content", array);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package org.nl.modules.lucence.enums;
|
||||
|
||||
public enum LogTypeEnum {
|
||||
DEVICE_LOG("设备日志"),
|
||||
INTERFACE_LOG("接口日志");
|
||||
|
||||
private String desc;
|
||||
|
||||
LogTypeEnum(String desc) {
|
||||
this.desc = desc;
|
||||
}
|
||||
public String getDesc() {
|
||||
return desc;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package org.nl.modules.lucence.service;
|
||||
|
||||
import org.nl.modules.lucence.service.dto.LuceneLogDto;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface LuceneExecuteLogService {
|
||||
/**
|
||||
* 设备光电变化实时光电信号
|
||||
@@ -25,7 +27,7 @@ public interface LuceneExecuteLogService {
|
||||
*
|
||||
* @param luceneLogDto 日志结果对象
|
||||
*/
|
||||
void interfaceExecuteLog(LuceneLogDto luceneLogDto);
|
||||
void interfaceExecuteLog(LuceneLogDto luceneLogDto) throws IOException;
|
||||
|
||||
/**
|
||||
* 设备执行日志,会保留历史记录
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
package org.nl.modules.lucence.service.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class LuceneLogDto {
|
||||
|
||||
/* 日志标识 */
|
||||
@@ -64,4 +68,19 @@ public class LuceneLogDto {
|
||||
/* 修改时间 */
|
||||
private String update_time;
|
||||
|
||||
|
||||
|
||||
public LuceneLogDto (final String opc_server_code,final String opc_plc_code,
|
||||
final String device_code,final String to_home,final int last_home,
|
||||
final int home) {
|
||||
super ();
|
||||
this.device_code = device_code;
|
||||
this.content = "信号"
|
||||
+ opc_server_code + "."
|
||||
+ opc_plc_code + "."
|
||||
+ device_code + "."
|
||||
+ to_home + "变更从"
|
||||
+ last_home + "->"
|
||||
+ home;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,12 +11,15 @@ import org.apache.lucene.document.Field;
|
||||
import org.apache.lucene.document.StringField;
|
||||
import org.apache.lucene.document.TextField;
|
||||
import org.apache.lucene.index.IndexWriter;
|
||||
import org.nl.modules.lucence.enums.LogTypeEnum;
|
||||
import org.nl.modules.lucence.service.LuceneExecuteLogService;
|
||||
import org.nl.modules.lucence.service.dto.LuceneLogDto;
|
||||
import org.nl.modules.lucence.common.LuceneIndexWriter;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @author jlm
|
||||
* @description 服务实现
|
||||
@@ -35,7 +38,17 @@ public class LuceneExecuteLogServiceImpl implements LuceneExecuteLogService {
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public void deviceExecuteLog(LuceneLogDto luceneLogDto) {
|
||||
luceneLogDto.setLogType(LogTypeEnum.DEVICE_LOG.getDesc());
|
||||
addIndex(luceneLogDto);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interfaceExecuteLog(LuceneLogDto luceneLogDto) throws IOException {
|
||||
luceneLogDto.setLogType(LogTypeEnum.INTERFACE_LOG.getDesc());
|
||||
addIndex(luceneLogDto);
|
||||
}
|
||||
|
||||
private void addIndex(LuceneLogDto luceneLogDto) throws IOException {
|
||||
IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter();
|
||||
//创建一个Document对象
|
||||
Document document = new Document();
|
||||
@@ -45,33 +58,36 @@ public class LuceneExecuteLogServiceImpl implements LuceneExecuteLogService {
|
||||
//向document对象中添加域。
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getDevice_code())){
|
||||
document.add(new StringField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES));
|
||||
// document.add(new TextField("device_code", luceneLogDto.getDevice_code(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getContent())) {
|
||||
document.add(new TextField("fieldContent", luceneLogDto.getContent(), Field.Store.YES));
|
||||
}
|
||||
document.add(new TextField("logType", "deviceLog", Field.Store.YES));
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getMethod())) {
|
||||
document.add(new StringField("method", luceneLogDto.getMethod(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getStatus_code())) {
|
||||
document.add(new StringField("status_code", luceneLogDto.getStatus_code(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getRequestparam())) {
|
||||
document.add(new TextField("requestparam", luceneLogDto.getRequestparam(), Field.Store.YES));
|
||||
}
|
||||
if (ObjectUtil.isNotEmpty(luceneLogDto.getResponseparam())) {
|
||||
document.add(new TextField("responseparam", luceneLogDto.getResponseparam(), Field.Store.YES));
|
||||
}
|
||||
document.add(new StringField("logType", luceneLogDto.getLogType(), Field.Store.YES));
|
||||
document.add(new StringField("logTime", DateUtil.format(new DateTime(),"yyyy-MM-dd HH:mm:ss.SSS"), Field.Store.YES));
|
||||
indexWriter.addDocument(document);
|
||||
//记录索引结束时间
|
||||
long endTime = System.currentTimeMillis();
|
||||
// System.out.println("建立索引"+ "共耗时" + (endTime-startTime) + "毫秒");
|
||||
log.info("建立索引共耗时{}毫秒",endTime-startTime);
|
||||
indexWriter.commit();
|
||||
log.info("{},{}", luceneLogDto.getDevice_code(), luceneLogDto.getContent());
|
||||
//步骤八:关闭资源
|
||||
// indexWriter.close();
|
||||
// System.out.println("建立索引成功-----关闭资源");
|
||||
MDC.put("DEVICECODE", luceneLogDto.getDevice_code());
|
||||
}catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
// 删除所有的索引
|
||||
// indexWriter.deleteAll();
|
||||
indexWriter.close();
|
||||
log.error(e.getMessage(),e);
|
||||
}finally {
|
||||
// MDC.remove("DEVICECODE");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void interfaceExecuteLog(LuceneLogDto luceneLogDto) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -5,6 +5,7 @@ import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.nl.modules.lucence.common.Searcher;
|
||||
import org.nl.modules.lucence.service.LuceneService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -16,6 +17,7 @@ import java.util.Map;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class LuceneServiceImpl implements LuceneService {
|
||||
|
||||
@Value("${loki.url}")
|
||||
@@ -65,17 +67,18 @@ public class LuceneServiceImpl implements LuceneService {
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getAll(Map whereJson, Pageable page) {
|
||||
JSONObject jsonObject1 = new JSONObject();
|
||||
try {
|
||||
jsonObject1 = (JSONObject) Searcher.search(luceneUrl, "",whereJson);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
JSONArray array = jsonObject1.getJSONArray("content");
|
||||
int totalElements = Integer.parseInt(jsonObject1.get("totalElements").toString());
|
||||
JSONObject jo = new JSONObject();
|
||||
jo.put("content", array);
|
||||
jo.put("totalElements", totalElements);
|
||||
try {
|
||||
JSONObject jsonObject = (JSONObject) Searcher.search(luceneUrl, "", whereJson);
|
||||
JSONArray array = jsonObject.getJSONArray("content");
|
||||
int totalElements = Integer.parseInt(jsonObject.get("totalElements").toString());
|
||||
jo.put("content", array);
|
||||
jo.put("totalElements", totalElements);
|
||||
} catch (Exception e) {
|
||||
log.error("索引查询为空", e);
|
||||
throw new NullPointerException("索引查询为空");
|
||||
}
|
||||
|
||||
return jo;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user