|
|
@@ -3,8 +3,8 @@ import ch.qos.logback.classic.Logger;
|
|
|
import ch.qos.logback.classic.LoggerContext;
|
|
|
import cn.reghao.jutil.jdk.db.PageList;
|
|
|
import cn.reghao.jutil.tool.id.SnowFlake;
|
|
|
+import cn.reghao.tnb.search.api.dto.IndexCount;
|
|
|
import cn.reghao.tnb.search.app.SearchApplication;
|
|
|
-import cn.reghao.tnb.search.app.es.*;
|
|
|
import cn.reghao.tnb.search.app.lucene.LuceneDocument;
|
|
|
import cn.reghao.tnb.search.app.lucene.LuceneIndex;
|
|
|
import cn.reghao.tnb.search.app.lucene.LuceneSearch;
|
|
|
@@ -12,21 +12,18 @@ import cn.reghao.tnb.search.app.model.po.Wenshu;
|
|
|
import cn.reghao.tnb.search.app.model.po.WenshuLucene;
|
|
|
import cn.reghao.tnb.search.app.model.vo.ElasticQuery;
|
|
|
import cn.reghao.tnb.search.app.service.WenshuService;
|
|
|
-import cn.reghao.tnb.search.app.util.ClassUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.lucene.document.Document;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
-import org.springframework.data.domain.Page;
|
|
|
import org.springframework.test.context.ActiveProfiles;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
import java.io.IOException;
|
|
|
-import java.nio.ByteBuffer;
|
|
|
-import java.nio.channels.FileChannel;
|
|
|
+import java.nio.file.*;
|
|
|
+import java.nio.file.attribute.BasicFileAttributes;
|
|
|
import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@@ -45,18 +42,6 @@ public class WenshuTest {
|
|
|
}
|
|
|
|
|
|
SnowFlake idGenerator = new SnowFlake(1, 1);
|
|
|
- @Autowired
|
|
|
- ElasticService elasticService;
|
|
|
- @Autowired
|
|
|
- IndexService indexService;
|
|
|
- @Autowired
|
|
|
- MappingService mappingService;
|
|
|
- @Autowired
|
|
|
- DocumentService documentService;
|
|
|
- @Autowired
|
|
|
- QueryService<Wenshu> queryService;
|
|
|
- String indexName = Wenshu.class.getSimpleName().toLowerCase(Locale.ROOT);
|
|
|
-
|
|
|
private void addLuceneIndex(List<Wenshu> wenshuList) {
|
|
|
long id = idGenerator.nextId();
|
|
|
List<Document> luceneDocumentList = wenshuList.stream()
|
|
|
@@ -68,14 +53,6 @@ public class WenshuTest {
|
|
|
.map(wenshu -> luceneDocument.getDocumentByWenshu(wenshu))
|
|
|
.collect(Collectors.toList());
|
|
|
|
|
|
- /*luceneDocumentList.forEach(doc -> {
|
|
|
- try {
|
|
|
- luceneIndex.createIndex(doc);
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- });*/
|
|
|
-
|
|
|
String indexName = "wenshu";
|
|
|
try {
|
|
|
luceneIndex.createIndex(indexName, wenshuDocumentList);
|
|
|
@@ -92,57 +69,56 @@ public class WenshuTest {
|
|
|
LuceneDocument luceneDocument;
|
|
|
@Autowired
|
|
|
WenshuService wenshuService;
|
|
|
- @Test
|
|
|
- public void addTest() throws IOException {
|
|
|
- String indexName = "wenshu";
|
|
|
- /*indexService.deleteIndex(indexName);
|
|
|
- Map<String, Property> propertyMap = mappingService.getPropertyMapByWenshu();
|
|
|
- indexService.createIndex(indexName, propertyMap);*/
|
|
|
-
|
|
|
- //documentService.deleteAllDocument(indexName);
|
|
|
- String filePath = "/home/reghao/Downloads/2021年07月裁判文书数据.csv";
|
|
|
- //readByFileChannel(filePath, documentService);
|
|
|
- wenshuService.processFile(filePath);
|
|
|
- }
|
|
|
|
|
|
- @Test
|
|
|
- public void deleteAllTest() throws IOException {
|
|
|
- wenshuService.deleteAll();
|
|
|
+ public void walkDir(Path path) throws IOException {
|
|
|
+ Files.walkFileTree(path, new FileVisitor<Path>() {
|
|
|
+ @Override
|
|
|
+ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
|
|
+ File file1 = file.toFile();
|
|
|
+ String filePath = file1.getAbsolutePath();
|
|
|
+ if (filePath.endsWith("csv")) {
|
|
|
+ wenshuService.processFile(filePath);
|
|
|
+ log.info("index {} done...", filePath);
|
|
|
+ }
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
|
|
|
+ return FileVisitResult.CONTINUE;
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void queryTest() {
|
|
|
- setLogLevel();
|
|
|
+ public void indexWenshuLucene() throws IOException {
|
|
|
+ String indexName = "wenshu_lucene";
|
|
|
+ String baseDir = "/home/reghao/disk/2/wenshu/";
|
|
|
+ Path path = Paths.get(baseDir);
|
|
|
+ walkDir(path);
|
|
|
|
|
|
- int pn = 1;
|
|
|
- int ps = 10;
|
|
|
- String queryString = "贩毒";
|
|
|
- ElasticQuery elasticQuery = new ElasticQuery.Builder()
|
|
|
- .pageSize(10)
|
|
|
- .pageNumber(1)
|
|
|
- .indexName(indexName)
|
|
|
- .queryFiledNames(List.of("caseName", "fullText"))
|
|
|
- .highlightFiledNames(List.of("caseName", "fullText"))
|
|
|
- .queryString(queryString)
|
|
|
- .build();
|
|
|
+ IndexCount indexCount = luceneIndex.countIndex(indexName);
|
|
|
+ System.out.printf("max: %s, num: %s\n", indexCount.getMaxDocs(), indexCount.getNumDocs());
|
|
|
+ wenshuService.printResult();
|
|
|
+ }
|
|
|
|
|
|
- Page<Wenshu> page = queryService.queryWithHighlight(elasticQuery, Wenshu.class);
|
|
|
- long total = page.getTotalElements();
|
|
|
- int totalPages = page.getTotalPages();
|
|
|
- while (pn <= totalPages) {
|
|
|
- elasticQuery.setPageNumber(pn);
|
|
|
- page = queryService.queryWithHighlight(elasticQuery, Wenshu.class);
|
|
|
- List<Wenshu> list = page.getContent();
|
|
|
- Wenshu wenshu = list.get(0);
|
|
|
- String id = wenshu.getId();
|
|
|
- String caseName = wenshu.getCaseName();
|
|
|
- System.out.printf("%s -> %s\n", id, caseName);
|
|
|
- pn++;
|
|
|
- }
|
|
|
+ @Test
|
|
|
+ public void deleteAllWenshuLucene() throws IOException {
|
|
|
+ wenshuService.deleteAll();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void queryTest1() throws Exception {
|
|
|
+ public void queryTest() {
|
|
|
setLogLevel();
|
|
|
|
|
|
String indexName = "wenshu_lucene";
|