lucene--索引的创建

3/8/2017来源:ASP.NET技巧人气:2674

public class IndexUtil { PRivate String[] ids = { "1", "2", "3", "4", "5", "6" }; private String[] emails = { "aa@QQ.com", "bb@sina.com", "cc@nuc.com", "dd@qq.com", "ee@sina.com", "ff@nuc.com" }; private String[] contents = { "My father was a self-taught mandolin player.", "Love your parents. We are too busy growing up yet we forget that they are already growing old", "Age has reached the end of the beginning of a Word.", "As we drove off from Columbia, I wanted to write a letter to you to tell you all that is on my mind.", "The past is gone and static. Nothing we can do will change it. ", "I will persist until I succeed." }; private int[] attachs = { 3, 1, 8, 5, 2, 7 }; private String[] names = { "zhangsan", "lisi", "wangwu", "Tom", "jack", "Rose" }; private Directory directory = null; public IndexUtil() { try { directory = FSDirectory.open(new File("D:/lucene/index02")); } catch (IOException e) { e.printStackTrace(); } } public void index() { IndexWriter writer = null; Document document = null; try { writer = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35))); for(int i = 0; i < ids.length; i++){ document = new Document(); document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); document.add(new Field("email", emails[i], Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new Field("content", contents[i], Field.Store.NO, Field.Index.ANALYZED)); document.add(new Field("name", names[i], Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); writer.addDocument(document); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if(writer != null){ writer.close(); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }