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

View File

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

View File

@@ -91,4 +91,21 @@ public class LuceneLogDto {
+ "] : " + "] : "
+ remark; + 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.Field;
import org.apache.lucene.document.StringField; import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter; 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.enums.LogTypeEnum;
import org.nl.modules.lucene.service.LuceneExecuteLogService; import org.nl.modules.lucene.service.LuceneExecuteLogService;
import org.nl.modules.lucene.service.dto.LuceneLogDto; import org.nl.modules.lucene.service.dto.LuceneLogDto;
@@ -48,7 +48,7 @@ public class LuceneExecuteLogServiceImpl implements LuceneExecuteLogService {
} }
private void addIndex(LuceneLogDto luceneLogDto) throws IOException { private void addIndex(LuceneLogDto luceneLogDto) throws IOException {
IndexWriter indexWriter = LuceneIndexWriter.getIndexWriter(); IndexWriter indexWriter = LuceneServiceAutoRun.getIndexWriter();
//创建一个Document对象 //创建一个Document对象
Document document = new Document(); Document document = new Document();
try { try {