Search in sources :

Example 36 with ValueSource

use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.

the class GroupingSearchTest method createRandomGroupingSearch.

private GroupingSearch createRandomGroupingSearch(String groupField, Sort groupSort, int docsInGroup, boolean canUseIDV) {
    GroupingSearch groupingSearch;
    if (random().nextBoolean()) {
        ValueSource vs = new BytesRefFieldSource(groupField);
        groupingSearch = new GroupingSearch(vs, new HashMap<>());
    } else {
        groupingSearch = new GroupingSearch(groupField);
    }
    groupingSearch.setGroupSort(groupSort);
    groupingSearch.setGroupDocsLimit(docsInGroup);
    if (random().nextBoolean()) {
        groupingSearch.setCachingInMB(4.0, true);
    }
    return groupingSearch;
}
Also used : HashMap(java.util.HashMap) ValueSource(org.apache.lucene.queries.function.ValueSource) BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource)

Example 37 with ValueSource

use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.

the class TestGrouping method createSecondPassCollector.

// Basically converts searchGroups from MutableValue to BytesRef if grouping by ValueSource
@SuppressWarnings("unchecked")
private TopGroupsCollector<?> createSecondPassCollector(FirstPassGroupingCollector<?> firstPassGroupingCollector, String groupField, Collection<SearchGroup<BytesRef>> searchGroups, Sort groupSort, Sort sortWithinGroup, int maxDocsPerGroup, boolean getScores, boolean getMaxScores, boolean fillSortFields) throws IOException {
    if (firstPassGroupingCollector.getGroupSelector().getClass().isAssignableFrom(TermGroupSelector.class)) {
        GroupSelector<BytesRef> selector = (GroupSelector<BytesRef>) firstPassGroupingCollector.getGroupSelector();
        return new TopGroupsCollector<>(selector, searchGroups, groupSort, sortWithinGroup, maxDocsPerGroup, getScores, getMaxScores, fillSortFields);
    } else {
        ValueSource vs = new BytesRefFieldSource(groupField);
        List<SearchGroup<MutableValue>> mvalSearchGroups = new ArrayList<>(searchGroups.size());
        for (SearchGroup<BytesRef> mergedTopGroup : searchGroups) {
            SearchGroup<MutableValue> sg = new SearchGroup<>();
            MutableValueStr groupValue = new MutableValueStr();
            if (mergedTopGroup.groupValue != null) {
                groupValue.value.copyBytes(mergedTopGroup.groupValue);
            } else {
                groupValue.exists = false;
            }
            sg.groupValue = groupValue;
            sg.sortValues = mergedTopGroup.sortValues;
            mvalSearchGroups.add(sg);
        }
        ValueSourceGroupSelector selector = new ValueSourceGroupSelector(vs, new HashMap<>());
        return new TopGroupsCollector<>(selector, mvalSearchGroups, groupSort, sortWithinGroup, maxDocsPerGroup, getScores, getMaxScores, fillSortFields);
    }
}
Also used : ArrayList(java.util.ArrayList) MutableValue(org.apache.lucene.util.mutable.MutableValue) BytesRefFieldSource(org.apache.lucene.queries.function.valuesource.BytesRefFieldSource) ValueSource(org.apache.lucene.queries.function.ValueSource) MutableValueStr(org.apache.lucene.util.mutable.MutableValueStr) BytesRef(org.apache.lucene.util.BytesRef)

Example 38 with ValueSource

use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.

the class SpatialDistanceQuery method getValueSource.

@Override
public ValueSource getValueSource(SchemaField field, QParser parser) {
    ArrayList<ValueSource> vs = new ArrayList<>(2);
    for (int i = 0; i < 2; i++) {
        SchemaField sub = subField(field, i, schema);
        vs.add(sub.getType().getValueSource(sub, parser));
    }
    return new LatLonValueSource(field, vs);
}
Also used : ValueSource(org.apache.lucene.queries.function.ValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) ArrayList(java.util.ArrayList) Point(org.locationtech.spatial4j.shape.Point)

Example 39 with ValueSource

use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.

the class NumericFieldType method getRangeQueryForFloatDoubleDocValues.

protected Query getRangeQueryForFloatDoubleDocValues(SchemaField sf, String min, String max, boolean minInclusive, boolean maxInclusive) {
    Query query;
    String fieldName = sf.getName();
    Number minVal = min == null ? null : getNumberType() == NumberType.FLOAT ? Float.parseFloat(min) : Double.parseDouble(min);
    Number maxVal = max == null ? null : getNumberType() == NumberType.FLOAT ? Float.parseFloat(max) : Double.parseDouble(max);
    Long minBits = min == null ? null : getNumberType() == NumberType.FLOAT ? (long) Float.floatToIntBits(minVal.floatValue()) : Double.doubleToLongBits(minVal.doubleValue());
    Long maxBits = max == null ? null : getNumberType() == NumberType.FLOAT ? (long) Float.floatToIntBits(maxVal.floatValue()) : Double.doubleToLongBits(maxVal.doubleValue());
    long negativeInfinityBits = getNumberType() == NumberType.FLOAT ? FLOAT_NEGATIVE_INFINITY_BITS : DOUBLE_NEGATIVE_INFINITY_BITS;
    long positiveInfinityBits = getNumberType() == NumberType.FLOAT ? FLOAT_POSITIVE_INFINITY_BITS : DOUBLE_POSITIVE_INFINITY_BITS;
    long minusZeroBits = getNumberType() == NumberType.FLOAT ? FLOAT_MINUS_ZERO_BITS : DOUBLE_MINUS_ZERO_BITS;
    long zeroBits = getNumberType() == NumberType.FLOAT ? FLOAT_ZERO_BITS : DOUBLE_ZERO_BITS;
    // If min is negative (or -0d) and max is positive (or +0d), then issue a FunctionRangeQuery
    if ((minVal == null || minVal.doubleValue() < 0d || minBits == minusZeroBits) && (maxVal == null || (maxVal.doubleValue() > 0d || maxBits == zeroBits))) {
        ValueSource vs = getValueSource(sf, null);
        query = new FunctionRangeQuery(new ValueSourceRangeFilter(vs, min, max, minInclusive, maxInclusive));
    } else {
        // If both max and min are negative (or -0d), then issue range query with max and min reversed
        if ((minVal == null || minVal.doubleValue() < 0d || minBits == minusZeroBits) && (maxVal != null && (maxVal.doubleValue() < 0d || maxBits == minusZeroBits))) {
            query = numericDocValuesRangeQuery(fieldName, maxBits, (min == null ? Long.valueOf(negativeInfinityBits) : minBits), maxInclusive, minInclusive, false);
        } else {
            // If both max and min are positive, then issue range query
            query = numericDocValuesRangeQuery(fieldName, minBits, (max == null ? Long.valueOf(positiveInfinityBits) : maxBits), minInclusive, maxInclusive, false);
        }
    }
    return query;
}
Also used : FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) FunctionRangeQuery(org.apache.solr.search.FunctionRangeQuery) ValueSourceRangeFilter(org.apache.solr.search.function.ValueSourceRangeFilter) ValueSource(org.apache.lucene.queries.function.ValueSource)

Example 40 with ValueSource

use of org.apache.lucene.queries.function.ValueSource in project lucene-solr by apache.

the class ExtendedDismaxQParser method parse.

@Override
public Query parse() throws SyntaxError {
    parsed = true;
    /* the main query we will execute.  we disable the coord because
     * this query is an artificial construct
     */
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    /* * * Main User Query * * */
    parsedUserQuery = null;
    String userQuery = getString();
    altUserQuery = null;
    if (userQuery == null || userQuery.trim().length() == 0) {
        // If no query is specified, we may have an alternate
        if (config.altQ != null) {
            QParser altQParser = subQuery(config.altQ, null);
            altUserQuery = altQParser.getQuery();
            query.add(altUserQuery, BooleanClause.Occur.MUST);
        } else {
            return null;
        // throw new SyntaxError("missing query string" );
        }
    } else {
        // There is a valid query string
        ExtendedSolrQueryParser up = createEdismaxQueryParser(this, IMPOSSIBLE_FIELD_NAME);
        up.addAlias(IMPOSSIBLE_FIELD_NAME, config.tiebreaker, config.queryFields);
        addAliasesFromRequest(up, config.tiebreaker);
        // slop for explicit user phrase queries
        up.setPhraseSlop(config.qslop);
        up.setAllowLeadingWildcard(true);
        // defer escaping and only do if lucene parsing fails, or we need phrases
        // parsing fails.  Need to sloppy phrase queries anyway though.
        List<Clause> clauses = splitIntoClauses(userQuery, false);
        // Always rebuild mainUserQuery from clauses to catch modifications from splitIntoClauses
        // This was necessary for userFields modifications to get propagated into the query.
        // Convert lower or mixed case operators to uppercase if we saw them.
        // only do this for the lucene query part and not for phrase query boosting
        // since some fields might not be case insensitive.
        // We don't use a regex for this because it might change and AND or OR in
        // a phrase query in a case sensitive field.
        String mainUserQuery = rebuildUserQuery(clauses, config.lowercaseOperators);
        // but always for unstructured implicit bqs created by getFieldQuery
        up.minShouldMatch = config.minShouldMatch;
        up.setSplitOnWhitespace(config.splitOnWhitespace);
        parsedUserQuery = parseOriginalQuery(up, mainUserQuery, clauses, config);
        if (parsedUserQuery == null) {
            parsedUserQuery = parseEscapedQuery(up, escapeUserQuery(clauses), config);
        }
        query.add(parsedUserQuery, BooleanClause.Occur.MUST);
        addPhraseFieldQueries(query, clauses, config);
    }
    /* * * Boosting Query * * */
    boostQueries = getBoostQueries();
    for (Query f : boostQueries) {
        query.add(f, BooleanClause.Occur.SHOULD);
    }
    /* * * Boosting Functions * * */
    List<Query> boostFunctions = getBoostFunctions();
    for (Query f : boostFunctions) {
        query.add(f, BooleanClause.Occur.SHOULD);
    }
    //
    // create a boosted query (scores multiplied by boosts)
    //
    Query topQuery = query.build();
    List<ValueSource> boosts = getMultiplicativeBoosts();
    if (boosts.size() > 1) {
        ValueSource prod = new ProductFloatFunction(boosts.toArray(new ValueSource[boosts.size()]));
        topQuery = new BoostedQuery(topQuery, prod);
    } else if (boosts.size() == 1) {
        topQuery = new BoostedQuery(topQuery, boosts.get(0));
    }
    return topQuery;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DisjunctionMaxQuery(org.apache.lucene.search.DisjunctionMaxQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostedQuery(org.apache.lucene.queries.function.BoostedQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ProductFloatFunction(org.apache.lucene.queries.function.valuesource.ProductFloatFunction) QueryValueSource(org.apache.lucene.queries.function.valuesource.QueryValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) BooleanClause(org.apache.lucene.search.BooleanClause) BoostedQuery(org.apache.lucene.queries.function.BoostedQuery)

Aggregations

ValueSource (org.apache.lucene.queries.function.ValueSource)54 Query (org.apache.lucene.search.Query)13 FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)12 SolrException (org.apache.solr.common.SolrException)11 SchemaField (org.apache.solr.schema.SchemaField)11 FunctionValues (org.apache.lucene.queries.function.FunctionValues)10 ArrayList (java.util.ArrayList)8 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)7 FieldType (org.apache.solr.schema.FieldType)6 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 AggValueSource (org.apache.solr.search.facet.AggValueSource)5 IOException (java.io.IOException)4 Map (java.util.Map)4 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)4 VectorValueSource (org.apache.lucene.queries.function.valuesource.VectorValueSource)4 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)4 IndexReader (org.apache.lucene.index.IndexReader)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)3 TermGroupSelector (org.apache.lucene.search.grouping.TermGroupSelector)3