public Object[] getElements(Object searchQueryObj)
{
if( searchQueryObj==null || !(searchQueryObj instanceof SearchQuery) )
return Collections.EMPTY_LIST.toArray(); // not searching
SearchQuery searchQuery = (SearchQuery) searchQueryObj;
String searchString = searchQuery.getSearchString();
if( searchString==null || searchString.toString().length() < Searcher.MIN_QUERY_LENGTH )
return Collections.EMPTY_LIST.toArray();
try
{
if( ! indexer.isIndexed() )
return NO_INDEX_MESSAGE;
} catch(Exception e)
{
InstaSearchPlugin.log(e);
return new Exception[]{e};
}
if( searchQuery.equals(currentSearchQuery) && cachedResults != null ) // same query
return cachedResults;
SearchResult result = null;
cachedResults = null;
resultCount = 0;
Object[] resultArray = null;
if(searchString != null)
{
try {
result = searcher.search(searchQuery); // do the search
currentSearchQuery = searchQuery;
if( result == null ) {
if( !searchQuery.isFuzzy() ) {
SearchQuery newQuery = new SearchQuery(searchQuery);
newQuery.setExact(false);
newQuery.setFuzzy(true);
resultArray = new Object[]{NO_RESULTS_MESSAGE, newQuery}; // add fuzzy query
} else {
resultArray = new Object[]{NO_RESULTS_MESSAGE};
}
cachedResults = resultArray;
return resultArray;
}
searchTerms = result.getSearchTerms();
} catch (Exception e) {
InstaSearchPlugin.log(e);
return new Exception[]{e};
}
}
List<SearchResultDoc> resultDocs = result.getResultDocs();
this.resultCount = resultDocs.size();
boolean addMoreResults = false, addFindSimilar = false;
if( searchQuery.isLimited() && result.isFull() ) { // if only showing limited number of matches
addMoreResults = true;
}
else
{
if( searchQuery.isExact() && !searchQuery.isFuzzy() ) // if query is exact, can try search for individual tokens
addFindSimilar = true;
}
if( addMoreResults || addFindSimilar )
resultArray = new Object[resultCount + 1]; // +1 because we append "More..." element (a SearchQuery object)
else
resultArray = new Object[resultCount];
resultDocs.toArray(resultArray);
if( addMoreResults ) { // if more results, create More result entry (return all elements)
SearchQuery moreResultsQuery = new SearchQuery(currentSearchQuery);
moreResultsQuery.setMaxResults(SearchQuery.UNLIMITED_RESULTS); // all results
resultArray[resultCount] = moreResultsQuery;
}
else if( addFindSimilar )
{
SearchQuery findSimilarQuery = new SearchQuery(currentSearchQuery);
findSimilarQuery.setExact(false);
resultArray[resultCount] = findSimilarQuery;
}
cachedResults = resultArray;