Java Code Examples for org.elasticsearch.index.query.QueryBuilders#existsQuery()

The following examples show how to use org.elasticsearch.index.query.QueryBuilders#existsQuery() . 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: ExistApiMain.java    From elasticsearch-pool with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws IOException {
    RestHighLevelClient client = HighLevelClient.getInstance();
    try{
        ExistsQueryBuilder matchQueryBuilder = QueryBuilders.existsQuery("retcode");//查询某个字段存在的记录

        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(matchQueryBuilder);
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(1);

        SearchRequest searchRequest = new SearchRequest("serverlog_20180701");//限定index
        searchRequest.types("log");//限定type
        searchRequest.source(searchSourceBuilder);

        SearchResponse searchResponse = client.search(searchRequest);
        System.out.println(searchResponse);


    }finally{
        HighLevelClient.close();
    }
}
 
Example 2
Source File: ExistsQueryParser.java    From elasticsearch-sql with MIT License 5 votes vote down vote up
@Override
public AtomicQuery parse(ElasticsearchParser.IsClauseContext expression) {
    String field = expression.field.getText();
    if(expression.not!=null){
        return new AtomicQuery(QueryBuilders.existsQuery(field));
    }
    return new AtomicQuery(QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(field)));
}
 
Example 3
Source File: ElasticSearchHelper.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
/**
 * this method return ExistsQueryBuilder with boosts if any provided
 *
 * @param name of the field which required for exists operation
 * @param boost for increasing the search parameters priority
 * @return ExistsQueryBuilder
 */
private static ExistsQueryBuilder createExistQuery(String name, Float boost) {
  if (isNotNull(boost)) {
    return QueryBuilders.existsQuery(name).boost(boost);
  } else {
    return QueryBuilders.existsQuery(name);
  }
}
 
Example 4
Source File: SearchConditionBuilder.java    From search-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
/**
 * 根据查询条件,构建基础查询
 *
 * @param condition 查询条件
 * @return 基础查询
 */
public QueryBuilder builder(SearchCondition condition, IndexHelper helper) {

    String fieldName = condition.getFieldName();
    final Object singleValue = condition.getSingleValue();
    final boolean multipleValue = condition.getMultipleValue();
    final ConditionExpressionEnum expression = condition.getConditionExpression();

    QueryBuilder queryBuilder;
    switch (expression) {
        case EQUAL:
            fieldName = getFieldName(helper, fieldName, singleValue);
            queryBuilder = QueryBuilders.termQuery(fieldName, singleValue);
            break;
        case LESSER:
            queryBuilder = QueryBuilders.rangeQuery(fieldName).lt(singleValue);
            break;
        case GREATER:
            queryBuilder = QueryBuilders.rangeQuery(fieldName).gt(singleValue);
            break;
        case LESSER_OR_EQUAL:
            queryBuilder = QueryBuilders.rangeQuery(fieldName).lte(singleValue);
            break;
        case GREATER_OR_EQUAL:
            queryBuilder = QueryBuilders.rangeQuery(fieldName).gte(singleValue);
            break;
        case UNEQUAL:
            fieldName = getFieldName(helper, fieldName, singleValue);
            queryBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery(fieldName, singleValue));
            break;
        case LIKE:
            fieldName = getFieldName(helper, fieldName, singleValue);
            queryBuilder = QueryBuilders.wildcardQuery(fieldName, "*" + singleValue + "*");
            break;
        case NULL:
            queryBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(fieldName));
            break;
        case NOT_NULL:
            queryBuilder = QueryBuilders.existsQuery(fieldName);
            break;
        case IN:
            fieldName = getFieldName(helper, fieldName, condition.getFieldValues());
            queryBuilder = QueryBuilders.termsQuery(fieldName, condition.getFieldValues());
            break;
        case NOT_IN:
            fieldName = getFieldName(helper, fieldName, condition.getFieldValues());
            queryBuilder = QueryBuilders.boolQuery().mustNot(QueryBuilders.termsQuery(fieldName, condition.getFieldValues()));
            break;
        case BETWEEN:
            queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.rangeQuery(fieldName).gt(condition.getMinValue()).lt(condition.getMaxValue()));
            break;
        case BETWEEN_AND:
            queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.rangeQuery(fieldName).gte(condition.getMinValue()).lte(condition.getMaxValue()));
            break;
        case BETWEEN_LEFT:
            queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.rangeQuery(fieldName).gte(condition.getMinValue()).lt(condition.getMaxValue()));
            break;
        case BETWEEN_RIGHR:
            queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.rangeQuery(fieldName).gt(condition.getMinValue()).lte(condition.getMaxValue()));
            break;
        case MATCH:
            queryBuilder = QueryBuilders.matchQuery(fieldName, singleValue);
            break;
        default:
            throw new RuntimeException("表达不存在");
    }

    // 若是多值字段,需使用nestedQuery保证查询结果的准确性
    if (multipleValue) {
        if (fieldName.contains(".")) {
            fieldName = fieldName.substring(0, fieldName.lastIndexOf("."));
        }

        queryBuilder = QueryBuilders.nestedQuery(fieldName, queryBuilder, ScoreMode.None);
    }
    return queryBuilder;
}
 
Example 5
Source File: ExistsQueryDemo.java    From elasticsearch-full with Apache License 2.0 4 votes vote down vote up
@Test
public void testForClient() throws Exception {
    QueryBuilder qb = QueryBuilders.existsQuery("name");
    client.prepareSearch().setQuery(qb).execute().actionGet();
}
 
Example 6
Source File: ElasticsearchAnySearchDAO.java    From syncope with Apache License 2.0 4 votes vote down vote up
private static QueryBuilder fillAttrQuery(
        final PlainSchema schema,
        final PlainAttrValue attrValue,
        final AttrCond cond) {

    Object value = schema.getType() == AttrSchemaType.Date && attrValue.getDateValue() != null
            ? attrValue.getDateValue().getTime()
            : attrValue.getValue();

    QueryBuilder builder = EMPTY_QUERY_BUILDER;

    switch (cond.getType()) {
        case ISNOTNULL:
            builder = QueryBuilders.existsQuery(schema.getKey());
            break;

        case ISNULL:
            builder = QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery(schema.getKey()));
            break;

        case ILIKE:
            StringBuilder output = new StringBuilder();
            for (char c : cond.getExpression().toLowerCase().toCharArray()) {
                if (c == '%') {
                    output.append(".*");
                } else if (Character.isLetter(c)) {
                    output.append('[').
                            append(c).
                            append(Character.toUpperCase(c)).
                            append(']');
                } else {
                    output.append(c);
                }
            }
            builder = QueryBuilders.regexpQuery(schema.getKey(), output.toString());
            break;

        case LIKE:
            builder = QueryBuilders.wildcardQuery(schema.getKey(), cond.getExpression().replace('%', '*'));
            break;

        case IEQ:
            builder = QueryBuilders.matchQuery(schema.getKey(), cond.getExpression().toLowerCase());
            break;

        case EQ:
            builder = QueryBuilders.termQuery(schema.getKey(), value);
            break;

        case GE:
            builder = QueryBuilders.rangeQuery(schema.getKey()).gte(value);
            break;

        case GT:
            builder = QueryBuilders.rangeQuery(schema.getKey()).gt(value);
            break;

        case LE:
            builder = QueryBuilders.rangeQuery(schema.getKey()).lte(value);
            break;

        case LT:
            builder = QueryBuilders.rangeQuery(schema.getKey()).lt(value);
            break;

        default:
    }

    return builder;
}
 
Example 7
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 4 votes vote down vote up
protected ExistsQueryBuilder regExistsQ(String name) {
    ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery(name);
    regQ(existsQuery);
    return existsQuery;
}
 
Example 8
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 4 votes vote down vote up
protected ExistsQueryBuilder regExistsQ(String name) {
    ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery(name);
    regQ(existsQuery);
    return existsQuery;
}
 
Example 9
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 4 votes vote down vote up
protected ExistsQueryBuilder regExistsQ(String name) {
    ExistsQueryBuilder existsQuery = QueryBuilders.existsQuery(name);
    regQ(existsQuery);
    return existsQuery;
}