I use Lucene
in my project (and LINQ
) for selecting from Product's table.
This is how I indexing field for Lucene:
var document = new Document();
document.Add(new Field("ID", HeaderSearchModel.ID.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("Summary", HeaderSearchModel.Summary, Field.Store.YES, Field.Index.NOT_ANALYZED));
document.Add(new Field("Picture", HeaderSearchModel.Picture, Field.Store.YES, Field.Index.NOT_ANALYZED));
var title = new Field("Title", HeaderSearchModel.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS);
title.Boost = 3;
document.Add(title);
document.Add(new Field("Description", HeaderSearchModel.Description.RemoveHtmlTags(), Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
The search will be on Title
& Description
fields, my solution to showing the result is:
First I use Lucene to search by Term
(that users requested), the result will be a list of IDs
, then I use this list for querying form DB, and fetching product's fields
than I need to show to the users.
The problem is, in this way, I lost Lucene's order
and result is not match with Lucene's algorithm
.
How I can fix this problem?
Please note: There is no error or fail on Lucene and everything works fine but the ordering of the results.
Update
Result of search using Lucene is a list of found IDs
foreach (var item in LUCENE_RESULT)
{
ids.Add(item.ID);
}
Then I use LINQ, like this:
var query = db.Products.Where(c => ids.Contains(c.ID))....
And get the field for showing the result, I didn't use OrderBy
in LINQ.