Salem
Salem

Reputation: 321

How to optimize Hits Object looping in Lucene.Net

I am trying to build a custom search engine using Lucene.Net.

All seems to be going fine but i'm facing a problem while looping the Hits Object returned by the Search function. Whenever I search for an item it takes around 0.07 seconds for the Search Function and between 20 to 90 seconds to loop into the returned object.

ex: (for a total of 11384 results returned)
Search took 0.068 seconds.
Object Looping took 25.50 seconds.

Now the second time I loop this same object it takes much less time (for the same above case it took 0.141 seconds.

Here's the Search function:

public List<SearchResultId> SearchId(string searchTerm)
    {
        IndexSearcher indexSearcher = new IndexSearcher(_indexDir);
        List<SearchResultId> searchList = new List<SearchResultId>();

        try
        {
            QueryParser queryParser = new QueryParser(_fieldName, new StandardAnalyzer());

            Query query = queryParser.Parse(searchTerm);
            Hits hits = indexSearcher.Search(query);
            int numHits = hits.Length();
            SearchResultId search;
            for (int i = 0; i < numHits; ++i)
            {
                search = new SearchResultId();
                search.score = hits.Score(i);
                search.id = hits.Doc(i).Get(LuceneIndexer.DOC_ID_FIELD_NAME);
                searchList.Add(search);
            }
        }
        catch
        {
            indexSearcher.Close();
        }
        indexSearcher.Close();
        return searchList;
    }

Is this normal to happen? Is there a way to optimize this process?

Thanks

Upvotes: 1

Views: 88

Answers (1)

Rishi Dua
Rishi Dua

Reputation: 2334

It is normal as the second query is always faster because of caching. There are a lot of useful tips out here that might help you to optimize.

The ones that helped me the most were:

  1. Make sure you are using the latest version of Lucene.
  2. Use a local filesystem.
  3. Open the IndexReader with readOnly=true.
  4. On non-Windows platform, using NIOFSDirectory instead of FSDirectory.
  5. Use one instance of IndexSearcher.

Upvotes: 1

Related Questions