轉載出處:http://blog.csdn.net/ayi_5788/article/details/52120846
簡述:
由於Lucene不同的版本差距較大,,此係列教程打算把3.5版本,4.5版本,5.0版本都給出個例子,方便大家學習,也方便自己複習。
注:由於Lucene5.0版本是基於JDK1.7開發的,所以想學習的同學請配置1.7及以上的版本。故測試Lucene 6.1.0也適用Lucene 5.0中的代碼。Lucene 6.1.0最低要求也是JDK1.7.
創建索引可分爲主要的幾步,我自己試驗過,不同的版本間會有些不同,但是跟着如下的幾大步驟一步一步寫,問題不會太大。
創建Directory
創建IndexWriter
創建Document對象
爲Document添加Field
通過IndexWriter添加文檔到索引中
下面爲例子代碼:
3.5版本:
3.5版本比較簡單,只需要Lucene核心包lucene-core即可,
例子代碼如下:
package com.darren.lucene35;
import java.io.File;
import java.io.FileReader;
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.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.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;
import org.apache.lucene.util.Version;
public class HelloLucene {
/**
* 建立索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
// 1、創建Directory
// 建立在內存中
// Directory directory = new RAMDirectory();
// 建立在硬盤式
Directory directory = FSDirectory.open(new File("F:/test/lucene/index"));
// 2、創建IndexWriter
// 使用默認的標準分詞器
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
indexWriter = new IndexWriter(directory, indexWriterConfig);
File dFile = new File("F:/test/lucene/document");
File[] files = dFile.listFiles();
for (File file : files) {
// 3、創建Document對象
Document document = new Document();
// 4、爲Document添加Field
document.add(new Field("content", new FileReader(file)));
document.add(new Field("filename", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("filepath", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
// 5、通過IndexWriter添加文檔到索引中
indexWriter.addDocument(document);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (indexWriter != null) {
indexWriter.close();
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
- 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
- 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
4.5版本:
4.5版本需要Lucene核心包lucene-core和分詞包lucene-analyzers-common,從4.0版本之後分詞包從核心包分離。
代碼如下:
package com.darren.lucene45;
import java.io.File;
import java.io.FileReader;
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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class HelloLucene {
/**
* 建立索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
// 1、創建Directory
Directory directory = FSDirectory.open(new File("F:/test/lucene/index"));
// 2、創建IndexWriter
/**
* 注意StandardAnalyzer與3.5版本的不同:
*
* StandardAnalyzer不在lucene-core包中而在lucene-analyzers-common包中 從4.0版本以後分離
*/
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_45, analyzer);
indexWriter = new IndexWriter(directory, indexWriterConfig);
File dFile = new File("F:/test/lucene/document");
File[] files = dFile.listFiles();
for (File file : files) {
// 3、創建Document對象
Document document = new Document();
// 4、爲Document添加Field
/**
* 注意Field與3.5版本的不同:兩個參數的構造器已過時,使用如下構造器
*/
// 第三個參數是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();
}
}
}
}
- 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
- 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
5.0版本:
5.0版本和4.5版本一樣,例子代碼如下:
package com.darren.lucene50;
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.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class HelloLucene {
/**
* 建立索引
*/
public void index() {
IndexWriter indexWriter = null;
try {
// 1、創建Directory
/**
* 注意open方法與3.5版本和4.5版本的不同:
*
* 這裏不再接受一個File而是Path,使用的是JDK1.7的新特性,也就是說5.0版本是基於JDK1.7開發的
*
* 如何獲取Path,請參照 Java7新特性--Path http://blog.csdn.net/zpf336/article/details/45074445
*
*/
Directory directory = FSDirectory.open(FileSystems.getDefault().getPath("F:/test/lucene/index"));
// 2、創建IndexWriter
/**
* 注意StandardAnalyzer與3.5版本的不同:
*
* StandardAnalyzer不在lucene-core包中而在lucene-analyzers-common包中 從4.0版本以後分離
*
* 並且不需要提供版本號
*/
Analyzer analyzer = new StandardAnalyzer();
/**
* 注意IndexWriterConfig與3.5版本和4.5版本的不同:
*
* 不需要提供版本號
*/
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
indexWriter = new IndexWriter(directory, indexWriterConfig);
File dFile = new File("F:/test/lucene/document");
File[] files = dFile.listFiles();
for (File file : files) {
// 3、創建Document對象
Document document = new Document();
// 4、爲Document添加Field
/**
* 注意Field與3.5版本的不同:兩個參數的構造器已過時,使用如下構造器
*
* 但是和4.5版本是相同的
*/
// 第三個參數是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();
}
}
}
}
- 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
- 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
測試代碼:
由於測試代碼都一樣,僅貼出5.0版本的此時代碼:
package com.darren.lucene50;
import org.junit.Test;
public class HelloLuceneTest {
@Test
public void testIndex() {
HelloLucene helloLucene = new HelloLucene();
helloLucene.index();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12