Java Code Examples for org.elasticsearch.index.query.RangeQueryBuilder#lt()

The following examples show how to use org.elasticsearch.index.query.RangeQueryBuilder#lt() . 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: BetweenAndQueryParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
public AtomicQuery parse(ElasticsearchParser.BetweenAndContext betweenAndContext) {
    if(betweenAndContext.rangeClause()!=null){
        ElasticsearchParser.RangeClauseContext rangeClauseContext=betweenAndContext.rangeClause();
        String field=rangeClauseContext.field.getText();
        RangeQueryBuilder queryBuilder=QueryBuilders.rangeQuery(field);
        String leftValue = StringManager.removeStringSymbol(rangeClauseContext.left.getText());
        String rightValue = StringManager.removeStringSymbol(rangeClauseContext.right.getText());
        if(rangeClauseContext.LPAREN()!=null){
            queryBuilder.gt(leftValue);
        }else{
            queryBuilder.gte(leftValue);
        }
        if(rangeClauseContext.RPAREN()!=null){
            queryBuilder.lt(rightValue);
        }else{
            queryBuilder.lte(rightValue);
        }
        return new AtomicQuery(queryBuilder);
    }
    return parseCondition(betweenAndContext, SqlConditionOperator.BetweenAnd,
            new Object[]{betweenAndContext.left.getText(), betweenAndContext.right.getText()},
            (field, operator, params) -> QueryBuilders.rangeQuery(field).gte(StringManager.removeStringSymbol(params[0].toString())).lte(StringManager.removeStringSymbol(params[1].toString())));
}
 
Example 2
Source File: ElasticSearchHelper.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
/**
 * This method returns RangeQueryBuilder with boosts if any provided
 *
 * @param name for the field
 * @param rangeOperation: keys and value related to range
 * @param boost for increasing the search parameters priority
 * @return RangeQueryBuilder
 */
private static RangeQueryBuilder createRangeQuery(
    String name, Map<String, Object> rangeOperation, Float boost) {

  RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(name + RAW_APPEND);
  for (Map.Entry<String, Object> it : rangeOperation.entrySet()) {
    switch (it.getKey()) {
      case LTE:
        rangeQueryBuilder.lte(it.getValue());
        break;
      case LT:
        rangeQueryBuilder.lt(it.getValue());
        break;
      case GTE:
        rangeQueryBuilder.gte(it.getValue());
        break;
      case GT:
        rangeQueryBuilder.gt(it.getValue());
        break;
    }
  }
  if (isNotNull(boost)) {
    return rangeQueryBuilder.boost(boost);
  }
  return rangeQueryBuilder;
}
 
Example 3
Source File: BaseQueryClauseRangeGenerator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) {
  List<Attribute> attributePath = getAttributePathExpanded(queryRule.getField(), entityType);
  Attribute attr = attributePath.get(attributePath.size() - 1);
  validateNumericalQueryField(attr);

  String fieldName = getQueryFieldName(attributePath);

  Object queryValue = getQueryValue(attr, queryRule.getValue());
  if (queryValue == null) {
    throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG);
  }

  RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery(fieldName);
  switch (getOperator()) {
    case GREATER:
      rangeQueryBuilder.gt(queryValue);
      break;
    case GREATER_EQUAL:
      rangeQueryBuilder.gte(queryValue);
      break;
    case LESS:
      rangeQueryBuilder.lt(queryValue);
      break;
    case LESS_EQUAL:
      rangeQueryBuilder.lte(queryValue);
      break;
    default:
      throw new UnexpectedEnumException(getOperator());
  }
  return QueryBuilders.constantScoreQuery(
      nestedQueryBuilder(entityType, attributePath, rangeQueryBuilder));
}
 
Example 4
Source File: QueryHelper.java    From fess with Apache License 2.0 5 votes vote down vote up
protected QueryBuilder convertTermRangeQuery(final QueryContext context, final TermRangeQuery termRangeQuery, final float boost) {
    final String field = getSearchField(context, termRangeQuery.getField());
    if (isSearchField(field)) {
        context.addFieldLog(field, termRangeQuery.toString(field));
        final RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery(field);
        final BytesRef min = termRangeQuery.getLowerTerm();
        if (min != null) {
            if (termRangeQuery.includesLower()) {
                rangeQuery.gte(min.utf8ToString());
            } else {
                rangeQuery.gt(min.utf8ToString());
            }
        }
        final BytesRef max = termRangeQuery.getUpperTerm();
        if (max != null) {
            if (termRangeQuery.includesUpper()) {
                rangeQuery.lte(max.utf8ToString());
            } else {
                rangeQuery.lt(max.utf8ToString());
            }
        }
        rangeQuery.boost(boost);
        return rangeQuery;
    } else {
        final String origQuery = termRangeQuery.toString();
        context.addFieldLog(Constants.DEFAULT_FIELD, origQuery);
        context.addHighlightedQuery(origQuery);
        return buildDefaultQueryBuilder((f, b) -> QueryBuilders.matchPhraseQuery(f, origQuery).boost(b));
    }
}
 
Example 5
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 5 votes vote down vote up
protected void addRangeC(RangeQueryBuilder builder, ConditionKey ck, Object value) {
    assertObjectNotNull("ck", ck);
    if (ck.equals(ConditionKey.CK_GREATER_THAN)) {
        builder.gt(value);
    } else if (ck.equals(ConditionKey.CK_GREATER_EQUAL)) {
        builder.gte(value);
    } else if (ck.equals(ConditionKey.CK_LESS_THAN)) {
        builder.lt(value);
    } else if (ck.equals(ConditionKey.CK_LESS_EQUAL)) {
        builder.lte(value);
    }
}
 
Example 6
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 5 votes vote down vote up
protected void addRangeC(RangeQueryBuilder builder, ConditionKey ck, Object value) {
    assertObjectNotNull("ck", ck);
    if (ck.equals(ConditionKey.CK_GREATER_THAN)) {
        builder.gt(value);
    } else if (ck.equals(ConditionKey.CK_GREATER_EQUAL)) {
        builder.gte(value);
    } else if (ck.equals(ConditionKey.CK_LESS_THAN)) {
        builder.lt(value);
    } else if (ck.equals(ConditionKey.CK_LESS_EQUAL)) {
        builder.lte(value);
    }
}
 
Example 7
Source File: EsAbstractConditionQuery.java    From fess with Apache License 2.0 5 votes vote down vote up
protected void addRangeC(RangeQueryBuilder builder, ConditionKey ck, Object value) {
    assertObjectNotNull("ck", ck);
    if (ck.equals(ConditionKey.CK_GREATER_THAN)) {
        builder.gt(value);
    } else if (ck.equals(ConditionKey.CK_GREATER_EQUAL)) {
        builder.gte(value);
    } else if (ck.equals(ConditionKey.CK_LESS_THAN)) {
        builder.lt(value);
    } else if (ck.equals(ConditionKey.CK_LESS_EQUAL)) {
        builder.lte(value);
    }
}
 
Example 8
Source File: LogConsoleServiceImpl.java    From super-cloudops with Apache License 2.0 4 votes vote down vote up
/**
 * Query log from ES documents
 * 
 * @param index
 * @param startTime
 * @param endTime
 * @param from
 * @param limit
 * @param queryList
 * @param level
 * @return
 * @throws Exception
 */
protected List<Log> queryLogFromESDocuments(String index, Long startTime, Long endTime, Integer from, Integer limit,
		List<Querycriteria> queryList, Integer level) throws Exception {

	// create bool query
	BoolQueryBuilder boolQueryBuilder = boolQuery();

	// fix key match
	if (!CollectionUtils.isEmpty(queryList)) {
		queryList.forEach(u -> {
			String query = u.getValue().trim();
			if (StringUtils.isEmpty(query)) {
				return;
			}
			// con = QueryParser.escape(con);
			query = "\"" + query + "\"";// for Special characters
			if (u.isEnable()) {// enable? must match
				// boolQueryBuilder.must(fuzzyQuery(KEY_DEFAULT_MSG,
				// con));
				boolQueryBuilder.must(queryStringQuery(query).field(KEY_DEFAULT_MSG));
			} else {// not enbale ? must not match
				// boolQueryBuilder.mustNot(fuzzyQuery(KEY_DEFAULT_MSG,
				// con));
				boolQueryBuilder.mustNot(queryStringQuery(query).field(KEY_DEFAULT_MSG));
			}
		});
	}

	// fix log level match
	if (!Objects.isNull(level) && level > 0) {
		BoolQueryBuilder boolQueryBuilder1 = boolQuery();
		for (int i = level - 1; i < LOG_LEVEL.size(); i++) {
			boolQueryBuilder1.should(matchQuery(KEY_DEFAULT_MSG, LOG_LEVEL.get(i)));
		}
		boolQueryBuilder.must(boolQueryBuilder1);
	}

	// fix time range
	if (null != startTime && null != endTime && (endTime != 0 || startTime != 0)) {
		RangeQueryBuilder rqb = rangeQuery("@timestamp").timeZone(DateTimeZone.UTC.toString());
		if (null != startTime) {
			rqb.gte(DateUtils2.timeToUTC(startTime));
		}
		if (null != endTime) {
			rqb.lt(DateUtils2.timeToUTC(endTime));
		}
		boolQueryBuilder.filter(rqb);
	}

	SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
	sourceBuilder.query(boolQueryBuilder);
	sourceBuilder.from(Objects.isNull(from) ? 0 : from);// from
	sourceBuilder.size(Objects.isNull(limit) ? 100 : limit);// limit
	// order by timestamp desc
	sourceBuilder.sort(new FieldSortBuilder("@timestamp").order(SortOrder.DESC));

	SearchRequest searchRequest = new SearchRequest(index);
	// searchRequest.types("doc"); // useful
	searchRequest.source(sourceBuilder);
	List<Log> logList = logHandler.findAll(searchRequest);
	return logList;
}