use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class SubjectSearchServiceImpl method getNaveDocs.
/**
* All entity docs for normal nave search
*
* @param sq the search query
* @return the entity docs matching the query
*/
private EntityDoc[] getNaveDocs(SearchQuery sq) {
final String query = sq.getCurrentSearch().getQuery();
final String[] split = StringUtils.split(query, "[, -=:]+");
final StringBuilder sb = new StringBuilder(query.length() + 16);
sb.append("+(");
for (final String s : split) {
// set mandatory
sb.append("+rootStem:");
sb.append(QueryParser.escape(s.trim()));
sb.append(" ");
}
sb.append(") ");
// construct query
sb.append(this.getInputReferenceForNaveSearch(sq.getCurrentSearch().getVersions(), sq.getCurrentSearch().getMainRange()).getValue());
try {
return this.naves.search(this.naves.getQueryParser(false, true, "rootStem").parse(sb.toString()), Integer.MAX_VALUE, NAVE_SORT, null);
} catch (ParseException ex) {
throw new StepInternalException("Unable to parse generated query.");
}
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class SubjectEntryServiceImpl method collectVersesFromReferences.
/**
* Collects individual ranges
*
* @param verses the verses
* @param inputVersions the versions
* @param references the list of resultsInKJV that form the results
* @param limitingScopeReference the limiting scope for the reference
* @param context the context to expand with the reference
*/
private boolean collectVersesFromReferences(final List<OsisWrapper> verses, final String[] inputVersions, final String references, final String limitingScopeReference, final int context) {
final String originalMaster = inputVersions[0];
Passage combinedScopeInKJVv11n = this.getCombinedBookScope(inputVersions);
// now let's retain the verses that are of interest in the selected books
Key resultsInKJV = null;
try {
resultsInKJV = this.versificationService.getBookFromVersion(JSwordPassageService.BEST_VERSIFICATION).getKey(references);
} catch (NoSuchKeyException e) {
throw new StepInternalException("Unable to parse resultsInKJV from Nave", e);
}
resultsInKJV.retainAll(combinedScopeInKJVv11n);
trimResultsToInputSearchRange(inputVersions[0], limitingScopeReference, resultsInKJV);
// then calculate what the best version order is
GetBestVersionOrderAndKey getBestVersionOrderAndKey = new GetBestVersionOrderAndKey(inputVersions, resultsInKJV).invoke();
Book book = getBestVersionOrderAndKey.getBook();
String[] versions = getBestVersionOrderAndKey.getVersions();
final Passage resultsInProperV11n = getBestVersionOrderAndKey.getVerseRanges();
final Iterator<VerseRange> rangeIterator = resultsInProperV11n.rangeIterator(RestrictionType.NONE);
final List<LookupOption> options = new ArrayList<LookupOption>();
options.add(LookupOption.HIDE_XGEN);
options.add(LookupOption.GREEK_ACCENTS);
options.add(LookupOption.HEBREW_VOWELS);
if (context > 0) {
// add verse numbers
// options.add(LookupOption.TINY_VERSE_NUMBERS);
options.add(LookupOption.VERSE_NUMBERS);
}
final Versification av11n = this.versificationService.getVersificationForVersion(book);
Verse lastVerse = null;
while (rangeIterator.hasNext()) {
final Key range = rangeIterator.next();
// get the distance between the first verse in the range and the last verse
if (lastVerse != null && isCloseVerse(av11n, lastVerse, range)) {
final OsisWrapper osisWrapper = verses.get(verses.size() - 1);
final StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(osisWrapper.getReference());
stringBuilder.append("; ");
stringBuilder.append(range.getName());
osisWrapper.setFragment(true);
try {
osisWrapper.setReference(book.getKey(stringBuilder.toString()).getName());
} catch (final NoSuchKeyException e) {
// fail to get a key, let's log and continue
LOGGER.warn("Unable to get key for reference: [{}]", osisWrapper.getReference());
LOGGER.trace("Root cause is", e);
}
} else {
final Key firstVerse = this.jsword.getFirstVersesFromRange(range, context);
final OsisWrapper passage = this.jsword.peakOsisText(versions, firstVerse, options, InterlinearMode.INTERLEAVED_COMPARE.name());
passage.setReference(range.getName());
if (range.getCardinality() > 1) {
passage.setFragment(true);
}
verses.add(passage);
}
// record last verse
if (range instanceof VerseRange) {
final VerseRange verseRange = (VerseRange) range;
lastVerse = verseRange.getEnd();
} else if (range instanceof Verse) {
lastVerse = (Verse) range;
}
}
return !getBestVersionOrderAndKey.versions[0].equals(originalMaster);
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class LuceneUtils method getAllTermsPrefixedWith.
/**
* Returns all terms starting with a particular prefix
*
* @param exact indicates we want 'exact' matches only
* @param fieldName the name of the fields
* @param searchTerm the search term
* @return the list of terms matching searchTerm as a prefix
*/
public static TermsAndMaxCount getAllTermsPrefixedWith(final boolean exact, final boolean trackMax, IndexSearcher searcher, final String fieldName, final String searchTerm, final int max) {
final String lastTerm = getLastTerm(searchTerm);
if (StringUtils.isBlank(lastTerm)) {
return getBlankTermsAndMaxCount();
}
TermEnum termEnum = null;
try {
final Term term = new Term(fieldName, QueryParser.escape(lastTerm.toLowerCase().trim()));
termEnum = exact ? new SingleTermEnum(searcher.getIndexReader(), term) : new PrefixTermEnum(searcher.getIndexReader(), term);
int count = 0;
if (termEnum.term() == null) {
return getBlankTermsAndMaxCount();
}
final Set<String> terms = new HashSet<String>();
do {
if (count < max) {
// when inexact, don't include exact terms
final String termValue = termEnum.term().text();
if (!exact && termValue.equalsIgnoreCase(searchTerm)) {
// we didn't really find a term after all, since it's the exact same term
count--;
} else {
terms.add(termValue);
}
}
count++;
// we continue round the loop until we've got enough, or in case we're wanting to keep track of the total number
} while (termEnum.next() && ((count < max) || trackMax || count < MAX_TRACK));
// finalise and return
TermsAndMaxCount termsAndMaxCount = new TermsAndMaxCount();
termsAndMaxCount.setTotalCount(count);
termsAndMaxCount.setTerms(terms);
return termsAndMaxCount;
} catch (IOException ex) {
throw new StepInternalException(ex.getMessage(), ex);
} finally {
IOUtils.closeQuietly(termEnum);
}
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class EntityIndexReaderImpl method searchSingleColumn.
@Override
public EntityDoc[] searchSingleColumn(final String fieldName, final String querySyntax, final Operator op, final boolean allowLeadingWildcard, final Sort sort, final Filter filter) {
final QueryParser parser = new QueryParser(LUCENE_30, fieldName, this.getAnalyzer());
parser.setDefaultOperator(op);
parser.setAllowLeadingWildcard(allowLeadingWildcard);
try {
final Query query = parser.parse(querySyntax);
return search(query, Integer.MAX_VALUE, sort, filter);
} catch (final ParseException e) {
throw new StepInternalException("Unable to parse query", e);
}
}
use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.
the class EntityIndexReaderImpl method extractDocIds.
/**
* Extracts the query results into an entity doc
*
* @param collector the collector with the results
* @return all the entity documents
*/
private EntityDoc[] extractDocIds(final AllResultsCollector collector) {
try {
final List<Integer> docIds = collector.getDocIds();
final EntityDoc[] docs = new EntityDoc[docIds.size()];
for (int ii = 0; ii < docIds.size(); ii++) {
docs[ii] = new EntityDoc(this.searcher.doc(docIds.get(ii)));
}
return docs;
} catch (final IOException e) {
throw new StepInternalException("Unable to extract results from query", e);
}
}
Aggregations