Search in sources :

Example 1 with Feature

use of com.yahoo.search.predicate.index.Feature in project vespa by vespa-engine.

the class PredicateTreeAnalyzer method findMinFeature.

// Second analysis pass. Traverses tree in depth-first order. Determines the min-feature value.
private static double findMinFeature(Predicate predicate, boolean isNegated, AnalyzerContext context) {
    if (predicate instanceof Conjunction) {
        // Sum of children values.
        return ((Conjunction) predicate).getOperands().stream().mapToDouble(child -> findMinFeature(child, isNegated, context)).sum();
    } else if (predicate instanceof FeatureConjunction) {
        if (isNegated) {
            return 0.0;
        }
        // The FeatureConjunction is handled as a leaf node in the interval algorithm.
        IndexableFeatureConjunction ifc = new IndexableFeatureConjunction((FeatureConjunction) predicate);
        return 1.0 / context.conjunctionOccurrences.get(ifc.id);
    } else if (predicate instanceof Disjunction) {
        // Minimum value of children.
        return ((Disjunction) predicate).getOperands().stream().mapToDouble(child -> findMinFeature(child, isNegated, context)).min().getAsDouble();
    } else if (predicate instanceof Negation) {
        return findMinFeature(((Negation) predicate).getOperand(), !isNegated, context);
    } else if (predicate instanceof FeatureSet) {
        if (isNegated) {
            return 0.0;
        }
        double minFeature = 1.0;
        FeatureSet featureSet = (FeatureSet) predicate;
        for (String value : featureSet.getValues()) {
            long featureHash = Feature.createHash(featureSet.getKey(), value);
            // Clever mathematics to handle scenarios where same feature is used several places in predicate tree.
            minFeature = Math.min(minFeature, 1.0 / context.featureOccurrences.get(featureHash));
        }
        return minFeature;
    } else if (predicate instanceof FeatureRange) {
        if (isNegated) {
            return 0.0;
        }
        return 1.0 / context.featureOccurrences.get(PredicateHash.hash64(((FeatureRange) predicate).getKey()));
    } else {
        throw new UnsupportedOperationException("Cannot handle predicate of type " + predicate.getClass().getSimpleName());
    }
}
Also used : FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet) Map(java.util.Map) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Negation(com.yahoo.document.predicate.Negation) PredicateHash(com.yahoo.document.predicate.PredicateHash) HashMap(java.util.HashMap) Feature(com.yahoo.search.predicate.index.Feature) Conjunction(com.yahoo.document.predicate.Conjunction) Disjunction(com.yahoo.document.predicate.Disjunction) Predicate(com.yahoo.document.predicate.Predicate) Disjunction(com.yahoo.document.predicate.Disjunction) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Negation(com.yahoo.document.predicate.Negation) FeatureConjunction(com.yahoo.document.predicate.FeatureConjunction) IndexableFeatureConjunction(com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction) Conjunction(com.yahoo.document.predicate.Conjunction) FeatureRange(com.yahoo.document.predicate.FeatureRange) FeatureSet(com.yahoo.document.predicate.FeatureSet)

Aggregations

Conjunction (com.yahoo.document.predicate.Conjunction)1 Disjunction (com.yahoo.document.predicate.Disjunction)1 FeatureConjunction (com.yahoo.document.predicate.FeatureConjunction)1 FeatureRange (com.yahoo.document.predicate.FeatureRange)1 FeatureSet (com.yahoo.document.predicate.FeatureSet)1 Negation (com.yahoo.document.predicate.Negation)1 Predicate (com.yahoo.document.predicate.Predicate)1 PredicateHash (com.yahoo.document.predicate.PredicateHash)1 Feature (com.yahoo.search.predicate.index.Feature)1 IndexableFeatureConjunction (com.yahoo.search.predicate.index.conjunction.IndexableFeatureConjunction)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1