轉載出處:http://blog.csdn.net/ayi_5788/article/details/52126481
根據Lucene 6.1.0 及JDK 1.8最新版本,結合前幾篇文章所寫的最新Demo.
代碼如下:
package HighLightSearch;
import java.io.File;
import java.io.FileReader;
import java.nio.file.FileSystems;
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.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
class Index {
// 建立索引
public void index() {
IndexWriter indexWriter = null;
try {
// 1、創建Directory
//JDK 1.7以後 open只能接收Path
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("C:/Users/jiangl10/Desktop/Java-Workspace/LuceneDemo/data/index"));
// 2、創建IndexWriter
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(directory, indexWriterConfig);
indexWriter.deleteAll();//清除以前的index
//要搜索的File路徑
File dFile = new File("C:/Users/jiangl10/Desktop/Java-Workspace/LuceneDemo/data/data");
File[] files = dFile.listFiles();
for (File file : files) {
// 3、創建Document對象
Document document = new Document();
// 4、爲Document添加Field
// 第三個參數是FieldType 但是定義在TextField中作爲靜態變量,看API也不好知道怎麼寫
document.add(new Field("content", new FileReader(file), TextField.TYPE_NOT_STORED));
document.add(new Field("filename", file.getName(), TextField.TYPE_STORED));
document.add(new Field("filepath", file.getAbsolutePath(), TextField.TYPE_STORED));
// 5、通過IndexWriter添加文檔到索引中
indexWriter.addDocument(document);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null) {
indexWriter.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/*
* 創建Directory
創建IndexReader
根據IndexReader創建IndexSearch
創建搜索的Query
根據searcher搜索並且返回TopDocs
根據TopDocs獲取ScoreDoc對象
根據searcher和ScoreDoc對象獲取具體的Document對象
根據Document對象獲取需要的值
*
*/
class Search {
/**
* 搜索
*/
public void search(String keyWord) {
DirectoryReader directoryReader = null;
try {
// 1、創建Directory
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("C:/Users/jiangl10/Desktop/Java-Workspace/LuceneDemo/data/index"));
// 2、創建IndexReader
directoryReader = DirectoryReader.open(directory);
// 3、根據IndexReader創建IndexSearch
IndexSearcher indexSearcher = new IndexSearcher(directoryReader);
// 4、創建搜索的Query
Analyzer analyzer = new StandardAnalyzer();
// 創建parser來確定要搜索文件的內容,第一個參數爲搜索的域
QueryParser queryParser = new QueryParser("content", analyzer);
// 創建Query表示搜索域爲content包含UIMA的文檔
Query query = queryParser.parse(keyWord);
// 5、根據searcher搜索並且返回TopDocs
TopDocs topDocs = indexSearcher.search(query, 10);
System.out.println("查找到的文檔總共有:"+topDocs.totalHits);
// 6、根據TopDocs獲取ScoreDoc對象
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
// 7、根據searcher和ScoreDoc對象獲取具體的Document對象
Document document = indexSearcher.doc(scoreDoc.doc);
// 8、根據Document對象獲取需要的值
System.out.println(document.get("filename") + " " + document.get("filepath"));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (directoryReader != null) {
directoryReader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public class HighLightSearch {
public static void main(String args[]) {
Index newIndex = new Index();
newIndex.index();
Search newSearch = new Search();
newSearch.search("UIMA");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
運行結果及所用JRE如下所圖: