Lucene6.0 查詢File實例

先取出文件夾下的全部File,而後給這些File建立索引,最後根據條件查詢

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>

2.遞歸取出文件夾下的全部文件ListFiles.java:

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;
	}
}

3.根據本身的條件建立索引+查詢:FilesTest.java:

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();
	}

}
相關文章
相關標籤/搜索