lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From 张志田 <zhitian.zh...@dianping.com>
Subject Can not delete index file after close the IndexSearcher
Date Wed, 12 Jan 2011 10:39:50 GMT
Dear Luceners,

I'm using lucene-3.0.2 in our app. There is some testing code for switching
index, however, when my code run a couple of times, I found the index file
was locked, I can not delete the old index files.


The code looks like:

public class SearchTest
{

    private static final int MAX_RESULT = 10000;

    private String indexPath1 = "D:\\index\\index1";
    private String indexPath2 = "D:\\index\\index2";

    private String indexPath = indexPath1;

    private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);

    private Directory dir = null;

    private IndexSearcher searcher;

    public void search()
    {
        while(true)
        {
            try
            {
                String keyword = "test";
                String fieldName = "searchfield";

                if(dir == null)
                {
                    dir = FSDirectory.open(new File(indexPath));
                }
                searcher = new IndexSearcher(dir);

                QueryParser queryParse = new QueryParser(Version.LUCENE_30,
fieldName, analyzer);
                Query query = queryParse.parse(keyword);

                TopDocs hits = searcher.search(query, MAX_RESULT);
                int size = 5;
                if(hits.scoreDocs.length < size)
                {
                    size = hits.scoreDocs.length;
                }
                for (int i = 0; i < size; i++)
                {
                    Document doc = searcher.doc(hits.scoreDocs[i].doc);
                    String text = doc.get(fieldName);
                    System.out.println("fieldContent is: " + text);
                }

                IndexSearcher oldSearcher = searcher;
                dir = FSDirectory.open(new File(getIndexPath()));
                IndexSearcher newSearcher = new IndexSearcher(dir);
                searcher = newSearcher;

                oldSearcher.close();
                System.out.println("Closed Searcher: " +
oldSearcher.getIndexReader().directory().toString());

                System.out.println("input 'Q' to quit testing...");
                BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));

                if(br.readLine().trim().equals("Q"))
                {
                    break;
                }
            }
            catch (CorruptIndexException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
            catch (ParseException e)
            {
                e.printStackTrace();
            }
        }
    }

    private String getIndexPath()
    {
        if(indexPath.equals(indexPath1))
        {
            indexPath = indexPath2;
        }
        else
        {
            indexPath = indexPath1;
        }

        return indexPath;
    }

    public static void main(String[] args)
    {
        SearchTest searchTest = new SearchTest();
        searchTest.search();
    }

}

Can anybody take a look at the above code snippet?

I want to search on the different index file every time so I created two
different folders and switch them time to time. The index files in the
index1/index2 maybe replaced before the search request comes.

The problem I found is after I ran the above code 2 or more loops, I can not
modify/delete the cfs/cfx file in the file system(Windows 2003), although I
closed the searcher every time in the code. It seems that the index file is
not released.

Is the problem caused by the shared reference of searcher? or some shared
thread in the lucene?

Thanks in advance!
Garry

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message