use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.
the class CriteriaQueryMappingUnitTests method shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteriaWithDate.
// #1668
@Test
void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteriaWithDate() throws JSONException {
// use POJO properties and types in the query building
CriteriaQuery criteriaQuery = new //
CriteriaQuery(Criteria.or().subCriteria(//
Criteria.where("birthDate").between(LocalDate.of(1989, 11, 9), //
LocalDate.of(1990, 11, 9))).subCriteria(//
Criteria.where("createdDate").is(new Date(383745721653L))));
// mapped field name and converted parameter
String expected = //
"{\n" + //
" \"bool\" : {\n" + //
" \"should\" : [\n" + //
" {\n" + //
" \"bool\" : {\n" + //
" \"must\" : [\n" + //
" {\n" + //
" \"range\" : {\n" + //
" \"birth-date\" : {\n" + //
" \"from\" : \"09.11.1989\",\n" + //
" \"to\" : \"09.11.1990\",\n" + //
" \"include_lower\" : true,\n" + //
" \"include_upper\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" }\n" + //
" }\n" + //
" ],\n" + //
" \"adjust_pure_negative\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" },\n" + //
" {\n" + //
" \"bool\" : {\n" + //
" \"must\" : [\n" + //
" {\n" + //
" \"query_string\" : {\n" + //
" \"query\" : \"383745721653\",\n" + //
" \"fields\" : [\n" + //
" \"created-date^1.0\"\n" + //
" ],\n" + //
" \"type\" : \"best_fields\",\n" + //
" \"default_operator\" : \"and\",\n" + //
" \"max_determinized_states\" : 10000,\n" + //
" \"enable_position_increments\" : true,\n" + //
" \"fuzziness\" : \"AUTO\",\n" + //
" \"fuzzy_prefix_length\" : 0,\n" + //
" \"fuzzy_max_expansions\" : 50,\n" + //
" \"phrase_slop\" : 0,\n" + //
" \"escape\" : false,\n" + //
" \"auto_generate_synonyms_phrase_query\" : true,\n" + //
" \"fuzzy_transpositions\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" }\n" + //
" ],\n" + //
" \"adjust_pure_negative\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" }\n" + //
" ],\n" + //
" \"adjust_pure_negative\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
"}";
mappingElasticsearchConverter.updateQuery(criteriaQuery, Person.class);
String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString();
assertEquals(expected, queryString, false);
}
use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.
the class CriteriaQueryMappingUnitTests method shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteria.
// #1668
@Test
void shouldMapNamesAndConvertValuesInCriteriaQueryForSubCriteria() throws JSONException {
// use POJO properties and types in the query building
CriteriaQuery criteriaQuery = new //
CriteriaQuery(Criteria.or().subCriteria(//
Criteria.where("birthDate").between(LocalDate.of(1989, 11, 9), //
LocalDate.of(1990, 11, 9))).subCriteria(//
Criteria.where("birthDate").is(LocalDate.of(2019, 12, 28))));
// mapped field name and converted parameter
String expected = //
"{\n" + //
" \"bool\" : {\n" + //
" \"should\" : [\n" + //
" {\n" + //
" \"bool\" : {\n" + //
" \"must\" : [\n" + //
" {\n" + //
" \"range\" : {\n" + //
" \"birth-date\" : {\n" + //
" \"from\" : \"09.11.1989\",\n" + //
" \"to\" : \"09.11.1990\",\n" + //
" \"include_lower\" : true,\n" + //
" \"include_upper\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" }\n" + //
" }\n" + //
" ],\n" + //
" \"adjust_pure_negative\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" },\n" + //
" {\n" + //
" \"bool\" : {\n" + //
" \"must\" : [\n" + //
" {\n" + //
" \"query_string\" : {\n" + //
" \"query\" : \"28.12.2019\",\n" + //
" \"fields\" : [\n" + //
" \"birth-date^1.0\"\n" + //
" ],\n" + //
" \"type\" : \"best_fields\",\n" + //
" \"default_operator\" : \"and\",\n" + //
" \"max_determinized_states\" : 10000,\n" + //
" \"enable_position_increments\" : true,\n" + //
" \"fuzziness\" : \"AUTO\",\n" + //
" \"fuzzy_prefix_length\" : 0,\n" + //
" \"fuzzy_max_expansions\" : 50,\n" + //
" \"phrase_slop\" : 0,\n" + //
" \"escape\" : false,\n" + //
" \"auto_generate_synonyms_phrase_query\" : true,\n" + //
" \"fuzzy_transpositions\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" }\n" + //
" ],\n" + //
" \"adjust_pure_negative\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
" }\n" + //
" ],\n" + //
" \"adjust_pure_negative\" : true,\n" + //
" \"boost\" : 1.0\n" + //
" }\n" + //
"}";
mappingElasticsearchConverter.updateQuery(criteriaQuery, Person.class);
String queryString = new CriteriaQueryProcessor().createQuery(criteriaQuery.getCriteria()).toString();
assertEquals(expected, queryString, false);
}
use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchPartQueryTests method getQueryBuilder.
private String getQueryBuilder(String methodName, Class<?>[] parameterClasses, Object[] parameters) throws NoSuchMethodException {
Method method = SampleRepository.class.getMethod(methodName, parameterClasses);
ElasticsearchQueryMethod queryMethod = new ElasticsearchQueryMethod(method, new DefaultRepositoryMetadata(SampleRepository.class), new SpelAwareProxyProjectionFactory(), converter.getMappingContext());
ElasticsearchPartQuery partQuery = new ElasticsearchPartQuery(queryMethod, operations);
CriteriaQuery criteriaQuery = partQuery.createQuery(new ParametersParameterAccessor(queryMethod.getParameters(), parameters));
SearchSourceBuilder source = new RequestFactory(converter).searchRequest(criteriaQuery, Book.class, IndexCoordinates.of("dummy")).source();
return source.toString();
}
use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.
the class CriteriaQueryMappingUnitTests method shouldMapNamesInGeoJsonQuery.
// DATAES-931
@Test
@DisplayName("should map names in GeoJson query")
void shouldMapNamesInGeoJsonQuery() throws JSONException {
GeoJsonPoint geoJsonPoint = GeoJsonPoint.of(1.2, 3.4);
CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("geoShapeField").intersects(geoJsonPoint));
String base64Query = getBase64EncodedGeoShapeQuery(geoJsonPoint, "geo-shape-field", "intersects");
String expected = //
"{\n" + //
" \"wrapper\": {\n" + " \"query\": \"" + base64Query + //
"\"\n" + //
" }\n" + //
"}\n";
mappingElasticsearchConverter.updateQuery(criteriaQuery, GeoShapeEntity.class);
String queryString = new CriteriaFilterProcessor().createFilter(criteriaQuery.getCriteria()).toString();
assertEquals(expected, queryString, false);
}
use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchPartQuery method execute.
@Override
public Object execute(Object[] parameters) {
Class<?> clazz = queryMethod.getResultProcessor().getReturnedType().getDomainType();
ParametersParameterAccessor accessor = new ParametersParameterAccessor(queryMethod.getParameters(), parameters);
CriteriaQuery query = createQuery(accessor);
Assert.notNull(query, "unsupported query");
elasticsearchConverter.updateQuery(query, clazz);
if (queryMethod.hasAnnotatedHighlight()) {
query.setHighlightQuery(queryMethod.getAnnotatedHighlightQuery());
}
IndexCoordinates index = elasticsearchOperations.getIndexCoordinatesFor(clazz);
Object result = null;
if (tree.isLimiting()) {
// noinspection ConstantConditions
query.setMaxResults(tree.getMaxResults());
}
if (tree.isDelete()) {
result = countOrGetDocumentsForDelete(query, accessor);
elasticsearchOperations.delete(query, clazz, index);
elasticsearchOperations.indexOps(index).refresh();
} else if (queryMethod.isPageQuery()) {
query.setPageable(accessor.getPageable());
SearchHits<?> searchHits = elasticsearchOperations.search(query, clazz, index);
if (queryMethod.isSearchPageMethod()) {
result = SearchHitSupport.searchPageFor(searchHits, query.getPageable());
} else {
result = SearchHitSupport.unwrapSearchHits(SearchHitSupport.searchPageFor(searchHits, query.getPageable()));
}
} else if (queryMethod.isStreamQuery()) {
if (accessor.getPageable().isUnpaged()) {
query.setPageable(PageRequest.of(0, DEFAULT_STREAM_BATCH_SIZE));
} else {
query.setPageable(accessor.getPageable());
}
result = StreamUtils.createStreamFromIterator(elasticsearchOperations.searchForStream(query, clazz, index));
} else if (queryMethod.isCollectionQuery()) {
if (accessor.getPageable().isUnpaged()) {
int itemCount = (int) elasticsearchOperations.count(query, clazz, index);
if (itemCount == 0) {
result = new SearchHitsImpl<>(0, TotalHitsRelation.EQUAL_TO, Float.NaN, null, Collections.emptyList(), null, null);
} else {
query.setPageable(PageRequest.of(0, Math.max(1, itemCount)));
}
} else {
query.setPageable(accessor.getPageable());
}
if (result == null) {
result = elasticsearchOperations.search(query, clazz, index);
}
} else if (tree.isCountProjection()) {
result = elasticsearchOperations.count(query, clazz, index);
} else {
result = elasticsearchOperations.searchOne(query, clazz, index);
}
return (queryMethod.isNotSearchHitMethod() && queryMethod.isNotSearchPageMethod()) ? SearchHitSupport.unwrapSearchHits(result) : result;
}
Aggregations