Skip to content

Commit

Permalink
fixed getTotalHits
Browse files Browse the repository at this point in the history
Issue #554
  • Loading branch information
rsoika committed Sep 1, 2019
1 parent 95e2fec commit 2181703
Showing 1 changed file with 27 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.queryparser.classic.QueryParser.Operator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
Expand Down Expand Up @@ -99,7 +98,6 @@ public class LuceneSearchService implements SearchService {
// number of hits
public static final int DEFAULT_PAGE_SIZE = 100; // default docs in one page


@Inject
@ConfigProperty(name = "index.defaultOperator", defaultValue = "AND")
private String luceneDefaultOperator;
Expand All @@ -110,16 +108,15 @@ public class LuceneSearchService implements SearchService {

@Inject
private LuceneIndexService luceneIndexService;

@Inject
private DocumentService documentService;
@Inject

@Inject
private SchemaService schemaService;

private static Logger logger = Logger.getLogger(LuceneSearchService.class.getName());


/**
* Returns a collection of documents matching the provided search term. The term
* will be extended with the current users roles to test the read access level
Expand Down Expand Up @@ -158,7 +155,7 @@ public List<ItemCollection> search(String searchTerm, int pageSize, int pageInde
long ltime = System.currentTimeMillis();

// flush eventlog (see issue #411)
//flush();
// flush();

// see issue #382
/*
Expand Down Expand Up @@ -221,7 +218,8 @@ public List<ItemCollection> search(String searchTerm, int pageSize, int pageInde
// sorted by sortoder
logger.finest("......lucene result sorted by sortOrder= '" + sortOrder + "' ");
// MAX_SEARCH_RESULT is limiting the total number of hits
collector = TopFieldCollector.create(buildLuceneSort(sortOrder), maxSearchResult, false, false, false, false);
collector = TopFieldCollector.create(buildLuceneSort(sortOrder), maxSearchResult, false, false, false,
false);

} else {
// sorted by score
Expand Down Expand Up @@ -293,8 +291,6 @@ public List<ItemCollection> search(String searchTerm, int pageSize, int pageInde
return workitems;
}



/**
* Returns the total hits for a given search term from the lucene index. The
* method did not load any data. The provided search term will we extended with
Expand All @@ -318,46 +314,45 @@ public int getTotalHits(final String _searchTerm, final int _maxResult, final De
throws QueryException {
int result;
int maxResult = _maxResult;

if (maxResult <= 0) {
maxResult = DEFAULT_MAX_SEARCH_RESULT;
}
String sSearchTerm =schemaService.getExtendedSearchTerm(_searchTerm);

String sSearchTerm = schemaService.getExtendedSearchTerm(_searchTerm);
// test if searchtem is provided
if (sSearchTerm == null || "".equals(sSearchTerm)) {
return 0;
}

try {
IndexSearcher searcher = createIndexSearcher();
QueryParser parser = createQueryParser();

parser.setAllowLeadingWildcard(true);

// set default operator?
if (defaultOperator.equals(Operator.AND)) {
parser.setDefaultOperator(org.apache.lucene.queryparser.classic.QueryParser.Operator.AND);
} else {
if (defaultOperator == DefaultOperator.OR) {
parser.setDefaultOperator(org.apache.lucene.queryparser.classic.QueryParser.Operator.OR);

} else {
parser.setDefaultOperator(org.apache.lucene.queryparser.classic.QueryParser.Operator.AND);
}

TopDocsCollector<?> collector = null;

Query query = parser.parse(sSearchTerm);
// MAX_SEARCH_RESULT is limiting the total number of hits
collector = TopScoreDocCollector.create(maxResult);

// - ignore time limiting for now
// Counter clock = Counter.newCounter(true);
// TimeLimitingCollector timeLimitingCollector = new
// TimeLimitingCollector(collector, clock, 10);

// start search....
searcher.search(query, collector);
result = collector.getTotalHits();

logger.finest("......lucene count result = " + result);
} catch (IOException e) {
// in case of an IOException we just print an error message and
Expand All @@ -368,12 +363,10 @@ public int getTotalHits(final String _searchTerm, final int _maxResult, final De
logger.severe("Lucene search error: " + e.getMessage());
throw new QueryException(QueryException.QUERY_NOT_UNDERSTANDABLE, e.getMessage(), e);
}

return result;
}



/**
* Creates a Lucene FSDirectory Instance. The method uses the proeprty
* LockFactory to set a custom LockFactory.
Expand Down Expand Up @@ -510,15 +503,14 @@ ItemCollection convertLuceneDocument(Document luceneDoc, SimpleDateFormat lucene

private Sort buildLuceneSort(org.imixs.workflow.engine.index.SortOrder sortOrder) {
Sort sort = null;
// we do not support multi values here - see
// LuceneUpdateService.addItemValues
// it would be possible if we use a SortedSetSortField class here
sort = new Sort(new SortField[] { new SortField(sortOrder.getField(), SortField.Type.STRING, sortOrder.isReverse()) });
// we do not support multi values here - see
// LuceneUpdateService.addItemValues
// it would be possible if we use a SortedSetSortField class here
sort = new Sort(
new SortField[] { new SortField(sortOrder.getField(), SortField.Type.STRING, sortOrder.isReverse()) });
return sort;
}



/**
* Helper method to check for numbers.
*
Expand All @@ -544,5 +536,4 @@ private static boolean isNumeric(String str) {

}


}

0 comments on commit 2181703

Please sign in to comment.