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

The following examples show how to use org.elasticsearch.common.lucene.search.Queries#not() . 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 5 votes vote down vote up
@Override
protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context) throws IOException {
    //  col != ANY ([1,2,3]) --> not(col=1 and col=2 and col=3)
    String columnName = reference.info().ident().columnIdent().fqn();
    QueryBuilderHelper helper = QueryBuilderHelper.forType(reference.valueType());

    BooleanQuery.Builder andBuilder = new BooleanQuery.Builder();
    for (Object value : toIterable(arrayLiteral.value())) {
        andBuilder.add(helper.eq(columnName, value), BooleanClause.Occur.MUST);
    }
    return Queries.not(andBuilder.build());
}
 
Example 2
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected Query applyArrayLiteral(Reference reference, Literal arrayLiteral, Context context) throws IOException {
    // col not like ANY (['a', 'b']) --> not(and(like(col, 'a'), like(col, 'b')))
    String columnName = reference.ident().columnIdent().fqn();
    QueryBuilderHelper builder = QueryBuilderHelper.forType(reference.valueType());

    BooleanQuery.Builder andLikeQueries = new BooleanQuery.Builder();
    for (Object value : toIterable(arrayLiteral.value())) {
        andLikeQueries.add(builder.like(columnName, value, context.indexCache.query()), BooleanClause.Occur.MUST);
    }
    return Queries.not(andLikeQueries.build());
}
 
Example 3
Source File: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query apply(Function parent, Function inner, Context context) throws IOException {
    FunctionLiteralPair outerPair = new FunctionLiteralPair(parent);
    if (!outerPair.isValid()) {
        return null;
    }
    Query query = getQuery(inner, context);
    if (query == null) return null;
    Boolean negate = !(Boolean) outerPair.input().value();
    if (negate) {
        return Queries.not(query);
    } else {
        return query;
    }
}
 
Example 4
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 5
Source File: AnyNotLikeQuery.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 not like ANY (['a', 'b']) --> not(and(like(col, 'a'), like(col, 'b')))
    String columnName = candidate.column().fqn();
    MappedFieldType fieldType = context.getFieldTypeOrNull(columnName);

    BooleanQuery.Builder andLikeQueries = new BooleanQuery.Builder();
    for (Object value : toIterable(array.value())) {
        andLikeQueries.add(
            LikeQuery.like(candidate.valueType(), fieldType, value, ignoreCase),
            BooleanClause.Occur.MUST);
    }
    return Queries.not(andLikeQueries.build());
}
 
Example 6
Source File: WithinQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public Query apply(Function parent, Function inner, LuceneQueryBuilder.Context context) {
    FunctionLiteralPair outerPair = new FunctionLiteralPair(parent);
    if (!outerPair.isValid()) {
        return null;
    }
    Query query = getQuery(inner, context);
    if (query == null) return null;
    boolean negate = !(Boolean) outerPair.input().value();
    if (negate) {
        return Queries.not(query);
    } else {
        return query;
    }
}
 
Example 7
Source File: NotQueryParser.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 query = null;
    boolean queryFound = false;

    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 (parseContext.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
                query = parseContext.parseInnerFilter();
                queryFound = true;
            } else {
                queryFound = true;
                // its the filter, and the name is the field
                query = parseContext.parseInnerFilter(currentFieldName);
            }
        } else if (token.isValue()) {
            if ("_name".equals(currentFieldName)) {
                queryName = parser.text();
            } else {
                throw new QueryParsingException(parseContext, "[not] query does not support [" + currentFieldName + "]");
            }
        }
    }

    if (!queryFound) {
        throw new QueryParsingException(parseContext, "filter is required when using `not` query");
    }

    if (query == null) {
        return null;
    }

    Query notQuery = Queries.not(query);
    if (queryName != null) {
        parseContext.addNamedQuery(queryName, notQuery);
    }
    return notQuery;
}
 
Example 8
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;
    }
}