欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java 基于lucene 如何创建index【索引】

程序员文章站 2022-07-09 09:35:36
...
    /**
     * 基于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();
    }