lucene--索引的创建

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

猜你喜欢

转载自blog.csdn.net/guozhangjie1992/article/details/60370431
今日推荐