Java Code Examples for org.elasticsearch.index.query.QueryBuilders#termsQuery()
The following examples show how to use
org.elasticsearch.index.query.QueryBuilders#termsQuery() .
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: NameValue.java From conductor with Apache License 2.0 | 6 votes |
@Override public QueryBuilder getFilterBuilder() { if (op.getOperator().equals(Operators.EQUALS.value())) { return QueryBuilders.queryStringQuery(name.getName() + ":" + value.getValue().toString()); } else if (op.getOperator().equals(Operators.BETWEEN.value())) { return QueryBuilders.rangeQuery(name.getName()).from(range.getLow()).to(range.getHigh()); } else if (op.getOperator().equals(Operators.IN.value())) { return QueryBuilders.termsQuery(name.getName(), valueList.getList()); } else if (op.getOperator().equals(Operators.NOT_EQUALS.value())) { return QueryBuilders.queryStringQuery("NOT " + name.getName() + ":" + value.getValue().toString()); } else if (op.getOperator().equals(Operators.GREATER_THAN.value())) { return QueryBuilders.rangeQuery(name.getName()).from(value.getValue()).includeLower(false).includeUpper(false); } else if (op.getOperator().equals(Operators.IS.value())) { if (value.getSysConstant().equals(ConstValue.SystemConsts.NULL)) { return QueryBuilders.boolQuery().mustNot(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).mustNot(QueryBuilders.existsQuery(name.getName()))); } else if (value.getSysConstant().equals(ConstValue.SystemConsts.NOT_NULL)) { return QueryBuilders.boolQuery().mustNot(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(QueryBuilders.existsQuery(name.getName()))); } } else if (op.getOperator().equals(Operators.LESS_THAN.value())) { return QueryBuilders.rangeQuery(name.getName()).to(value.getValue()).includeLower(false).includeUpper(false); } else if (op.getOperator().equals(Operators.STARTS_WITH.value())) { return QueryBuilders.prefixQuery(name.getName(), value.getUnquotedValue()); } throw new IllegalStateException("Incorrect/unsupported operators"); }
Example 2
Source File: NameValue.java From conductor with Apache License 2.0 | 6 votes |
@Override public QueryBuilder getFilterBuilder() { if (op.getOperator().equals(Operators.EQUALS.value())) { return QueryBuilders.queryStringQuery(name.getName() + ":" + value.getValue().toString()); } else if (op.getOperator().equals(Operators.BETWEEN.value())) { return QueryBuilders.rangeQuery(name.getName()).from(range.getLow()).to(range.getHigh()); } else if (op.getOperator().equals(Operators.IN.value())) { return QueryBuilders.termsQuery(name.getName(), valueList.getList()); } else if (op.getOperator().equals(Operators.NOT_EQUALS.value())) { return QueryBuilders.queryStringQuery("NOT " + name.getName() + ":" + value.getValue().toString()); } else if (op.getOperator().equals(Operators.GREATER_THAN.value())) { return QueryBuilders.rangeQuery(name.getName()).from(value.getValue()).includeLower(false).includeUpper(false); } else if (op.getOperator().equals(Operators.IS.value())) { if (value.getSysConstant().equals(ConstValue.SystemConsts.NULL)) { return QueryBuilders.boolQuery().mustNot(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).mustNot(QueryBuilders.existsQuery(name.getName()))); } else if (value.getSysConstant().equals(ConstValue.SystemConsts.NOT_NULL)) { return QueryBuilders.boolQuery().mustNot(QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(QueryBuilders.existsQuery(name.getName()))); } } else if (op.getOperator().equals(Operators.LESS_THAN.value())) { return QueryBuilders.rangeQuery(name.getName()).to(value.getValue()).includeLower(false).includeUpper(false); } else if (op.getOperator().equals(Operators.STARTS_WITH.value())) { return QueryBuilders.prefixQuery(name.getName(), value.getUnquotedValue()); } throw new IllegalStateException("Incorrect/unsupported operators"); }
Example 3
Source File: SimpleSearchQueryBuilder.java From onetwo with Apache License 2.0 | 6 votes |
public SimpleBooleanQueryBuilder<PB> doTerms(String field, Consumer<TermsQueryBuilder> consumer, Object... values){ Assert.hasText(field); if(values==null || values.length==0){ return this; } List<Object> listValue = Lists.newArrayList(values); listValue.removeIf(Objects::isNull); if(listValue.isEmpty()){ return this; } TermsQueryBuilder termQueryBuilder = null; if(listValue.get(0) instanceof Collection){ Collection<?> colValue = (Collection<?>) listValue.get(0); if(!colValue.isEmpty()){ termQueryBuilder = QueryBuilders.termsQuery(field, colValue); } }else{ termQueryBuilder = QueryBuilders.termsQuery(field, listValue.toArray(new Object[0])); } // mustNot(termQueryBuilder); consumer.accept(termQueryBuilder); return this; }
Example 4
Source File: WhereParser.java From sql4es with Apache License 2.0 | 5 votes |
/** * Parses predicates of type IN (...) * @param node * @param state * @return */ private QueryWrapper processIn(InPredicate node, QueryState state) { String field = getVariableName(node.getValue()); FieldAndType fat = getFieldAndType(field, state); field = fat.getFieldName(); if(node.getValueList() instanceof InListExpression){ InListExpression list = (InListExpression)(node).getValueList(); List<Object> values = new ArrayList<Object>(); for(Expression listItem : list.getValues()){ Object value = this.getLiteralValue(listItem, state); if(state.hasException()) return null; values.add(value); } if(field.equals(Heading.ID)) { String[] types = new String[state.getSources().size()]; for(int i=0; i<types.length; i++) types[i] = state.getSources().get(i).getSource(); String[] ids = new String[values.size()]; return new QueryWrapper(QueryBuilders.idsQuery(types).addIds(values.toArray(ids))); } if(fat.getFieldType() == Types.REF) return new QueryWrapper(QueryBuilders.termsQuery(field, values), field.split("\\.")[0]); return new QueryWrapper(QueryBuilders.termsQuery(field, values)); }else { state.addException("SELECT ... IN can only be used with a list of values!"); return null; } }
Example 5
Source File: SearchConditionBuilder.java From search-spring-boot-starter with Apache License 2.0 | 4 votes |
/** * 根据查询条件,构建基础查询 * * @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 6
Source File: TermsQueryDemo.java From elasticsearch-full with Apache License 2.0 | 4 votes |
@Test public void testForClient() throws Exception { QueryBuilder qb = QueryBuilders.termsQuery("tags", "blue", "pill"); client.prepareSearch().setQuery(qb).execute().actionGet(); }
Example 7
Source File: QueryClauseInGenerator.java From molgenis with GNU Lesser General Public License v3.0 | 4 votes |
private Optional<QueryBuilder> createNonNullValuesQueryBuilder( EntityType entityType, List<Attribute> attributePath, Attribute attr, String fieldName, List<Object> queryValuesWithoutNulls) { Optional<QueryBuilder> nonNullQueryBuilder; AttributeType dataType = attr.getDataType(); switch (dataType) { case BOOL: case DATE: case DATE_TIME: case DECIMAL: case EMAIL: case ENUM: case HTML: case HYPERLINK: case INT: case LONG: case SCRIPT: case STRING: case TEXT: String termsQueryFieldName = fieldName; if (useNotAnalyzedField(attr)) { termsQueryFieldName += '.' + FIELD_NOT_ANALYZED; } QueryBuilder termsQueryBuilder = QueryBuilders.termsQuery(termsQueryFieldName, queryValuesWithoutNulls.toArray()); nonNullQueryBuilder = Optional.of(nestedQueryBuilder(entityType, attributePath, termsQueryBuilder)); break; case CATEGORICAL: case CATEGORICAL_MREF: case MREF: case XREF: case FILE: case ONE_TO_MANY: case COMPOUND: throw new MolgenisQueryException( format("Illegal data type [%s] for operator [%s]", dataType, Operator.IN)); default: throw new UnexpectedEnumException(dataType); } return nonNullQueryBuilder; }
Example 8
Source File: EsAbstractConditionQuery.java From fess with Apache License 2.0 | 4 votes |
protected TermsQueryBuilder regTermsQ(String name, Collection<?> values) { checkEsInvalidQueryCollection(name, values); TermsQueryBuilder termsQuery = QueryBuilders.termsQuery(name, values); regQ(termsQuery); return termsQuery; }
Example 9
Source File: EsAbstractConditionQuery.java From fess with Apache License 2.0 | 4 votes |
protected TermsQueryBuilder regTermsQ(String name, Collection<?> values) { checkEsInvalidQueryCollection(name, values); TermsQueryBuilder termsQuery = QueryBuilders.termsQuery(name, values); regQ(termsQuery); return termsQuery; }
Example 10
Source File: EsAbstractConditionQuery.java From fess with Apache License 2.0 | 4 votes |
protected TermsQueryBuilder regTermsQ(String name, Collection<?> values) { checkEsInvalidQueryCollection(name, values); TermsQueryBuilder termsQuery = QueryBuilders.termsQuery(name, values); regQ(termsQuery); return termsQuery; }
Example 11
Source File: ElasticSearchHelper.java From sunbird-lms-service with MIT License | 3 votes |
/** * This method returns TermsQueryBuilder with boosts if any provided * * @param key : field name * @param values : values for the field value * @param boost for increasing the search parameters priority * @return TermsQueryBuilder */ private static TermsQueryBuilder createTermsQuery(String key, List values, Float boost) { if (isNotNull(boost)) { return QueryBuilders.termsQuery(key, (values).stream().toArray(Object[]::new)).boost(boost); } else { return QueryBuilders.termsQuery(key, (values).stream().toArray(Object[]::new)); } }