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

BOBO-286 Make SortCollector more extendable #15

Open
wants to merge 2 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
@@ -0,0 +1,28 @@
/*
* Copyright 2013 jonathan.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.browseengine.bobo.sort;

import com.browseengine.bobo.api.Browsable;
import java.util.Set;
import org.apache.lucene.search.SortField;

public class DefaultSortCollectorFactory implements SortCollectorFactory {

@Override
public SortCollector create(DocComparatorSource compSource, SortField[] sortFields, Browsable boboBrowser, int offset, int count, boolean doScoring, boolean fetchStoredFields, Set<String> termVectorsToFetch, String[] groupBy, int maxPerGroup, boolean collectDocIdCache) {
return new SortCollectorImpl(compSource, sortFields, boboBrowser, offset, count, doScoring, fetchStoredFields, termVectorsToFetch, groupBy, maxPerGroup, collectDocIdCache);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ public void clearRuntimeFacetData() {
public LinkedList<float[]> scorearraylist;

public static int BLOCK_SIZE = 4096;
private static SortCollectorFactory _sortCollectorFactory = new DefaultSortCollectorFactory();

protected Collector _collector = null;
protected final SortField[] _sortFields;
Expand Down Expand Up @@ -232,9 +233,15 @@ private static SortField convert(Browsable browser, SortField sort) {
}
}

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) {
return buildSortCollector(browser, q, sort, offset, count, fetchStoredFields, termVectorsToFetch, groupBy, maxPerGroup, collectDocIdCache, _sortCollectorFactory);
}

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) {
String[] groupBy, int maxPerGroup, boolean collectDocIdCache, SortCollectorFactory sortCollectorFactory) {
if (sort == null || sort.length == 0) {
if (q != null && !(q instanceof MatchAllDocsQuery)) {
sort = new SortField[] { SortField.FIELD_SCORE };
Expand Down Expand Up @@ -262,10 +269,17 @@ public static SortCollector buildSortCollector(Browsable browser, Query q, SortF
}
compSource = new MultiDocIdComparatorSource(compSources);
}
return new SortCollectorImpl(compSource, sort, browser, offset, count, doScoring,
return sortCollectorFactory.create(compSource, sort, browser, offset, count, doScoring,
fetchStoredFields, termVectorsToFetch, groupBy, maxPerGroup, collectDocIdCache);
}

public static void setSortCollectorFactory(SortCollectorFactory sortCollectorFactory) {
if (sortCollectorFactory == null) {
throw new IllegalArgumentException("null sortCollectorFactory is not supported.");
}
_sortCollectorFactory = sortCollectorFactory;
}

public SortCollector setCollector(Collector collector) {
_collector = collector;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2013 jonathan.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.browseengine.bobo.sort;

import com.browseengine.bobo.api.Browsable;
import java.util.Set;
import org.apache.lucene.search.SortField;

public interface SortCollectorFactory {

SortCollector create(DocComparatorSource compSource,
SortField[] sortFields,
Browsable boboBrowser,
int offset,
int count,
boolean doScoring,
boolean fetchStoredFields,
Set<String> termVectorsToFetch,
String[] groupBy,
int maxPerGroup,
boolean collectDocIdCache);
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public int compare(MyScoreDoc o1, MyScoreDoc o2) {
private FacetCountCollector[] _facetCountCollectorMulti = null;

private final boolean _doScoring;
private Scorer _scorer;
protected Scorer _scorer;
private final int _offset;
private final int _count;

Expand Down Expand Up @@ -185,6 +185,16 @@ public boolean acceptsDocsOutOfOrder() {
return _collector == null ? true : _collector.acceptsDocsOutOfOrder();
}

/**
* Allows sublclasses to mess with the score.
* @param score
* @return
* @throws IOException
*/
public float score(float score) throws IOException {
return (_doScoring ? score : 0.0f);
}

@Override
public void collect(int doc) throws IOException {
++_totalHits;
Expand All @@ -196,7 +206,7 @@ public void collect(int doc) throws IOException {
}

if (_count > 0) {
final float score = (_doScoring ? _scorer.score() : 0.0f);
final float score = score(_scorer.score());

if (_collectDocIdCache) {
if (_totalHits > _docIdCacheCapacity) {
Expand All @@ -218,7 +228,7 @@ public void collect(int doc) throws IOException {
return;
} else {
if (_count > 0) {
final float score = (_doScoring ? _scorer.score() : 0.0f);
final float score = score(_scorer.score());

if (_collectDocIdCache) {
if (_totalHits > _docIdCacheCapacity) {
Expand Down Expand Up @@ -271,7 +281,7 @@ public void collect(int doc) throws IOException {
}
} else {
if (_count > 0) {
final float score = (_doScoring ? _scorer.score() : 0.0f);
final float score = score(_scorer.score());

if (_queueFull) {
_tmpScoreDoc.doc = doc;
Expand Down Expand Up @@ -369,7 +379,7 @@ public BrowseHit[] topDocs() throws IOException {

PrimitiveLongArrayWrapper primitiveLongArrayWrapperTmp = new PrimitiveLongArrayWrapper(null);

Object rawGroupValue = null;
Object rawGroupValue;

if (_facetAccessibleLists != null) {
_groupAccessibles = new CombinedFacetAccessible[_facetAccessibleLists.length];
Expand Down