使用Apache Lucene删除磁盘中的所有索引数据/文件?

时间:2014-07-14 09:21:45

标签: java lucene search-engine flush

如何使用Apache Lucene刷新/删除/擦除磁盘中的所有索引文件/数据。这是我的代码到目前为止仍然无法删除索引文件。请帮助我...

测试:

public class Test {
    private static final String INDEX_DIR = "/home/amila/Lucene/REST/indexing"; 
    public static void main(String[] args) {

         try {
            ContentIndexer contentIndexer = new ContentIndexer(INDEX_DIR);
            contentIndexer.flushDisk();
            System.out.println("Flushed");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

ContentIndexer:

public class ContentIndexer {
    private IndexWriter writer;

    public ContentIndexer(String indexDir) throws IOException {

        // create the index
        if (writer == null) {
            writer = new IndexWriter(FSDirectory.open(new File(indexDir)),
                    new IndexWriterConfig(Version.LUCENE_36,
                            new StandardAnalyzer(Version.LUCENE_36)));
        }
    }

    public void flushDisk() {
        try {
            writer.deleteAll();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

已修改 - 已更新答案

public class Test {
    private static final String INDEX_DIR = "/home/amila/Lucene/REST/indexing";

    public static void main(String[] args) {

        try {
            IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36,
                    new StandardAnalyzer(Version.LUCENE_36));
            conf.setOpenMode(OpenMode.CREATE);
            Directory directory = FSDirectory.open(new File(INDEX_DIR));

            IndexWriter indexWriter  = new IndexWriter(directory, conf);
            indexWriter.deleteAll();
            indexWriter.commit();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

3 个答案:

答案 0 :(得分:5)

您可以使用两个选项:

  1. 您可以调用writer的delete all方法

    indexWriter.DeleteAll();

  2. 您可以创建一个新的indexWriter,并将create标志设置为true(open mode = created)

    new IndexWriter(_luceneDirectory,_analyzer,true,IndexWriter.MaxFieldLength.UNLIMITED);

答案 1 :(得分:4)

最简单的方法是使用CREATE mode(通过IndexWriter)打开indexWriterConfig.setOpenMode(...)。这将删除给定目录中的所有现有索引文件。

对于旧版本IndexWriter,构造函数也有一个特殊的boolean create标志,它可以执行相同的操作。

答案 2 :(得分:0)

只需在LuceneIndex.cs类中的一个方法中使用这行代码,您确定它将被执行。

ClearLuceneIndex();

例如我在那里使用了这段代码

    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.IO;
    using System.Linq;
    using System.Web;
    using Iris.DomainClasses;
    using Iris.ViewModels;
    using Lucene.Net.Analysis.Standard;
    using Lucene.Net.Documents;
    using Lucene.Net.Index;
    using Lucene.Net.QueryParsers;
    using Lucene.Net.Search;
    using Lucene.Net.Search.Similar;
    using Lucene.Net.Store;

    namespace Iris.LuceneSearch
    {
        public class LuceneIndex
        {
          public static IList<ProductWidgetViewModel> GetMoreLikeThisProjectItems(int projectId)
            {
            ClearLuceneIndex();
            Query query = CreateMoreProjectsLikeThisQuery(projectId);
            if (query == null)
                return new List<ProductWidgetViewModel>();
            using (var searcher = new IndexSearcher(_directory, false))
            {

                TopDocs hits = searcher.Search(query, 10);
                var docs = hits.ScoreDocs.Select(item => searcher.Doc(item.Doc)).ToList();
                return (from doc in docs
                        where !string.IsNullOrEmpty(doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.ProductStatus)))
                        select new ProductWidgetViewModel
                        {
                            Name = doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.Title)),
                            Id = Convert.ToInt32(doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.ProductId))),
                            Image = doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.Image)),
                            SlugUrl = doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.SlugUrl)),
                            ProductStatus = (ProductStatus)Enum.Parse(typeof(ProductStatus), doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.ProductStatus)), true),
                            Category = doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.Category)),
                            Price = decimal.Parse(doc.Get(StronglyTyped.PropertyName<LuceneSearchModel>(x => x.Price)))
                        }).ToList();
            }
        }
    }
    }

并且在执行ClearLuceneIndex()之后,您将看到所有lucene缓存都被删除,lucene将被重置。 希望它可以帮助你。