1. 程式人生 > >java 基於lucene 如何建立index【索引】

java 基於lucene 如何建立index【索引】

    /**
     * 基於lucene 如何建立index【索引】
     * @param indexPath  索引檔案路徑
     * @param analyzer   分詞器
     * @param list       文件物件集合
     * @throws Exception
     */
    public static void createIndex(String indexPath,Analyzer analyzer,List<Document> list) throws Exception{
        Directory directory=FSDirectory.open(new File(indexPath));
//        Analyzer analyzer=new StandardAnalyzer();
//        Document document=new Document();
//        document.add(new Field("name","chenxiaoyang", Field.Store.YES, Field.Index.ANALYZED));
//        document.add(new Field("address","beijing", Field.Store.YES, Field.Index.ANALYZED));
//        document.add(new Field("sex","man" ,Field.Store.YES, Field.Index.ANALYZED));
//        document.add(new Field("introduce"," i am chenxuyuan",Field.Store.YES,Field.Index.ANALYZED));
        IndexWriter indexWriter=new IndexWriter(directory,analyzer, IndexWriter.MaxFieldLength.LIMITED);
        for (Document document:list){
            indexWriter.addDocument(document);
        }
        indexWriter.close();
    }