1.pom.xml:
java
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>hui</groupId> <artifactId>Lucene</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Lucene</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-highlighter</artifactId> <version>6.0.0</version> </dependency> </dependencies> </project>
package luceneTest; import java.io.File; import java.util.ArrayList; import java.util.List; public class ListFiles { private static List<File> filesResult = new ArrayList<>(); public static void main(String[] args) { List<File> files = listFiles("e:/eclipse/OOP"); System.out.println(files.size()); for (File f : files) { System.out.println(f.getName()); } } public static List<File> listFiles(String srcPath) { File file = new File(srcPath); File[] files = file.listFiles(); for (File f : files) { if (f.isDirectory()) { listFiles(f.getAbsolutePath()); } else { filesResult.add(f); } } return filesResult; } }
package luceneTest; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.nio.file.Paths; import java.util.List; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class FilesTest { public static void main(String[] args) throws ParseException, IOException { String srcPath = "E:/eclipse/OOP"; String destPath = "e:/test/index"; // createIndexTo(srcPath, destPath); read(destPath, "fileContent", "string"); } public static void read(String indexPath, String fieldName, String parseString) throws IOException, ParseException { Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.open(Paths.get(indexPath)); // Now search the index: DirectoryReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); process(searcher, analyzer, fieldName, parseString); reader.close(); directory.close(); } private static void process(IndexSearcher searcher, Analyzer analyzer, String fieldName, String parseString) throws ParseException, IOException { QueryParser parser = new QueryParser(fieldName, analyzer); Query query = parser.parse(parseString); ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs; System.out.println(hits.length); for (int i = 0; i < hits.length; i++) { Document hitDoc = searcher.doc(hits[i].doc); System.out.println(hitDoc.get("filePath")); } } public static void createIndexTo(String srcPath, String destPath) throws IOException { Analyzer analyzer = new StandardAnalyzer(); Directory directory = FSDirectory.open(Paths.get(destPath)); IndexWriterConfig config = new IndexWriterConfig(analyzer); IndexWriter writer = new IndexWriter(directory, config); List<File> files = ListFiles.listFiles(srcPath); FileReader fileReader = null; for (File temp : files) { if (temp.isFile()) { fileReader = new FileReader(temp); Document doc = new Document(); doc.add(new Field("fileName", temp.getName(), TextField.TYPE_STORED)); doc.add(new Field("fileContent", fileReader, TextField.TYPE_NOT_STORED)); doc.add(new Field("filePath", temp.getAbsolutePath(), TextField.TYPE_STORED)); System.out.println(temp.getAbsolutePath() + " indexed"); writer.addDocument(doc); } } writer.close(); } }