基於jar lucene3.6.2
Field.Store.YES / NO --- 存儲選項
設置爲YES表示把這個域中的內容完全存儲到文件中,方便進行文本的還原
設置爲NO表示把這個域的內容不存儲到文件中,但是可以被索引,此時內容無法完全還原
Field.Index. --- 索引選項
ANALYZED 進行分詞和索引,適用於標題,內容等
NOT_ANALYZED 進行索引,但不進行分詞,適用於精確搜索
ANALYZED_NOT_NORMS 進行分詞但是不存儲norms信息,這個norms中包括了創建索引的時間和全職等信息
NOT_ANALYZED_NOT_NORMS 既不進行分詞也不存儲norms信息
NO 不進行索引
下面直接通過例子說明問題
- IndexUtil.java
- import java.io.File;
- import java.io.IOException;
- import org.apache.lucene.analysis.standard.StandardAnalyzer;
- import org.apache.lucene.document.Document;
- import org.apache.lucene.document.Field;
- import org.apache.lucene.index.CorruptIndexException;
- import org.apache.lucene.index.IndexReader;
- 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.store.LockObtainFailedException;
- import org.apache.lucene.util.Version;
- public class IndexUtil {
- private String[] ids = {"1","2","3","4","5","6"};
- private String[] emails = {"[email protected]","[email protected]","[email protected]","[email protected]","[email protected]","[email protected]"};
- private String[] content ={
- "Welcome to my home",
- "Hello Kenan",
- "Good morning",
- "Are you OK?",
- "Yeah hahahahahahaha",
- "I like foot ball"
- };
- private int[] attachs = {1,4,6,2,3,8};
- private String[] names = {"zhangsan","kenan","soukenan","Micheal","Liangchengpeng","Jah"};
- //詞典
- private Directory directory = null;
- //寫入筆
- private IndexWriter writer = null;
- //文檔對象
- private Document doc = null;
- //讀取對象
- private IndexReader reader = null;
- public IndexUtil(){
- try {
- this.directory = FSDirectory.open(new File("d:/lucene/index02"));
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /**
- * 構建索引
- */
- public void buildIndex(){
- try {
- writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_36)));
- for(int i=0;i<6;i++){
- doc = new Document();
- doc.add(new Field ("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- doc.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));
- doc.add(new Field("content",this.content[i],Field.Store.NO,Field.Index.ANALYZED));
- doc.add(new Field("name",this.names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
- writer.addDocument(doc);
- }
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (LockObtainFailedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally{
- if(writer != null ){
- try {
- writer.close();
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- public void query(){
- try {
- this.reader = IndexReader.open(directory);
- System.out.println("maxDoc:"+reader.maxDoc());
- System.out.println("numDocs:"+reader.numDocs());
- } catch (CorruptIndexException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- 測試類 TestCase.java
- import static org.junit.Assert.*;
- import org.junit.BeforeClass;
- import org.junit.Test;
- public class TestCase {
- @BeforeClass
- public static void setUpBeforeClass() throws Exception {
- }
- @Test
- public void testBuildIndex() {
- new IndexUtil().buildIndex();
- }
- @Test
- public void testQuery(){
- new IndexUtil().query();
- }
- }
本文出自 “Kenan_ITBlog” 博客,請務必保留此出處http://soukenan.blog.51cto.com/5130995/1119923