Reputation: 321
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
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:
Upvotes: 1