1

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.

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64
  • How are you querying using the result list of lucene ? that could be important for the end result order! could you please provide code for that ? – Maxim Fleitling Feb 14 '16 at 12:18
  • Have you tried reordering the returned products based on the list of identifiers you already have? – sisve Feb 14 '16 at 13:00
  • You can reorder your db-results via "ID" to match the lucene order. Or your can solve this db-problem: http://stackoverflow.com/questions/2185029/sort-by-order-of-values-in-a-select-statement-in-clause-in-mysql – Karsten R. Feb 14 '16 at 17:26
  • @SimonSvensson I didn't get you, but if I log the `lucene` result before re-selecting form db, everything is fine, but I need to re-select, because I can not index all fields with lucene. – Mehdi Dehghani Feb 14 '16 at 18:25
  • @KarstenR. what you mean of this: "You can reorder your db-results via "ID" to match the lucene order", how? btw, I use MS SQL, not mySQL – Mehdi Dehghani Feb 14 '16 at 18:25
  • @MehdiDehghani, I understand. But isn't this just about doing the sort again with the database results, based on the list of identifiers you got from Lucene? – sisve Feb 14 '16 at 18:26
  • Yes, the number of result is fine, just I need to get same order as lucene did – Mehdi Dehghani Feb 14 '16 at 18:28
  • 1
    I mean just this: you can put your LINQ-Results in main memory as list or array and reorder this to the original lucene order, because you have the same unique key in lucene list and LINQ-Results. – Karsten R. Feb 14 '16 at 19:06

1 Answers1

3

You can get the original order by linq join of your query result to your ids collection:

var orderedResult = from i in ids
                join a in query on i equals a.ID
                select a;

The leading collection(in your case "ids") will be the master for sorting order.

Maxim Fleitling
  • 542
  • 4
  • 14