Search in sources :

Example 1 with CriteriaQuery

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);
}
Also used : CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Date(java.util.Date) LocalDate(java.time.LocalDate) Test(org.junit.jupiter.api.Test)

Example 2 with CriteriaQuery

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);
}
Also used : CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Test(org.junit.jupiter.api.Test)

Example 3 with CriteriaQuery

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();
}
Also used : SpelAwareProxyProjectionFactory(org.springframework.data.projection.SpelAwareProxyProjectionFactory) ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) ElasticsearchQueryMethod(org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) DefaultRepositoryMetadata(org.springframework.data.repository.core.support.DefaultRepositoryMetadata) ElasticsearchPartQuery(org.springframework.data.elasticsearch.repository.query.ElasticsearchPartQuery) ElasticsearchQueryMethod(org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod) Method(java.lang.reflect.Method) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 4 with CriteriaQuery

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);
}
Also used : CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) GeoJsonPoint(org.springframework.data.elasticsearch.core.geo.GeoJsonPoint) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with CriteriaQuery

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;
}
Also used : ParametersParameterAccessor(org.springframework.data.repository.query.ParametersParameterAccessor) CriteriaQuery(org.springframework.data.elasticsearch.core.query.CriteriaQuery) SearchHitsImpl(org.springframework.data.elasticsearch.core.SearchHitsImpl) SearchHits(org.springframework.data.elasticsearch.core.SearchHits) IndexCoordinates(org.springframework.data.elasticsearch.core.mapping.IndexCoordinates)

Aggregations

CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)38 Criteria (org.springframework.data.elasticsearch.core.query.Criteria)30 Test (org.junit.jupiter.api.Test)29 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)16 DisplayName (org.junit.jupiter.api.DisplayName)10 Query (org.springframework.data.elasticsearch.core.query.Query)8 MemberDocument (com.example.elasticsearch.member.domain.MemberDocument)4 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 SearchHits (org.springframework.data.elasticsearch.core.SearchHits)2 StringQuery (org.springframework.data.elasticsearch.core.query.StringQuery)2 ParametersParameterAccessor (org.springframework.data.repository.query.ParametersParameterAccessor)2 Method (java.lang.reflect.Method)1 LocalDate (java.time.LocalDate)1 Date (java.util.Date)1 List (java.util.List)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 SearchHitsImpl (org.springframework.data.elasticsearch.core.SearchHitsImpl)1 SearchPage (org.springframework.data.elasticsearch.core.SearchPage)1 GeoJsonPoint (org.springframework.data.elasticsearch.core.geo.GeoJsonPoint)1 GeoPoint (org.springframework.data.elasticsearch.core.geo.GeoPoint)1