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

The following examples show how to use org.elasticsearch.common.lucene.search.Queries#newMatchNoDocsQuery() . 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: NumberFieldMapper.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public Query termsQuery(String field, List<Object> values) {
    long[] v = new long[values.size()];
    int upTo = 0;

    for (int i = 0; i < values.size(); i++) {
        Object value = values.get(i);
        if (!hasDecimalPart(value)) {
            v[upTo++] = parse(value, true);
        }
    }

    if (upTo == 0) {
        return Queries.newMatchNoDocsQuery("All values have a decimal part");
    }
    if (upTo != v.length) {
        v = Arrays.copyOf(v, upTo);
    }
    return LongPoint.newSetQuery(field, v);
}
 
Example 3
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 4
Source File: EqQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query apply(Function input, LuceneQueryBuilder.Context context) {
    RefAndLiteral refAndLiteral = RefAndLiteral.of(input);
    if (refAndLiteral == null) {
        return null;
    }
    Reference reference = refAndLiteral.reference();
    Literal literal = refAndLiteral.literal();
    String columnName = reference.column().fqn();
    MappedFieldType fieldType = context.getFieldTypeOrNull(columnName);
    if (reference.valueType().id() == ObjectType.ID) {
        //noinspection unchecked
        return refEqObject(input, reference, (Map<String, Object>) literal.value(), context);
    }
    if (fieldType == null) {
        // field doesn't exist, can't match
        return Queries.newMatchNoDocsQuery("column does not exist in this index");
    }
    if (DataTypes.isArray(reference.valueType()) &&
        DataTypes.isArray(literal.valueType())) {

        List values = LuceneQueryBuilder.asList(literal);
        if (values.isEmpty()) {
            return genericFunctionFilter(input, context);
        }
        Query termsQuery = LuceneQueryBuilder.termsQuery(fieldType, values, context.queryShardContext);

        // wrap boolTermsFilter and genericFunction filter in an additional BooleanFilter to control the ordering of the filters
        // termsFilter is applied first
        // afterwards the more expensive genericFunctionFilter
        BooleanQuery.Builder filterClauses = new BooleanQuery.Builder();
        filterClauses.add(termsQuery, BooleanClause.Occur.MUST);
        filterClauses.add(genericFunctionFilter(input, context), BooleanClause.Occur.MUST);
        return filterClauses.build();
    }
    return fieldType.termQuery(literal.value(), context.queryShardContext);
}
 
Example 5
Source File: NumberFieldMapper.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query termQuery(String field, Object value) {
    if (hasDecimalPart(value)) {
        return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part");
    }
    int v = parse(value, true);
    return IntPoint.newExactQuery(field, v);
}
 
Example 6
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 7
Source File: ExistsQueryBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
private static Query newFieldExistsQuery(QueryShardContext context, String field) {
    MappedFieldType fieldType = context.getMapperService().fullName(field);
    if (fieldType == null) {
        // The field does not exist as a leaf but could be an object so
        // check for an object mapper
        if (context.getObjectMapper(field) != null) {
            return newObjectFieldExistsQuery(context, field);
        }
        return Queries.newMatchNoDocsQuery("No field \"" + field + "\" exists in mappings.");
    }
    Query filter = fieldType.existsQuery(context);
    return new ConstantScoreQuery(filter);
}
 
Example 8
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 9
Source File: LuceneQueryBuilder.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query visitReference(Reference symbol, Context context) {
    // called for queries like: where boolColumn
    if (symbol.valueType() == DataTypes.BOOLEAN) {
        MappedFieldType fieldType = context.getFieldTypeOrNull(symbol.column().fqn());
        if (fieldType == null) {
            return Queries.newMatchNoDocsQuery("column does not exist in this index");
        }
        return fieldType.termQuery(true, context.queryShardContext());
    }
    return super.visitReference(symbol, context);
}
 
Example 10
Source File: AnyNeqQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
protected Query refMatchesAnyArrayLiteral(Reference candidate, Literal array, LuceneQueryBuilder.Context context) {
    //  col != ANY ([1,2,3]) --> not(col=1 and col=2 and col=3)
    String columnName = candidate.column().fqn();
    MappedFieldType fieldType = context.getFieldTypeOrNull(columnName);
    if (fieldType == null) {
        return Queries.newMatchNoDocsQuery("column does not exist in this index");
    }

    BooleanQuery.Builder andBuilder = new BooleanQuery.Builder();
    for (Object value : toIterable(array.value())) {
        andBuilder.add(fieldType.termQuery(value, context.queryShardContext()), BooleanClause.Occur.MUST);
    }
    return Queries.not(andBuilder.build());
}
 
Example 11
Source File: IndexQueryParserService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private ParsedQuery innerParse(QueryParseContext parseContext, XContentParser parser) throws IOException, QueryParsingException {
    parseContext.reset(parser);
    try {
        parseContext.parseFieldMatcher(parseFieldMatcher);
        Query query = parseContext.parseInnerQuery();
        if (query == null) {
            query = Queries.newMatchNoDocsQuery();
        }
        return new ParsedQuery(query, parseContext.copyNamedQueries());
    } finally {
        parseContext.reset(null);
    }
}
 
Example 12
Source File: IndexQueryParserService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Nullable
public Query parseInnerQuery(QueryParseContext parseContext) throws IOException {
    parseContext.parseFieldMatcher(parseFieldMatcher);
    Query query = parseContext.parseInnerQuery();
    if (query == null) {
        query = Queries.newMatchNoDocsQuery();
    }
    return query;
}
 
Example 13
Source File: QueryBuilderHelper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query eq(String columnName, Object value) {
    if (value == null) {
        return Queries.newMatchNoDocsQuery();
    }
    return new TermQuery(new Term(columnName, (BytesRef)value));
}
 
Example 14
Source File: QueryBuilderHelper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query eq(String columnName, Object value) {
    if (value == null) {
        return Queries.newMatchNoDocsQuery();
    }
    return new TermQuery(new Term(columnName, valueForSearch(value)));
}
 
Example 15
Source File: QueryBuilderHelper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query eq(String columnName, Object value) {
    if (value == null) {
        return Queries.newMatchNoDocsQuery();
    }
    return new TermQuery(new Term(columnName, (boolean)value ? "T" : "F"));
}
 
Example 16
Source File: ExistsQueryParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static Query newFilter(QueryParseContext parseContext, String fieldPattern, String queryName) {
    final FieldNamesFieldMapper.FieldNamesFieldType fieldNamesFieldType = (FieldNamesFieldMapper.FieldNamesFieldType)parseContext.mapperService().fullName(FieldNamesFieldMapper.NAME);
    if (fieldNamesFieldType == null) {
        // can only happen when no types exist, so no docs exist either
        return Queries.newMatchNoDocsQuery();
    }

    ObjectMapper objectMapper = parseContext.getObjectMapper(fieldPattern);
    if (objectMapper != null) {
        // automatic make the object mapper pattern
        fieldPattern = fieldPattern + ".*";
    }

    Collection<String> fields = parseContext.simpleMatchToIndexNames(fieldPattern);
    if (fields.isEmpty()) {
        // no fields exists, so we should not match anything
        return Queries.newMatchNoDocsQuery();
    }

    BooleanQuery.Builder boolFilterBuilder = new BooleanQuery.Builder();
    for (String field : fields) {
        MappedFieldType fieldType = parseContext.fieldMapper(field);
        Query filter = null;
        if (fieldNamesFieldType.isEnabled()) {
            final String f;
            if (fieldType != null) {
                f = fieldType.names().indexName();
            } else {
                f = field;
            }
            filter = fieldNamesFieldType.termQuery(f, parseContext);
        }
        // if _field_names are not indexed, we need to go the slow way
        if (filter == null && fieldType != null) {
            filter = fieldType.rangeQuery(null, null, true, true);
        }
        if (filter == null) {
            filter = new TermRangeQuery(field, null, null, true, true);
        }
        boolFilterBuilder.add(filter, BooleanClause.Occur.SHOULD);
    }

    BooleanQuery boolFilter = boolFilterBuilder.build();
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, boolFilter);
    }
    return new ConstantScoreQuery(boolFilter);
}
 
Example 17
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 18
Source File: LuceneQueryBuilder.java    From crate with Apache License 2.0 4 votes vote down vote up
static Query termsQuery(@Nullable MappedFieldType fieldType, List values, QueryShardContext context) {
    if (fieldType == null) {
        return Queries.newMatchNoDocsQuery("column does not exist in this index");
    }
    return fieldType.termsQuery(values, context);
}
 
Example 19
Source File: QueryBuilderHelper.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Query eq(String columnName, Object value) {
    if (value == null) {
        return Queries.newMatchNoDocsQuery();
    }
    return rangeQuery(columnName, value, value, true, true);
}
 
Example 20
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();
}