Search in sources :

Example 1 with TermsAndMaxCount

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

the class SubjectSuggestionServiceImpl method collectNonExactMatches.

@Override
public SubjectSuggestion[] collectNonExactMatches(final TermsAndMaxCount<SubjectSuggestion> collector, final SuggestionContext context, final SubjectSuggestion[] alreadyRetrieved, final int leftToCollect) {
    final Map<String, SubjectSuggestion> suggestions = new TreeMap<String, SubjectSuggestion>();
    final PorterStemmer stemmer = new PorterStemmer();
    addExistingMappings(suggestions, stemmer, alreadyRetrieved);
    final String input = context.getInput();
    final TermsAndMaxCount termsFromHeadings = LuceneUtils.getAllTermsPrefixedWith(false, false, this.jSwordSearchService.getIndexSearcher(JSwordPassageService.REFERENCE_BOOK), LuceneIndex.FIELD_HEADING, input, leftToCollect);
    final TermsAndMaxCount termsFromSimpleNave = this.naves.findSetOfTermsWithCounts(false, true, input, leftToCollect, "root");
    final TermsAndMaxCount termsFromFullNave = this.naves.findSetOfTermsWithCounts(false, true, input, leftToCollect, "fullTerm");
    addSubjectTerms(suggestions, stemmer, termsFromHeadings.getTerms(), SearchType.SUBJECT_SIMPLE);
    addSubjectTerms(suggestions, stemmer, termsFromSimpleNave.getTerms(), SearchType.SUBJECT_EXTENDED);
    addSubjectTerms(suggestions, stemmer, termsFromFullNave.getTerms(), SearchType.SUBJECT_FULL);
    // termsFromHeadings.setTotalCount(termsFromHeadings.getTotalCount() - addSubjectTerms(suggestions, stemmer, termsFromHeadings.getTerms(), SearchType.SUBJECT_SIMPLE));
    // termsFromSimpleNave.setTotalCount(termsFromSimpleNave.getTotalCount() - addSubjectTerms(suggestions, stemmer, termsFromSimpleNave.getTerms(), SearchType.SUBJECT_EXTENDED));
    // termsFromFullNave.setTotalCount(termsFromFullNave.getTotalCount() - addSubjectTerms(suggestions, stemmer, termsFromFullNave.getTerms(), SearchType.SUBJECT_FULL));
    TermsAndMaxCount countsAndResults = new TermsAndMaxCount();
    countsAndResults.setTerms(new HashSet<SubjectSuggestion>(suggestions.values()));
    collector.setTotalCount(suggestions.size());
    return suggestions.values().toArray(new SubjectSuggestion[countsAndResults.getTerms().size()]);
}
Also used : PorterStemmer(org.tartarus.snowball.ext.PorterStemmer) SubjectSuggestion(com.tyndalehouse.step.core.models.search.SubjectSuggestion) TreeMap(java.util.TreeMap) TermsAndMaxCount(com.tyndalehouse.step.core.data.common.TermsAndMaxCount)

Example 2 with TermsAndMaxCount

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

the class MeaningSuggestionServiceImpl method collectNonExactMatches.

@Override
public String[] collectNonExactMatches(final TermsAndMaxCount<String> collector, final SuggestionContext context, final String[] alreadyRetrieved, final int leftToCollect) {
    TermsAndMaxCount countsAndResults = this.definitions.findSetOfTermsWithCounts(false, true, context.getInput(), collector.getTotalCount(), ANCIENT_MEANING_FIELDS);
    final Set<String> resultTerms = countsAndResults.getTerms();
    collector.setTotalCount(countsAndResults.getTotalCount());
    collector.setTerms(countsAndResults.getTerms());
    return resultTerms.toArray(new String[resultTerms.size()]);
}
Also used : TermsAndMaxCount(com.tyndalehouse.step.core.data.common.TermsAndMaxCount)

Example 3 with TermsAndMaxCount

use of com.tyndalehouse.step.core.data.common.TermsAndMaxCount 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 4 with TermsAndMaxCount

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

the class EntityIndexReaderImpl method findSetOfTermsWithCounts.

@Override
public TermsAndMaxCount findSetOfTermsWithCounts(final boolean exact, final boolean trackMax, String searchTerm, int maxReturned, final String... fieldNames) {
    TermsAndMaxCount hits = new TermsAndMaxCount();
    if (fieldNames.length == 0) {
        hits.setTerms(new HashSet<String>(0));
        return hits;
    }
    if (fieldNames.length == 1) {
        return LuceneUtils.getAllTermsPrefixedWith(exact, trackMax, this.searcher, fieldNames[0], searchTerm, maxReturned);
    }
    hits.setTerms(new HashSet<String>(32));
    for (int ii = 0; ii < fieldNames.length; ii++) {
        final TermsAndMaxCount termsByField = LuceneUtils.getAllTermsPrefixedWith(exact, trackMax, this.searcher, fieldNames[ii], searchTerm, maxReturned);
        hits.getTerms().addAll(termsByField.getTerms());
        hits.setTotalCount(hits.getTotalCount() + termsByField.getTotalCount());
    }
    // total count, is count - the existing ters
    hits.setTotalCount(hits.getTotalCount() - hits.getTerms().size());
    return hits;
}
Also used : TermsAndMaxCount(com.tyndalehouse.step.core.data.common.TermsAndMaxCount)

Example 5 with TermsAndMaxCount

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

the class LuceneUtils method getBlankTermsAndMaxCount.

private static TermsAndMaxCount getBlankTermsAndMaxCount() {
    TermsAndMaxCount termsAndMaxCount = new TermsAndMaxCount();
    termsAndMaxCount.setTerms(new HashSet<String>());
    return termsAndMaxCount;
}
Also used : TermsAndMaxCount(com.tyndalehouse.step.core.data.common.TermsAndMaxCount)

Aggregations

TermsAndMaxCount (com.tyndalehouse.step.core.data.common.TermsAndMaxCount)5 StepInternalException (com.tyndalehouse.step.core.exceptions.StepInternalException)1 SubjectSuggestion (com.tyndalehouse.step.core.models.search.SubjectSuggestion)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 Term (org.apache.lucene.index.Term)1 TermEnum (org.apache.lucene.index.TermEnum)1 PrefixTermEnum (org.apache.lucene.search.PrefixTermEnum)1 SingleTermEnum (org.apache.lucene.search.SingleTermEnum)1 PorterStemmer (org.tartarus.snowball.ext.PorterStemmer)1