Example usage for org.apache.solr.spelling QueryConverter TERM_IN_BOOLEAN_QUERY_FLAG

List of usage examples for org.apache.solr.spelling QueryConverter TERM_IN_BOOLEAN_QUERY_FLAG

Introduction

In this page you can find the example usage for org.apache.solr.spelling QueryConverter TERM_IN_BOOLEAN_QUERY_FLAG.

Prototype

int TERM_IN_BOOLEAN_QUERY_FLAG

To view the source code for org.apache.solr.spelling QueryConverter TERM_IN_BOOLEAN_QUERY_FLAG.

Click Source Link

Document

This term exists in a query that contains boolean operators (AND/OR/NOT)

Usage

From source file:org.alfresco.solr.component.spellcheck.AlfrescoSpellCheckCollator.java

License:Open Source License

@SuppressWarnings("deprecation")
private String getCollation(String origQuery, List<SpellCheckCorrection> corrections) {
    StringBuilder collation = new StringBuilder(origQuery);
    int offset = 0;
    String corr = "";
    for (int i = 0; i < corrections.size(); i++) {
        SpellCheckCorrection correction = corrections.get(i);
        Token tok = correction.getOriginal();
        // we are replacing the query in order, but injected terms might
        // cause illegal offsets due to previous replacements.
        if (tok.getPositionIncrement() == 0)
            continue;
        corr = correction.getCorrection();
        boolean addParenthesis = false;
        Character requiredOrProhibited = null;
        int indexOfSpace = corr.indexOf(' ');
        StringBuilder corrSb = new StringBuilder(corr);
        int bump = 1;

        // If the correction contains whitespace (because it involved
        // breaking a word in 2+ words),
        // then be sure all of the new words have the same
        // optional/required/prohibited status in the query.
        while (indexOfSpace > -1 && indexOfSpace < corr.length() - 1) {
            addParenthesis = true;//from ww w.  j ava 2  s.  com
            char previousChar = tok.startOffset() > 0 ? origQuery.charAt(tok.startOffset() - 1) : ' ';
            if (previousChar == '-' || previousChar == '+') {
                corrSb.insert(indexOfSpace + bump, previousChar);
                if (requiredOrProhibited == null) {
                    requiredOrProhibited = previousChar;
                }
                bump++;
            } else if ((tok.getFlags()
                    & QueryConverter.TERM_IN_BOOLEAN_QUERY_FLAG) == QueryConverter.TERM_IN_BOOLEAN_QUERY_FLAG) {
                corrSb.insert(indexOfSpace + bump, "AND ");
                bump += 4;
            }
            indexOfSpace = correction.getCorrection().indexOf(' ', indexOfSpace + bump);
        }

        int oneForReqOrProhib = 0;
        if (addParenthesis) {
            if (requiredOrProhibited != null) {
                corrSb.insert(0, requiredOrProhibited);
                oneForReqOrProhib++;
            }
            corrSb.insert(0, '(');
            corrSb.append(')');
        }
        corr = corrSb.toString();
        int startIndex = tok.startOffset() + offset - oneForReqOrProhib;
        int endIndex = tok.endOffset() + offset;
        collation.replace(startIndex, endIndex, corr);
        offset += corr.length() - oneForReqOrProhib - (tok.endOffset() - tok.startOffset());
    }
    return collation.toString();
}