Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug when the array of SortFields contains a null SortField on it #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,27 @@ private static SortField convert(Browsable browser, SortField sort) {
}
}

protected static SortField buildSortFieldFromQuery(Query q) {
if (q != null && !(q instanceof MatchAllDocsQuery)) {
return SortField.FIELD_SCORE;
} else {
return SortField.FIELD_DOC;
}
}

public static SortCollector buildSortCollector(Browsable browser, Query q, SortField[] sort,
int offset, int count, boolean fetchStoredFields, Set<String> termVectorsToFetch,
String[] groupBy, int maxPerGroup, boolean collectDocIdCache) {
if (sort == null || sort.length == 0) {
if (q != null && !(q instanceof MatchAllDocsQuery)) {
sort = new SortField[] { SortField.FIELD_SCORE };
} else {
sort = new SortField[] { SortField.FIELD_DOC };
}
sort = new SortField[] { buildSortFieldFromQuery(q) };
}

boolean doScoring = false;
for (SortField sf : sort) {
if (sf == null) {
sf = buildSortFieldFromQuery(q);
}

if (sf.getType() == SortField.Type.SCORE) {
doScoring = true;
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.browseengine.bobo.sort;

import junit.framework.TestCase;

import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.SortField;

/**
* @author mdelapenya
*/
public class SortCollectorTest extends TestCase {

public void testBuildSortFieldFromQuery() {
assertEquals(SortCollector.buildSortFieldFromQuery(null), SortField.FIELD_DOC);
assertEquals(SortCollector.buildSortFieldFromQuery(new MatchAllDocsQuery()), SortField.FIELD_DOC);
assertEquals(SortCollector.buildSortFieldFromQuery(new BooleanQuery()), SortField.FIELD_SCORE);
}
}