Search in sources :

Example 1 with QueryTerm

use of org.apache.stanbol.entityhub.yard.solr.query.QueryUtils.QueryTerm in project stanbol by apache.

the class WildcardEncoder method encode.

@Override
public void encode(EncodedConstraintParts constraint, ConstraintValue value) {
    if (value == null || value.getValues().isEmpty()) {
        throw new IllegalArgumentException("This encoder does not support the NULL IndexValue!");
    }
    // encode the value based on the type
    Set<String> queryConstraints = new HashSet<String>();
    //the query constraints used for the phrase constraint
    Collection<String> phraseTerms = new ArrayList<String>();
    for (IndexValue indexValue : value) {
        if (indexValue != null) {
            if (!SUPPORTED_TYPES.contains(indexValue.getType())) {
                throw new IllegalArgumentException(String.format("This encoder does not support the IndexDataType %s (supported: %s)", indexValue.getType(), SUPPORTED_TYPES));
            } else {
                for (QueryTerm qt : QueryUtils.encodeQueryValue(indexValue, false)) {
                    StringBuilder sb = new StringBuilder(qt.needsQuotes ? qt.term.length() + 2 : 0);
                    if (qt.needsQuotes) {
                        sb.append('"').append(qt.term).append('"');
                        queryConstraints.add(sb.toString());
                    } else {
                        queryConstraints.add(qt.term);
                    }
                    if (value.getBoost() != null) {
                        sb.append("^").append(value.getBoost());
                    }
                    if (!qt.hasWildcard && qt.isText) {
                        //phrases do not work with wildcard and are only
                        //relevant for texts
                        phraseTerms.add(qt.term);
                    }
                }
            }
            if (value.getMode() == MODE.any) {
                //in any mode
                //we need to add constraints separately (to connect them with OR)
                constraint.addEncoded(POS, queryConstraints);
                queryConstraints.clear();
            }
        }
    // else ignore null value
    }
    if (value.getMode() == MODE.all) {
        // an all mode we need to add all
        //constraint in a single call (to connect them with AND)
        constraint.addEncoded(POS, queryConstraints);
    } else {
        if (phraseTerms.size() > 1) {
            Boolean state = (Boolean) value.getProperty(QueryConst.PHRASE_QUERY_STATE);
            if (state != null && state.booleanValue()) {
                StringBuilder sb = encodePhraseQuery(phraseTerms);
                if (value.getBoost() != null) {
                    sb.append("^").append(value.getBoost());
                }
                constraint.addEncoded(POS, sb.toString());
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) QueryTerm(org.apache.stanbol.entityhub.yard.solr.query.QueryUtils.QueryTerm) IndexValue(org.apache.stanbol.entityhub.yard.solr.model.IndexValue) HashSet(java.util.HashSet)

Example 2 with QueryTerm

use of org.apache.stanbol.entityhub.yard.solr.query.QueryUtils.QueryTerm in project stanbol by apache.

the class AssignmentEncoder method encode.

@Override
public void encode(EncodedConstraintParts constraint, ConstraintValue value) {
    if (value == null) {
        //if no value is parsed
        // add the default
        constraint.addEncoded(POS, EQ);
        //and return
        return;
    }
    //else encode the values and add them depending on the MODE
    Set<String> queryConstraints = new HashSet<String>();
    Collection<String> phraseTerms = new ArrayList<String>();
    for (IndexValue indexValue : value) {
        QueryTerm[] qts = QueryUtils.encodeQueryValue(indexValue, true);
        if (qts != null) {
            for (QueryTerm qt : qts) {
                StringBuilder sb = new StringBuilder(qt.term.length() + (qt.needsQuotes ? 3 : 1));
                sb.append(EQ);
                if (qt.needsQuotes) {
                    sb.append('"').append(qt.term).append('"');
                } else {
                    sb.append(qt.term);
                }
                if (value.getBoost() != null) {
                    sb.append("^").append(value.getBoost());
                }
                queryConstraints.add(sb.toString());
                if (!qt.hasWildcard && qt.isText) {
                    phraseTerms.add(qt.term);
                }
            }
        } else {
            queryConstraints.add(EQ);
        }
        if (value.getMode() == MODE.any) {
            //in any mode we need to add values separately
            constraint.addEncoded(POS, queryConstraints);
            //addEncoded copies the added values so we can clear and reuse
            queryConstraints.clear();
        }
    }
    if (value.getMode() == MODE.all) {
        //in all mode we need to add all values in a single call
        constraint.addEncoded(POS, queryConstraints);
    //NOTE also that for ALL mode Phrase queries do not make sense, as
    //     they would weaken the selection criteria
    } else {
        if (phraseTerms.size() > 1) {
            Boolean state = (Boolean) value.getProperty(QueryConst.PHRASE_QUERY_STATE);
            if (state != null && state.booleanValue()) {
                StringBuilder sb = encodePhraseQuery(phraseTerms);
                sb.insert(0, EQ);
                if (value.getBoost() != null) {
                    sb.append("^").append(value.getBoost());
                }
                constraint.addEncoded(POS, sb.toString());
            }
        //phrase query deactivated
        }
    //else for less than two terms we can not build a phrase query
    }
}
Also used : ArrayList(java.util.ArrayList) QueryTerm(org.apache.stanbol.entityhub.yard.solr.query.QueryUtils.QueryTerm) IndexValue(org.apache.stanbol.entityhub.yard.solr.model.IndexValue) HashSet(java.util.HashSet)

Aggregations

ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 IndexValue (org.apache.stanbol.entityhub.yard.solr.model.IndexValue)2 QueryTerm (org.apache.stanbol.entityhub.yard.solr.query.QueryUtils.QueryTerm)2