Search in sources :

Example 21 with EntityDoc

use of com.tyndalehouse.step.core.data.EntityDoc in project step by STEPBible.

the class SubjectSearchServiceImpl method naveDocsToReference.

/**
 * Converts a set of nave documents to their reference equivalent
 *
 * @param extendedDocs
 * @return
 */
private Key naveDocsToReference(SearchQuery sq, EntityDoc[] extendedDocs) {
    String mainVersion = sq.getCurrentSearch().getVersions()[0];
    Book naveVersion = this.jSwordVersificationService.getBookFromVersion(JSwordPassageService.BEST_VERSIFICATION);
    Book bookFromVersion = this.jSwordVersificationService.getBookFromVersion(mainVersion);
    Key passageKey = null;
    for (EntityDoc d : extendedDocs) {
        String storedReferences = d.get(NAVE_STORED_REFERENCES);
        final Key key;
        try {
            // NEEDS TO BE KJV
            // NEEDS TO BE KJV
            // NEEDS TO BE KJV
            // NEEDS TO BE KJV
            // NEEDS TO BE KJV
            // NEEDS TO BE KJV
            // NEEDS TO BE KJV
            // NEEDS TO BE KJV
            key = naveVersion.getKey(storedReferences);
        } catch (Exception ex) {
            throw new StepInternalException("Stored references are unparseable in nave module: " + storedReferences);
        }
        if (passageKey == null) {
            passageKey = key;
        } else {
            passageKey.addAll(key);
        }
    }
    return passageKey;
}
Also used : StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException) Book(org.crosswire.jsword.book.Book) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) VerseKey(org.crosswire.jsword.passage.VerseKey) Key(org.crosswire.jsword.passage.Key) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) ParseException(org.apache.lucene.queryParser.ParseException) StepInternalException(com.tyndalehouse.step.core.exceptions.StepInternalException)

Example 22 with EntityDoc

use of com.tyndalehouse.step.core.data.EntityDoc in project step by STEPBible.

the class SubjectEntryServiceImpl method getVersesForResults.

/**
 * obtains the verses for all results
 *
 * @param results  the results
 * @param versions the version in which to look it up
 * @param context  the context to expand with the reference
 * @return the verses
 */
private SubjectEntries getVersesForResults(final EntityDoc[] results, final String[] versions, final String limitingScopeReference, final int context) {
    final List<OsisWrapper> verses = new ArrayList<OsisWrapper>(32);
    boolean masterVersionSwapped = false;
    for (final EntityDoc doc : results) {
        final String references = doc.get("references");
        masterVersionSwapped |= collectVersesFromReferences(verses, versions, references, limitingScopeReference, context);
    }
    return new SubjectEntries(verses, masterVersionSwapped);
}
Also used : SubjectEntries(com.tyndalehouse.step.core.models.search.SubjectEntries) ArrayList(java.util.ArrayList) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) OsisWrapper(com.tyndalehouse.step.core.models.OsisWrapper)

Example 23 with EntityDoc

use of com.tyndalehouse.step.core.data.EntityDoc in project step by STEPBible.

the class SearchServiceImpl method retrieveStrongDefinitions.

/**
 * Retrieves the correct entity documents from a built up query and passed in parser
 *
 * @param sq              the search query
 * @param filteredStrongs the list of filtered strongs so far
 * @param p               the parser
 * @param query           the query
 * @param fullQuery       the full query so far
 * @return the list of matched entity documents
 */
private EntityDoc[] retrieveStrongDefinitions(final SearchQuery sq, final Set<String> filteredStrongs, final QueryParser p, final String query, final StringBuilder fullQuery) {
    if (StringUtils.isNotBlank(query)) {
        Query q;
        try {
            q = p.parse(query);
        } catch (final ParseException e) {
            throw new TranslatedException(e, "search_invalid");
        }
        final EntityDoc[] results = this.definitions.search(q);
        for (final EntityDoc doc : results) {
            // remove from matched strong if not in filter
            final String strongNumber = doc.get(STRONG_NUMBER_FIELD);
            if (isInFilter(strongNumber, sq)) {
                filteredStrongs.add(strongNumber);
                fullQuery.append(STRONG_QUERY);
                fullQuery.append(strongNumber);
                fullQuery.append(' ');
            }
        }
        return results;
    }
    return new EntityDoc[0];
}
Also used : Query(org.apache.lucene.search.Query) SearchQuery(com.tyndalehouse.step.core.service.impl.SearchQuery) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) ParseException(org.apache.lucene.queryParser.ParseException)

Example 24 with EntityDoc

use of com.tyndalehouse.step.core.data.EntityDoc in project step by STEPBible.

the class SearchServiceImpl method setUniqueConsideredDefinitions.

/**
 * Sets up all definitions that are related, regardless of whether they have been filtered out. Makes a unique set
 * of these
 *
 * @param sq             the search query that is being
 * @param results        the results from the direct strong search
 * @param relatedResults the related results
 */
private void setUniqueConsideredDefinitions(final SearchQuery sq, final EntityDoc[] results, final EntityDoc[] relatedResults) {
    // make entity docs unique:
    final Set<EntityDoc> joinedDocs = new HashSet<EntityDoc>(relatedResults.length + results.length);
    final Set<String> strongNumbers = new HashSet<String>(relatedResults.length + results.length);
    for (final EntityDoc queryDoc : results) {
        final String strong = queryDoc.get(STRONG_NUMBER_FIELD);
        if (!strongNumbers.contains(strong)) {
            joinedDocs.add(queryDoc);
            strongNumbers.add(strong);
        }
    }
    for (final EntityDoc relatedDoc : relatedResults) {
        final String strong = relatedDoc.get(STRONG_NUMBER_FIELD);
        if (!strongNumbers.contains(strong)) {
            joinedDocs.add(relatedDoc);
            strongNumbers.add(strong);
        }
    }
    sq.setDefinitions(new ArrayList<EntityDoc>(joinedDocs));
}
Also used : EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) HashSet(java.util.HashSet)

Example 25 with EntityDoc

use of com.tyndalehouse.step.core.data.EntityDoc in project step by STEPBible.

the class SearchServiceImpl method adaptQueryForMeaningSearch.

/**
 * Looks up all the glosses for a particular word, and then adapts to strong search and continues as before
 *
 * @param sq search criteria
 * @return a list of matching strongs
 */
private Set<String> adaptQueryForMeaningSearch(final SearchQuery sq) {
    final String query = sq.getCurrentSearch().getQuery();
    final QueryParser queryParser = new QueryParser(Version.LUCENE_30, "translationsStem", this.definitions.getAnalyzer());
    queryParser.setDefaultOperator(Operator.OR);
    try {
        // we need to also add the step gloss, but since we need the analyser for stems,
        // we want to use the query parser that does the tokenization for us
        // could probably do better if required
        String[] terms = StringUtils.split(query);
        StringBuilder finalQuery = new StringBuilder();
        for (String term : terms) {
            final String escapedTerm = QueryParser.escape(term);
            finalQuery.append(escapedTerm);
            finalQuery.append(" stepGlossStem:");
            finalQuery.append(escapedTerm);
        }
        final Query parsed = queryParser.parse("-stopWord:true " + finalQuery.toString());
        final EntityDoc[] matchingMeanings = this.definitions.search(parsed);
        final Set<String> strongs = new HashSet<String>(matchingMeanings.length);
        for (final EntityDoc d : matchingMeanings) {
            final String strongNumber = d.get(STRONG_NUMBER_FIELD);
            if (isInFilter(strongNumber, sq)) {
                strongs.add(strongNumber);
            }
        }
        final String textQuery = getQuerySyntaxForStrongs(strongs, sq);
        sq.getCurrentSearch().setQuery(textQuery, true);
        sq.setDefinitions(matchingMeanings);
        // return the strongs that the search will match
        return strongs;
    } catch (final ParseException e) {
        throw new TranslatedException(e, "search_invalid");
    }
}
Also used : QueryParser(org.apache.lucene.queryParser.QueryParser) MultiFieldQueryParser(org.apache.lucene.queryParser.MultiFieldQueryParser) Query(org.apache.lucene.search.Query) SearchQuery(com.tyndalehouse.step.core.service.impl.SearchQuery) TranslatedException(com.tyndalehouse.step.core.exceptions.TranslatedException) EntityDoc(com.tyndalehouse.step.core.data.EntityDoc) ParseException(org.apache.lucene.queryParser.ParseException) HashSet(java.util.HashSet)

Aggregations

EntityDoc (com.tyndalehouse.step.core.data.EntityDoc)39 ArrayList (java.util.ArrayList)14 HashMap (java.util.HashMap)6 HashSet (java.util.HashSet)6 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)5 TranslatedException (com.tyndalehouse.step.core.exceptions.TranslatedException)5 LexiconSuggestion (com.tyndalehouse.step.core.models.LexiconSuggestion)5 ParseException (org.apache.lucene.queryParser.ParseException)5 SearchQuery (com.tyndalehouse.step.core.service.impl.SearchQuery)4 IOException (java.io.IOException)4 MultiFieldQueryParser (org.apache.lucene.queryParser.MultiFieldQueryParser)4 Query (org.apache.lucene.search.Query)4 BookAndBibleCount (com.tyndalehouse.step.core.models.search.BookAndBibleCount)3 SearchEntry (com.tyndalehouse.step.core.models.search.SearchEntry)3 SearchResult (com.tyndalehouse.step.core.models.search.SearchResult)3 QueryParser (org.apache.lucene.queryParser.QueryParser)3 Key (org.crosswire.jsword.passage.Key)3 ExpandableSubjectHeadingEntry (com.tyndalehouse.step.core.models.search.ExpandableSubjectHeadingEntry)2 LexicalSearchEntry (com.tyndalehouse.step.core.models.search.LexicalSearchEntry)2 SubjectHeadingSearchEntry (com.tyndalehouse.step.core.models.search.SubjectHeadingSearchEntry)2