Search in sources :

Example 26 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class EntityIndexReaderImpl method search.

@Override
public EntityDoc[] search(final Query query) {
    final AllResultsCollector collector = new AllResultsCollector();
    try {
        LOGGER.debug("Search query is [{}], with filter [{}]", query);
        this.searcher.search(query, collector);
        return extractDocIds(collector);
    } catch (final IOException e) {
        throw new StepInternalException("Unable to search", e);
    }
}
Also used : AllResultsCollector(com.tyndalehouse.step.core.data.AllResultsCollector) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) IOException(java.io.IOException)

Example 27 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class EntityIndexReaderImpl method extractDocIds.

/**
 * Extracts all the results
 *
 * @param results the results that have been collected
 * @return the results
 */
private EntityDoc[] extractDocIds(final TopDocs results) {
    try {
        final ScoreDoc[] scoreDocs = results.scoreDocs;
        final EntityDoc[] docs = new EntityDoc[scoreDocs.length];
        for (int ii = 0; ii < scoreDocs.length; ii++) {
            docs[ii] = new EntityDoc(this.searcher.doc(scoreDocs[ii].doc));
        }
        return docs;
    } catch (final IOException e) {
        throw new StepInternalException("Unable to extract results", e);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) IOException(java.io.IOException)

Example 28 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class EntityIndexReaderImpl method getAnalyzedTokens.

@Override
public List<String> getAnalyzedTokens(final String fieldName, final String input, boolean escapeToken) {
    final TokenStream tokens = this.getAnalyzer().tokenStream(fieldName, new StringReader(input));
    // construct query to search for both stepGloss and translations - the last word gets a trailing wildcard
    // query will be in the form of +(gloss:a trans:a) +(gloss:b* trans:b*) +strong:H*
    List<String> tokenItems = new ArrayList<String>(2);
    try {
        tokens.reset();
        TermAttribute termAttribute = tokens.getAttribute(TermAttribute.class);
        while (tokens.incrementToken()) {
            String term = termAttribute.term();
            if (escapeToken) {
                term = QueryParser.escape(term);
            }
            tokenItems.add(term);
        }
    } catch (IOException e) {
        throw new StepInternalException("Unable to parse query", e);
    } finally {
        try {
            tokens.end();
            tokens.close();
        } catch (IOException e) {
            LOGGER.trace("Unable to properly close stream.");
        }
    }
    return tokenItems;
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) TermAttribute(org.apache.lucene.analysis.tokenattributes.TermAttribute) IOException(java.io.IOException)

Example 29 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class EntityIndexReaderImpl method search.

@Override
public EntityDoc[] search(final Query query, final int max, final Sort sortField, final Filter filter) {
    LOGGER.debug("Search query is [{}]", query);
    try {
        final TopDocs search;
        if (sortField != null) {
            search = this.searcher.search(query, filter, max, sortField);
        } else {
            search = this.searcher.search(query, filter, max);
        }
        final EntityDoc[] results = new EntityDoc[search.scoreDocs.length];
        for (int ii = 0; ii < search.scoreDocs.length; ii++) {
            results[ii] = new EntityDoc(this.searcher.doc(search.scoreDocs[ii].doc));
        }
        return results;
    } catch (final IOException e) {
        throw new StepInternalException("Failed to search", e);
    }
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) IOException(java.io.IOException)

Example 30 with StepInternalException

use of com.tyndalehouse.step.core.exceptions.StepInternalException in project step by STEPBible.

the class EntityIndexReaderImpl method search.

// CHECKSTYLE:OFF
@Override
public EntityDoc[] search(final String[] fieldNames, final String value, final Filter filter, final Sort sort, final boolean analyzePrefix, final String queryRemainder, final Integer maxResults, final boolean useOrOperatorBetweenValues) {
    // CHECKSTYLE:ON
    final AllResultsCollector collector = new AllResultsCollector();
    Query parsed = null;
    QueryParser parser;
    if (analyzePrefix) {
        parser = new AnalyzedPrefixSearchQueryParser(LUCENE_30, fieldNames, this.config.getAnalyzerInstance());
    } else {
        parser = new MultiFieldQueryParser(LUCENE_30, fieldNames, this.config.getAnalyzerInstance());
    }
    parser.setDefaultOperator(useOrOperatorBetweenValues ? Operator.OR : Operator.AND);
    try {
        if (queryRemainder != null) {
            final StringBuilder sb = new StringBuilder(value.length() + queryRemainder.length() + 1);
            sb.append(value);
            sb.append(' ');
            sb.append(queryRemainder);
            parsed = parser.parse(sb.toString());
        } else {
            parsed = parser.parse(value);
        }
        LOGGER.debug("Search query is [{}]", parsed);
        if (sort != null) {
            final TopFieldDocs search = this.searcher.search(parsed, filter, maxResults == null ? Integer.MAX_VALUE : maxResults, sort);
            return extractDocIds(search);
        } else {
            this.searcher.search(parsed, filter, collector);
            return extractDocIds(collector);
        }
    } catch (final ParseException e) {
        throw new StepInternalException("Unable to parse query", e);
    } catch (final IOException e) {
        throw new StepInternalException("Unable to search given query: " + parsed != null ? parsed.toString() : "<unknown>", e);
    }
}
Also used : AnalyzedPrefixSearchQueryParser(com.tyndalehouse.step.core.data.AnalyzedPrefixSearchQueryParser) MultiFieldQueryParser(org.apache.lucene.queryParser.MultiFieldQueryParser) QueryParser(org.apache.lucene.queryParser.QueryParser) AnalyzedPrefixSearchQueryParser(com.tyndalehouse.step.core.data.AnalyzedPrefixSearchQueryParser) AllResultsCollector(com.tyndalehouse.step.core.data.AllResultsCollector) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) MultiFieldQueryParser(org.apache.lucene.queryParser.MultiFieldQueryParser) ParseException(org.apache.lucene.queryParser.ParseException) 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