Java Code Examples for org.elasticsearch.common.lucene.search.Queries#newMatchAllQuery()

The following examples show how to use org.elasticsearch.common.lucene.search.Queries#newMatchAllQuery() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public Context convert(WhereClause whereClause,
                       MapperService mapperService,
                       IndexFieldDataService indexFieldDataService,
                       IndexCache indexCache) throws UnsupportedFeatureException {
    Context ctx = new Context(inputSymbolVisitor, mapperService, indexFieldDataService, indexCache);
    if (whereClause.noMatch()) {
        ctx.query = Queries.newMatchNoDocsQuery();
    } else if (!whereClause.hasQuery()) {
        ctx.query = Queries.newMatchAllQuery();
    } else {
        ctx.query = VISITOR.process(whereClause.query(), ctx);
    }
    if (LOGGER.isTraceEnabled()) {
        if (whereClause.hasQuery()) {
            LOGGER.trace("WHERE CLAUSE [{}] -> LUCENE QUERY [{}] ", SymbolPrinter.INSTANCE.printSimple(whereClause.query()), ctx.query);
        }
    }
    return ctx;
}
 
Example 2
Source File: LuceneQueryBuilder.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Query visitLiteral(Literal literal, Context context) {
    Object value = literal.value();
    if (value == null) {
        return Queries.newMatchNoDocsQuery("WHERE null -> no match");
    }
    try {
        return (boolean) value
            ? Queries.newMatchAllQuery()
            : Queries.newMatchNoDocsQuery("WHERE false -> no match");
    } catch (ClassCastException e) {
        // Throw a nice error if the top-level literal doesn't have a boolean type
        // (This is currently caught earlier, so this code is just a safe-guard)
        return visitSymbol(literal, context);
    }
}
 
Example 3
Source File: MatchAllQueryParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    float boost = 1.0f;
    String currentFieldName = null;

    XContentParser.Token token;
    while (((token = parser.nextToken()) != XContentParser.Token.END_OBJECT && token != XContentParser.Token.END_ARRAY)) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new QueryParsingException(parseContext, "[match_all] query does not support [" + currentFieldName + "]");
            }
        }
    }

    if (boost == 1.0f) {
        return Queries.newMatchAllQuery();
    }

    MatchAllDocsQuery query = new MatchAllDocsQuery();
    query.setBoost(boost);
    return query;
}
 
Example 4
Source File: LimitQueryParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    deprecationLogger.deprecated("The [limit] query is deprecated, please use the [terminate_after] parameter of the search API instead.");

    XContentParser parser = parseContext.parser();

    int limit = -1;
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if ("value".equals(currentFieldName)) {
                limit = parser.intValue();
            } else {
                throw new QueryParsingException(parseContext, "[limit] query does not support [" + currentFieldName + "]");
            }
        }
    }

    if (limit == -1) {
        throw new QueryParsingException(parseContext, "No value specified for limit query");
    }

    // this filter is deprecated and parses to a filter that matches everything
    return Queries.newMatchAllQuery();
}
 
Example 5
Source File: IndexFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * This termQuery impl looks at the context to determine the index that
 * is being queried and then returns a MATCH_ALL_QUERY or MATCH_NO_QUERY
 * if the value matches this index. This can be useful if aliases or
 * wildcards are used but the aim is to restrict the query to specific
 * indices
 */
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    if (context == null) {
        return super.termQuery(value, context);
    }
    if (isSameIndex(value, context.index().getName())) {
        return Queries.newMatchAllQuery();
    } else {
        return Queries.newMatchNoDocsQuery();
    }
}
 
Example 6
Source File: IndexFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query termsQuery(List values, QueryParseContext context) {
    if (context == null) {
        return super.termsQuery(values, context);
    }
    for (Object value : values) {
        if (isSameIndex(value, context.index().getName())) {
            // No need to OR these clauses - we can only logically be
            // running in the context of just one of these index names.
            return Queries.newMatchAllQuery();
        }
    }
    // None of the listed index names are this one
    return Queries.newMatchNoDocsQuery();
}
 
Example 7
Source File: MatchQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
protected Query zeroTermsQuery() {
    switch (zeroTermsQuery) {
        case NULL:
            return null;
        case NONE:
            return Queries.newMatchNoDocsQuery("Matching no documents because no terms present");
        case ALL:
            return Queries.newMatchAllQuery();
        default:
            throw new IllegalStateException("unknown zeroTermsQuery " + zeroTermsQuery);
    }
}
 
Example 8
Source File: MatchQueryBuilder.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private Query zeroTermsQuery() {
    return options.zeroTermsQuery() == MatchQuery.ZeroTermsQuery.NONE ?
            Queries.newMatchNoDocsQuery() :
            Queries.newMatchAllQuery();
}
 
Example 9
Source File: MapperQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected Query newMatchAllDocsQuery() {
    return Queries.newMatchAllQuery();
}
 
Example 10
Source File: IndicesQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    XContentParser parser = parseContext.parser();

    Query noMatchQuery = null;
    boolean queryFound = false;
    boolean indicesFound = false;
    boolean currentIndexMatchesIndices = false;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;
    XContentStructure.InnerQuery innerQuery = null;
    XContentStructure.InnerQuery innerNoMatchQuery = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
                innerQuery = new XContentStructure.InnerQuery(parseContext, null);
                queryFound = true;
            } else if (parseContext.parseFieldMatcher().match(currentFieldName, NO_MATCH_QUERY)) {
                innerNoMatchQuery = new XContentStructure.InnerQuery(parseContext, null);
            } else {
                throw new QueryParsingException(parseContext, "[indices] query does not support [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if ("indices".equals(currentFieldName)) {
                if (indicesFound) {
                    throw new QueryParsingException(parseContext, "[indices] indices or index already specified");
                }
                indicesFound = true;
                Collection<String> indices = new ArrayList<>();
                while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                    String value = parser.textOrNull();
                    if (value == null) {
                        throw new QueryParsingException(parseContext, "[indices] no value specified for 'indices' entry");
                    }
                    indices.add(value);
                }
                currentIndexMatchesIndices = matchesIndices(parseContext.index().name(), indices.toArray(new String[indices.size()]));
            } else {
                throw new QueryParsingException(parseContext, "[indices] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if ("index".equals(currentFieldName)) {
                if (indicesFound) {
                    throw new QueryParsingException(parseContext, "[indices] indices or index already specified");
                }
                indicesFound = true;
                currentIndexMatchesIndices = matchesIndices(parseContext.index().name(), parser.text());
            } else if (parseContext.parseFieldMatcher().match(currentFieldName, NO_MATCH_QUERY)) {
                String type = parser.text();
                if ("all".equals(type)) {
                    noMatchQuery = Queries.newMatchAllQuery();
                } else if ("none".equals(type)) {
                    noMatchQuery = Queries.newMatchNoDocsQuery();
                }
            } else if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new QueryParsingException(parseContext, "[indices] query does not support [" + currentFieldName + "]");
            }
        }
    }
    if (!queryFound) {
        throw new QueryParsingException(parseContext, "[indices] requires 'query' element");
    }
    if (!indicesFound) {
        throw new QueryParsingException(parseContext, "[indices] requires 'indices' or 'index' element");
    }

    Query chosenQuery;
    if (currentIndexMatchesIndices) {
        chosenQuery = innerQuery.asQuery();
    } else {
        // If noMatchQuery is set, it means "no_match_query" was "all" or "none"
        if (noMatchQuery != null) {
            chosenQuery = noMatchQuery;
        } else {
            // There might be no "no_match_query" set, so default to the match_all if not set
            if (innerNoMatchQuery == null) {
                chosenQuery = Queries.newMatchAllQuery();
            } else {
                chosenQuery = innerNoMatchQuery.asQuery();
            }
        }
    }
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, chosenQuery);
    }
    return chosenQuery;
}
 
Example 11
Source File: FunctionScoreQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private String parseFiltersAndFunctions(QueryParseContext parseContext, XContentParser parser,
                                        ArrayList<FiltersFunctionScoreQuery.FilterFunction> filterFunctions, String currentFieldName) throws IOException {
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
        Query filter = null;
        ScoreFunction scoreFunction = null;
        Float functionWeight = null;
        if (token != XContentParser.Token.START_OBJECT) {
            throw new QueryParsingException(parseContext, "failed to parse [{}]. malformed query, expected a [{}] while parsing functions but got a [{}] instead", XContentParser.Token.START_OBJECT, token, NAME);
        } else {
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (parseContext.parseFieldMatcher().match(currentFieldName, WEIGHT_FIELD)) {
                    functionWeight = parser.floatValue();
                } else {
                    if ("filter".equals(currentFieldName)) {
                        filter = parseContext.parseInnerFilter();
                    } else {
                        // do not need to check null here,
                        // functionParserMapper throws exception if parser
                        // non-existent
                        ScoreFunctionParser functionParser = functionParserMapper.get(parseContext, currentFieldName);
                        scoreFunction = functionParser.parse(parseContext, parser);
                    }
                }
            }
            if (functionWeight != null) {
                scoreFunction = new WeightFactorFunction(functionWeight, scoreFunction);
            }
        }
        if (filter == null) {
            filter = Queries.newMatchAllQuery();
        }
        if (scoreFunction == null) {
            throw new ElasticsearchParseException("failed to parse [{}] query. an entry in functions list is missing a function.", NAME);
        }
        filterFunctions.add(new FiltersFunctionScoreQuery.FilterFunction(filter, scoreFunction));

    }
    return currentFieldName;
}
 
Example 12
Source File: ParsedQuery.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static ParsedQuery parsedMatchAllQuery() {
    return new ParsedQuery(Queries.newMatchAllQuery(), ImmutableMap.<String, Query>of());
}
 
Example 13
Source File: FilteredQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
    deprecationLogger.deprecated("The [filtered] query is deprecated, please use a [bool] query instead with a [must] "
            + "clause for the query part and a [filter] clause for the filter part.");

    XContentParser parser = parseContext.parser();

    Query query = Queries.newMatchAllQuery();
    Query filter = null;
    boolean filterFound = false;
    float boost = 1.0f;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;

    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (parseContext.isDeprecatedSetting(currentFieldName)) {
            // skip
        } else if (token == XContentParser.Token.START_OBJECT) {
            if ("query".equals(currentFieldName)) {
                query = parseContext.parseInnerQuery();
            } else if ("filter".equals(currentFieldName)) {
                filterFound = true;
                filter = parseContext.parseInnerFilter();
            } else {
                throw new QueryParsingException(parseContext, "[filtered] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if ("strategy".equals(currentFieldName)) {
                // ignore
            } else if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else if ("boost".equals(currentFieldName)) {
                boost = parser.floatValue();
            } else {
                throw new QueryParsingException(parseContext, "[filtered] query does not support [" + currentFieldName + "]");
            }
        }
    }

    // parsed internally, but returned null during parsing...
    if (query == null) {
        return null;
    }

    BooleanQuery filteredQuery = Queries.filtered(query, filter);
    filteredQuery.setBoost(boost);
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, filteredQuery);
    }
    return filteredQuery;
}
 
Example 14
Source File: MatchQuery.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
protected Query zeroTermsQuery() {
    return zeroTermsQuery == ZeroTermsQuery.NONE ? Queries.newMatchNoDocsQuery() : Queries.newMatchAllQuery();
}
 
Example 15
Source File: DistanceQuery.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Create a LatLonPoint distance query.
 *
 *
 * <pre>
 *          , - ~ ~ ~ - ,
 *      , '               ' ,
 *    ,                       ,     X = lonLat coordinates
 *   ,                         ,
 *  ,              [distance]   ,
 *  ,            p<------------>X
 *  ,            ^              ,
 *   ,           |             ,
 *    ,      [columnName]     ,
 *      ,     point        , '
 *        ' - , _ _ _ ,  '
 *
 *  lt and lte -> match everything WITHIN distance
 *  gt and gte -> match everything OUTSIDE distance
 *
 *  eq distance ~ 0 -> match everything within distance + tolerance
 *
 *  eq distance > 0 -> build two circles, one slightly smaller, one slightly larger
 *                          distance must not be within the smaller distance but within the larger.
 * </pre>
 */
private static Query esV5DistanceQuery(Function parentFunction,
                                       LuceneQueryBuilder.Context context,
                                       String parentOperatorName,
                                       String columnName,
                                       Double distance,
                                       Point lonLat) {
    switch (parentOperatorName) {
        // We documented that using distance in the WHERE clause utilizes the index which isn't precise so treating
        // lte & lt the same should be acceptable
        case LteOperator.NAME:
        case LtOperator.NAME:
            return LatLonPoint.newDistanceQuery(columnName, lonLat.getY(), lonLat.getX(), distance);
        case GteOperator.NAME:
            if (distance - GeoUtils.TOLERANCE <= 0.0d) {
                return Queries.newMatchAllQuery();
            }
            // fall through
        case GtOperator.NAME:
            return Queries.not(LatLonPoint.newDistanceQuery(columnName, lonLat.getY(), lonLat.getX(), distance));
        case EqOperator.NAME:
            return eqDistance(parentFunction, context, columnName, distance, lonLat);
        default:
            return null;
    }
}
 
Example 16
Source File: EngineSearcherTotalHitsMatcher.java    From crate with Apache License 2.0 4 votes vote down vote up
static Matcher<Engine.Searcher> engineSearcherTotalHits(int totalHits) {
    return new EngineSearcherTotalHitsMatcher(Queries.newMatchAllQuery(), totalHits);
}