0
votes

I am using Lucene.Net 3.0.3. I'm trying to sort my result by product price and stock. But the sorting doesn't seem to be doing anything. I tried debugging the lucene searcher.Search() in Visual Studio, but this will eventually throw a debug exception: http://i62.tinypic.com/m7zyi9.png

My code looks like this:

Adding the fields to the document:

doc.Add(new NumericField("Price", Field.Store.YES, true).SetFloatValue(productLucene.Price));
doc.Add(new NumericField("Stock", Field.Store.YES, true).SetIntValue(productLucene.Stock));

Creating the sort:

switch (sortField)
{
    case "Price":
        sortFieldType = SortField.FLOAT;
        break;
    case "Stock":
        sortFieldType = SortField.INT;
        break;
}
sort = new Sort(new SortField(sortField, sortFieldType, false));

Sort the result set:

var hits = searcher.Search(booleanQuery, null, _hitsLimit, sort).ScoreDocs;
var results = _mapLuceneToDataList(hits, searcher);

I also tried sorting by string and String_val, but this doesn't make any difference. I know the fields should be indexed to be able to sort. I know the field should be untokenized, but I can't find the untokenized index option in lucene.net 3.0.3. Any help would be appreciated.

1
what do you mean by saying sort is not working? could you sort by one field?Mysterion
@Mysterion The order in wich my data is displayed doesn't seem to change with the sort. So basically the searcher.search() returns the data the same way with or without the sort.Donny
also stuck on this. tried every combination of Index and Store..still no diceanaval

1 Answers

0
votes

For me, sorting also didn't go as I expected. The following solution from another stackoverflow thread helped me out:

...sorting will not work until the following conditions are met :

1) You have to specify the type parameter of SortField(String field, int type) to make Lucene find your field, even if this is normaly optional.

2) The sort fields must be indexed but not tokenized:

document.add (new Field ("byNumber", Integer.toString(x), Field.Store.NO, Field.Index.NOT_ANALYZED));

3) The sort fields content must be plain text only. If only one single element has a special character or accent in one of the fields used for sorting, the whole search will return unsorted results.

I added an extra field for sorting with the above configuration: document.add(new Field("MunicipalitySort", value, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS));