Search in sources :

Example 21 with StepInternalException

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.");
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) ParseException(org.apache.lucene.queryParser.ParseException)

Example 22 with StepInternalException

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);
}
Also used : VerseRange(org.crosswire.jsword.passage.VerseRange) LookupOption(com.tyndalehouse.step.core.models.LookupOption) ArrayList(java.util.ArrayList) Versification(org.crosswire.jsword.versification.Versification) Passage(org.crosswire.jsword.passage.Passage) RangedPassage(org.crosswire.jsword.passage.RangedPassage) NoSuchKeyException(org.crosswire.jsword.passage.NoSuchKeyException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Book(org.crosswire.jsword.book.Book) Key(org.crosswire.jsword.passage.Key) VerseKey(org.crosswire.jsword.passage.VerseKey) Verse(org.crosswire.jsword.passage.Verse) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper)

Example 23 with StepInternalException

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);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) SingleTermEnum(org.apache.lucene.search.SingleTermEnum) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) TermEnum(org.apache.lucene.index.TermEnum) SingleTermEnum(org.apache.lucene.search.SingleTermEnum) PrefixTermEnum(org.apache.lucene.search.PrefixTermEnum) PrefixTermEnum(org.apache.lucene.search.PrefixTermEnum) HashSet(java.util.HashSet) TermsAndMaxCount(com.tyndalehouse.step.core.data.common.TermsAndMaxCount)

Example 24 with StepInternalException

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);
    }
}
Also used : MultiFieldQueryParser(org.apache.lucene.queryParser.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryParser.QueryParser) AnalyzedPrefixSearchQueryParser(com.tyndalehouse.step.core.data.AnalyzedPrefixSearchQueryParser) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) ParseException(org.apache.lucene.queryParser.ParseException)

Example 25 with StepInternalException

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);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) IOException(java.io.IOException)

Aggregations

StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)62 IOException (java.io.IOException)25 Book (org.crosswire.jsword.book.Book)9 Key (org.crosswire.jsword.passage.Key)7 EntityDoc (com.tyndalehouse.step.core.data.EntityDoc)5 OsisWrapper (com.tyndalehouse.step.core.models.OsisWrapper)4 InputStream (java.io.InputStream)4 ArrayList (java.util.ArrayList)4 ParseException (org.apache.lucene.queryParser.ParseException)4 BookException (org.crosswire.jsword.book.BookException)4 Versification (org.crosswire.jsword.versification.Versification)4 LocalisedException (com.tyndalehouse.step.core.exceptions.LocalisedException)3 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)3 KeyWrapper (com.tyndalehouse.step.core.models.KeyWrapper)3 FileInputStream (java.io.FileInputStream)3 TransformingSAXEventProvider (org.crosswire.common.xml.TransformingSAXEventProvider)3 XMLUtil.writeToString (org.crosswire.common.xml.XMLUtil.writeToString)3 NoSuchKeyException (org.crosswire.jsword.passage.NoSuchKeyException)3 BibleBook (org.crosswire.jsword.versification.BibleBook)3 AllResultsCollector (com.tyndalehouse.step.core.data.AllResultsCollector)2