维护lucene索引库

删除所有索引:

/创建目录Directory 对象 连接
        try {
            Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
            Analyzer analyzer = new IKAnalyzer();
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
            indexWriter.deleteAll();
            indexWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

删除某一类索引:

try {
            Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
            Analyzer analyzer = new IKAnalyzer();
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
            Query query = new TermQuery(new Term("fileName","java"));
            indexWriter.deleteDocuments(query);
            indexWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

修改索引(先删除,再添加):

try {
            Directory directory = FSDirectory.open(Paths.get("D:\\Java\\lucene\\index"));
            Analyzer analyzer = new IKAnalyzer();
            IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer);
            IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
            Document document = new Document();
            document.add(new TextField("fileName","测试测试",Store.YES));
            indexWriter.updateDocument(new Term("fileName","spring" ), document);
            indexWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 

你可能感兴趣的:(lucene&solr,删除lucene索引,修改lucene索引)