rev:lucene

Signed-off-by: jiaoliming <dawnjiao@foxmail.com>
This commit is contained in:
jiaoliming
2023-09-01 10:07:22 +08:00
parent 7727ca0699
commit 4b8d2a7814
4 changed files with 87 additions and 70 deletions

View File

@@ -1,66 +1,66 @@
package org.nl.modules.lucene.common;
import cn.hutool.core.date.DateUtil;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.nl.modules.lucene.config.UrlConfig;
import org.wltea.analyzer.lucene.IKAnalyzer;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LuceneIndexWriter {
private static volatile IndexWriter indexWriter;
static {
try {
Directory directory = FSDirectory.open(new File(UrlConfig.luceneUrl).toPath());
IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
indexWriter = new IndexWriter(directory, config);
} catch (Exception e) {
e.printStackTrace();
}
/**当当前线程结束时自动关闭IndexWriter使用Runtime对象*/
// Runtime.getRuntime().addShutdownHook(new Thread(){
// @Override
// public void run() {
// try {
// closeIndexWriter();
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
// });
}
/**在线程结束时自动关闭IndexWriter*/
public static IndexWriter getIndexWriter() {
return indexWriter;
}
/**关闭IndexWriter
* @throws IOException
* @throws CorruptIndexException */
public static void closeIndexWriter() throws Exception {
if(indexWriter != null) {
// indexWriter.close();
}
}
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);
timeString = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss.SSS");//格式化后的时间
return timeString;
}
}
//package org.nl.modules.lucene.common;
//
//import cn.hutool.core.date.DateUtil;
//import org.apache.lucene.index.CorruptIndexException;
//import org.apache.lucene.index.IndexWriter;
//import org.apache.lucene.index.IndexWriterConfig;
//import org.apache.lucene.store.Directory;
//import org.apache.lucene.store.FSDirectory;
//import org.nl.modules.lucene.config.UrlConfig;
//import org.wltea.analyzer.lucene.IKAnalyzer;
//
//import java.io.File;
//import java.io.IOException;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.Date;
//
//public class LuceneIndexWriter {
// private static volatile IndexWriter indexWriter;
//
// static {
// try {
// Directory directory = FSDirectory.open(new File(UrlConfig.luceneUrl).toPath());
// IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
// indexWriter = new IndexWriter(directory, config);
// } catch (Exception e) {
// e.printStackTrace();
// }
// /**当当前线程结束时自动关闭IndexWriter使用Runtime对象*/
//// Runtime.getRuntime().addShutdownHook(new Thread(){
//// @Override
//// public void run() {
//// try {
//// closeIndexWriter();
//// } catch (Exception e) {
//// e.printStackTrace();
//// }
//// }
//// });
// }
// /**在线程结束时自动关闭IndexWriter*/
// public static IndexWriter getIndexWriter() {
// return indexWriter;
// }
// /**关闭IndexWriter
// * @throws IOException
// * @throws CorruptIndexException */
// public static void closeIndexWriter() throws Exception {
// if(indexWriter != null) {
//// indexWriter.close();
// }
// }
//
// 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);
// timeString = DateUtil.format(date, "yyyy-MM-dd HH:mm:ss.SSS");//格式化后的时间
// return timeString;
// }
//
//
//}

View File

@@ -58,12 +58,12 @@ public class Searcher {
if (startDate == null){
startDate = DateUtil.format(calendar.getTime(),"yyyy-MM-dd HH:mm:ss.SSS");
}else{
startDate = LuceneIndexWriter.getDate(startDate);
startDate = LuceneServiceAutoRun.getDate(startDate);
}
if (endDate == null){
endDate = DateUtil.format(new DateTime(),"yyyy-MM-dd HH:mm:ss.SSS");
} else {
endDate = LuceneIndexWriter.getDate(endDate);
endDate = LuceneServiceAutoRun.getDate(endDate);
}
TermRangeQuery termRangeQuery = new TermRangeQuery("logTime", new BytesRef(startDate), new BytesRef(endDate), true, true);
booleanQueryBuilder.add(termRangeQuery,BooleanClause.Occur.MUST);

View File

@@ -91,4 +91,21 @@ public class LuceneLogDto {
+ "] : "
+ remark;
}
public LuceneLogDto(final String device_code, final String item, final int last_info,
final int info) {
super();
String info_china = "";
switch(info){
case 0: info_china = "未联机";
case 1: info_china = "已联机";
case 2: break;
default:;
}
this.device_code = device_code;
this.content = "信号 ["
+item + "] 发生变更 "
+ last_info + " -> "
+ info;
}
}

View File

@@ -10,7 +10,7 @@ import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.nl.modules.lucene.common.LuceneIndexWriter;
import org.nl.modules.lucene.common.LuceneServiceAutoRun;
import org.nl.modules.lucene.enums.LogTypeEnum;
import org.nl.modules.lucene.service.LuceneExecuteLogService;
import org.nl.modules.lucene.service.dto.LuceneLogDto;
@@ -48,7 +48,7 @@ public class LuceneExecuteLogServiceImpl implements LuceneExecuteLogService {
}
private void addIndex(LuceneLogDto luceneLogDto) throws IOException {
IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter();
IndexWriter indexWriter = LuceneServiceAutoRun.getIndexWriter();
//创建一个Document对象
Document document = new Document();
try {