Search in sources :

Example 26 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class CriteriaQueryMappingUnitTests method shouldMapNamesAndValueInNestedEntitiesWithSubfields.

// #1753
@Test
@DisplayName("should map names and value in nested entities with sub-fields")
void shouldMapNamesAndValueInNestedEntitiesWithSubfields() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\": {\n" + // 
    "    \"must\": [\n" + // 
    "      {\n" + // 
    "        \"nested\": {\n" + // 
    "          \"query\": {\n" + // 
    "            \"query_string\": {\n" + // 
    "              \"query\": \"Foobar\",\n" + // 
    "              \"fields\": [\n" + // 
    "                \"per-sons.nick-name.keyword^1.0\"\n" + // 
    "              ]\n" + // 
    "            }\n" + // 
    "          },\n" + // 
    "          \"path\": \"per-sons\"\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}\n";
    CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("persons.nickName.keyword").is("Foobar"));
    mappingElasticsearchConverter.updateQuery(criteriaQuery, House.class);
    String queryString = new CriteriaQueryProcessor().createQuery(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) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 27 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class CriteriaQueryMappingUnitTests method shouldMapNamesAndConvertValuesInCriteriaQuery.

// endregion
// region tests
// DATAES-716
@Test
void shouldMapNamesAndConvertValuesInCriteriaQuery() throws JSONException {
    // use POJO properties and types in the query building
    CriteriaQuery criteriaQuery = new // 
    CriteriaQuery(// 
    new Criteria("birthDate").between(LocalDate.of(1989, 11, 9), // 
    LocalDate.of(1990, 11, 9)).or("birthDate").is(// 
    LocalDate.of(2019, 12, 28)));
    // mapped field name and converted parameter
    String expected = // 
    '{' + // 
    "  \"bool\" : {" + // 
    "    \"should\" : [" + // 
    "      {" + // 
    "        \"range\" : {" + // 
    "          \"birth-date\" : {" + // 
    "            \"from\" : \"09.11.1989\"," + // 
    "            \"to\" : \"09.11.1990\"," + // 
    "            \"include_lower\" : true," + // 
    "            \"include_upper\" : true" + // 
    "          }" + // 
    "        }" + // 
    "      }," + // 
    "      {" + // 
    "        \"query_string\" : {" + // 
    "          \"query\" : \"28.12.2019\"," + // 
    "          \"fields\" : [" + // 
    "            \"birth-date^1.0\"" + // 
    "          ]" + // 
    "        }" + // 
    "      }" + // 
    "    ]" + // 
    "  }" + // 
    '}';
    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) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 28 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class CriteriaQueryMappingUnitTests method shouldMapNamesAndValuesInSubCriteriaQuery.

// DATAES-706
@Test
void shouldMapNamesAndValuesInSubCriteriaQuery() throws JSONException {
    CriteriaQuery criteriaQuery = new // 
    CriteriaQuery(// 
    new Criteria("firstName").matches("John").subCriteria(// 
    new Criteria("birthDate").between(LocalDate.of(1989, 11, 9), // 
    LocalDate.of(1990, 11, 9)).or("birthDate").is(LocalDate.of(2019, 12, 28))));
    String expected = // 
    "{\n" + // 
    "  \"bool\": {\n" + // 
    "    \"must\": [\n" + // 
    "      {\n" + // 
    "        \"match\": {\n" + // 
    "          \"first-name\": {\n" + // 
    "            \"query\": \"John\"\n" + // 
    "          }\n" + // 
    "        }\n" + // 
    "      },\n" + // 
    "      {\n" + // 
    "        \"bool\": {\n" + // 
    "          \"should\": [\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" + // 
    "                }\n" + // 
    "              }\n" + // 
    "            },\n" + // 
    "            {\n" + // 
    "              \"query_string\": {\n" + // 
    "                \"query\": \"28.12.2019\",\n" + // 
    "                \"fields\": [\n" + // 
    "                  \"birth-date^1.0\"\n" + // 
    "                ]\n" + // 
    "              }\n" + // 
    "            }\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\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) Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 29 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class CriteriaQueryMappingUnitTests method shouldMapNamesAndValueInNestedEntities.

// #1753
@Test
@DisplayName("should map names and value in nested entities")
void shouldMapNamesAndValueInNestedEntities() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\": {\n" + // 
    "    \"must\": [\n" + // 
    "      {\n" + // 
    "        \"nested\": {\n" + // 
    "          \"query\": {\n" + // 
    "            \"query_string\": {\n" + // 
    "              \"query\": \"03.10.1999\",\n" + // 
    "              \"fields\": [\n" + // 
    "                \"per-sons.birth-date^1.0\"\n" + // 
    "              ]\n" + // 
    "            }\n" + // 
    "          },\n" + // 
    "          \"path\": \"per-sons\"\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}\n";
    CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("persons.birthDate").is(LocalDate.of(1999, 10, 3)));
    mappingElasticsearchConverter.updateQuery(criteriaQuery, House.class);
    String queryString = new CriteriaQueryProcessor().createQuery(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) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 30 with CriteriaQuery

use of org.springframework.data.elasticsearch.core.query.CriteriaQuery in project spring-data-elasticsearch by spring-projects.

the class CriteriaQueryMappingUnitTests method shouldMapNamesAndValueInObjectEntities.

// #1761
@Test
@DisplayName("should map names and value in object entities")
void shouldMapNamesAndValueInObjectEntities() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\": {\n" + // 
    "    \"must\": [\n" + // 
    "      {\n" + // 
    "         \"query_string\": {\n" + // 
    "              \"query\": \"03.10.1999\",\n" + // 
    "              \"fields\": [\n" + // 
    "                \"per-sons.birth-date^1.0\"\n" + // 
    "              ]\n" + // 
    "            }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}\n";
    CriteriaQuery criteriaQuery = new CriteriaQuery(new Criteria("persons.birthDate").is(LocalDate.of(1999, 10, 3)));
    mappingElasticsearchConverter.updateQuery(criteriaQuery, ObjectWithPerson.class);
    String queryString = new CriteriaQueryProcessor().createQuery(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) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

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