@Test void mapQueryRule() { QueryRule queryRule = mock(QueryRule.class); when(queryRule.getField()).thenReturn("attr"); when(queryRule.getValue()).thenReturn("val"); Attribute attribute = mock(Attribute.class); when(attribute.getDataType()).thenReturn(AttributeType.STRING); when(documentIdGenerator.generateId(attribute)).thenReturn("attr"); EntityType entityType = mock(EntityType.class); when(entityType.getAttributeByName("attr")).thenReturn(attribute); QueryBuilder queryBuilder = queryClauseFuzzyMatchNgramGenerator.mapQueryRule(queryRule, entityType); QueryBuilder expectedQueryBuilder = QueryBuilders.queryStringQuery("attr.ngram:(val)"); assertQueryBuilderEquals(expectedQueryBuilder, queryBuilder); }
QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { String queryField = queryRule.getField(); Object queryValue = queryRule.getValue(); QueryBuilder queryBuilder; if (queryValue == null) throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); if (queryField == null) { queryBuilder = QueryBuilders.matchQuery("_all", queryValue); } else { Attribute attr = entityType.getAttributeByName(queryField); AttributeType dataType = attr.getDataType(); switch (dataType) { 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: queryField = getQueryFieldName(attr) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"); break; case MREF: case XREF: queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.nestedQuery( getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max); break; default: throw new UnexpectedEnumException(dataType); } } return queryBuilder; }
QueryClauseFuzzyMatchNgramGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { String queryField = queryRule.getField(); Object queryValue = queryRule.getValue(); QueryBuilder queryBuilder; if (queryValue == null) throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); if (queryField == null) { queryBuilder = QueryBuilders.matchQuery("_all", queryValue); } else { Attribute attr = entityType.getAttributeByName(queryField); AttributeType dataType = attr.getDataType(); switch (dataType) { 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: queryField = getQueryFieldName(attr) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"); break; case MREF: case XREF: queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.nestedQuery( getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max); break; default: throw new UnexpectedEnumException(dataType); } } return queryBuilder; } }
QueryClauseFuzzyMatchNgramGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { String queryField = queryRule.getField(); Object queryValue = queryRule.getValue(); QueryBuilder queryBuilder; if (queryValue == null) throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); if (queryField == null) { queryBuilder = QueryBuilders.matchQuery("_all", queryValue); } else { Attribute attr = entityType.getAttributeByName(queryField); AttributeType dataType = attr.getDataType(); switch (dataType) { 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: queryField = getQueryFieldName(attr) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"); break; case MREF: case XREF: queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.nestedQuery( getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max); break; default: throw new UnexpectedEnumException(dataType); } } return queryBuilder; } QueryClauseFuzzyMatchNgramGenerator(DocumentIdGenerator documentIdGenerator); }
QueryClauseFuzzyMatchNgramGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { String queryField = queryRule.getField(); Object queryValue = queryRule.getValue(); QueryBuilder queryBuilder; if (queryValue == null) throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); if (queryField == null) { queryBuilder = QueryBuilders.matchQuery("_all", queryValue); } else { Attribute attr = entityType.getAttributeByName(queryField); AttributeType dataType = attr.getDataType(); switch (dataType) { 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: queryField = getQueryFieldName(attr) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"); break; case MREF: case XREF: queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.nestedQuery( getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max); break; default: throw new UnexpectedEnumException(dataType); } } return queryBuilder; } QueryClauseFuzzyMatchNgramGenerator(DocumentIdGenerator documentIdGenerator);  }
QueryClauseFuzzyMatchNgramGenerator extends BaseQueryClauseGenerator { QueryBuilder mapQueryRule(QueryRule queryRule, EntityType entityType) { String queryField = queryRule.getField(); Object queryValue = queryRule.getValue(); QueryBuilder queryBuilder; if (queryValue == null) throw new MolgenisQueryException(QUERY_VALUE_CANNOT_BE_NULL_MSG); if (queryField == null) { queryBuilder = QueryBuilders.matchQuery("_all", queryValue); } else { Attribute attr = entityType.getAttributeByName(queryField); AttributeType dataType = attr.getDataType(); switch (dataType) { 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: queryField = getQueryFieldName(attr) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"); break; case MREF: case XREF: queryField = getQueryFieldName(attr) + "." + getQueryFieldName(attr.getRefEntity().getLabelAttribute()) + "." + FIELD_NGRAM; queryBuilder = QueryBuilders.nestedQuery( getQueryFieldName(attr), QueryBuilders.queryStringQuery(queryField + ":(" + queryValue + ")"), ScoreMode.Max); break; default: throw new UnexpectedEnumException(dataType); } } return queryBuilder; } QueryClauseFuzzyMatchNgramGenerator(DocumentIdGenerator documentIdGenerator);   }