Search in sources :

Example 1 with Criteria

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

the class CriteriaQueryProcessorUnitTests method shouldProcessTwoCriteriaWithOr.

// DATAES-706
@Test
void shouldProcessTwoCriteriaWithOr() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\": {\n" + // 
    "    \"should\": [\n" + // 
    "      {\n" + // 
    "        \"query_string\": {\n" + // 
    "          \"query\": \"value1\",\n" + // 
    "          \"fields\": [\n" + // 
    "            \"field1^1.0\"\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      },\n" + // 
    "      {\n" + // 
    "        \"query_string\": {\n" + // 
    "          \"query\": \"value2\",\n" + // 
    "          \"fields\": [\n" + // 
    "            \"field2^1.0\"\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}";
    Criteria criteria = new Criteria("field1").is("value1").or("field2").is("value2");
    String query = queryProcessor.createQuery(criteria).toString();
    assertEquals(expected, query, false);
}
Also used : Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 2 with Criteria

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

the class CriteriaQueryProcessorUnitTests method shouldBuildMatchAllQuery.

// DATAES-706
@Test
void shouldBuildMatchAllQuery() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\" : {\n" + // 
    "    \"must\" : [\n" + // 
    "      {\n" + // 
    "        \"match\" : {\n" + // 
    "          \"field1\" : {\n" + // 
    "            \"query\" : \"value1 value2\",\n" + // 
    "            \"operator\" : \"AND\"\n" + // 
    "          }\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}\n";
    Criteria criteria = new Criteria("field1").matchesAll("value1 value2");
    String query = queryProcessor.createQuery(criteria).toString();
    assertEquals(expected, query, false);
}
Also used : Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 3 with Criteria

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

the class CriteriaQueryProcessorUnitTests method shouldProcessMixedCriteriaWithOrAnd.

// DATAES-706
@Test
void shouldProcessMixedCriteriaWithOrAnd() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\": {\n" + // 
    "    \"must\": [\n" + // 
    "      {\n" + // 
    "        \"query_string\": {\n" + // 
    "          \"query\": \"value1\",\n" + // 
    "          \"fields\": [\n" + // 
    "            \"field1^1.0\"\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      },\n" + // 
    "      {\n" + // 
    "        \"query_string\": {\n" + // 
    "          \"query\": \"value3\",\n" + // 
    "          \"fields\": [\n" + // 
    "            \"field3^1.0\"\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ],\n" + // 
    "    \"should\": [\n" + // 
    "      {\n" + // 
    "        \"query_string\": {\n" + // 
    "          \"query\": \"value2\",\n" + // 
    "          \"fields\": [\n" + // 
    "            \"field2^1.0\"\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      },\n" + // 
    "      {\n" + // 
    "        \"query_string\": {\n" + // 
    "          \"query\": \"value4\",\n" + // 
    "          \"fields\": [\n" + // 
    "            \"field4^1.0\"\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}\n";
    Criteria criteria = // 
    new Criteria("field1").is("value1").or("field2").is(// 
    "value2").and("field3").is(// 
    "value3").or("field4").is(// 
    "value4");
    String query = queryProcessor.createQuery(criteria).toString();
    assertEquals(expected, query, false);
}
Also used : Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Example 4 with Criteria

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

the class CriteriaQueryProcessorUnitTests method shouldBuildQueryForEmptyProperty.

// #1909
@Test
@DisplayName("should build query for empty property")
void shouldBuildQueryForEmptyProperty() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\" : {\n" + // 
    "    \"must\" : [\n" + // 
    "      {\n" + // 
    "        \"bool\" : {\n" + // 
    "          \"must\" : [\n" + // 
    "            {\n" + // 
    "              \"exists\" : {\n" + // 
    "                \"field\" : \"lastName\"" + // 
    "              }\n" + // 
    "            }\n" + // 
    "          ],\n" + // 
    "          \"must_not\" : [\n" + // 
    "            {\n" + // 
    "              \"wildcard\" : {\n" + // 
    "                \"lastName\" : {\n" + // 
    "                  \"wildcard\" : \"*\"" + // 
    "                }\n" + // 
    "              }\n" + // 
    "            }\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}";
    Criteria criteria = new Criteria("lastName").empty();
    String query = queryProcessor.createQuery(criteria).toString();
    assertEquals(expected, query, false);
}
Also used : Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with Criteria

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

the class CriteriaQueryProcessorUnitTests method shouldProcessNestedSubCriteria.

// DATAES-706
@Test
void shouldProcessNestedSubCriteria() throws JSONException {
    String expected = // 
    "{\n" + // 
    "  \"bool\": {\n" + // 
    "    \"should\": [\n" + // 
    "      {\n" + // 
    "        \"bool\": {\n" + // 
    "          \"must\": [\n" + // 
    "            {\n" + // 
    "              \"query_string\": {\n" + // 
    "                \"query\": \"Miller\",\n" + // 
    "                \"fields\": [\n" + // 
    "                  \"lastName^1.0\"\n" + // 
    "                ]\n" + // 
    "              }\n" + // 
    "            },\n" + // 
    "            {\n" + // 
    "              \"bool\": {\n" + // 
    "                \"should\": [\n" + // 
    "                  {\n" + // 
    "                    \"query_string\": {\n" + // 
    "                      \"query\": \"Jack\",\n" + // 
    "                      \"fields\": [\n" + // 
    "                        \"firstName^1.0\"\n" + // 
    "                      ]\n" + // 
    "                    }\n" + // 
    "                  },\n" + // 
    "                  {\n" + // 
    "                    \"query_string\": {\n" + // 
    "                      \"query\": \"John\",\n" + // 
    "                      \"fields\": [\n" + // 
    "                        \"firstName^1.0\"\n" + // 
    "                      ]\n" + // 
    "                    }\n" + // 
    "                  }\n" + // 
    "                ]\n" + // 
    "              }\n" + // 
    "            }\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      },\n" + // 
    "      {\n" + // 
    "        \"bool\": {\n" + // 
    "          \"must\": [\n" + // 
    "            {\n" + // 
    "              \"query_string\": {\n" + // 
    "                \"query\": \"Smith\",\n" + // 
    "                \"fields\": [\n" + // 
    "                  \"lastName^1.0\"\n" + // 
    "                ]\n" + // 
    "              }\n" + // 
    "            },\n" + // 
    "            {\n" + // 
    "              \"bool\": {\n" + // 
    "                \"should\": [\n" + // 
    "                  {\n" + // 
    "                    \"query_string\": {\n" + // 
    "                      \"query\": \"Emma\",\n" + // 
    "                      \"fields\": [\n" + // 
    "                        \"firstName^1.0\"\n" + // 
    "                      ]\n" + // 
    "                    }\n" + // 
    "                  },\n" + // 
    "                  {\n" + // 
    "                    \"query_string\": {\n" + // 
    "                      \"query\": \"Lucy\",\n" + // 
    "                      \"fields\": [\n" + // 
    "                        \"firstName^1.0\"\n" + // 
    "                      ]\n" + // 
    "                    }\n" + // 
    "                  }\n" + // 
    "                ]\n" + // 
    "              }\n" + // 
    "            }\n" + // 
    "          ]\n" + // 
    "        }\n" + // 
    "      }\n" + // 
    "    ]\n" + // 
    "  }\n" + // 
    "}";
    Criteria criteria = Criteria.or().subCriteria(new Criteria("lastName").is("Miller").subCriteria(new Criteria().or("firstName").is("John").or("firstName").is("Jack"))).subCriteria(new Criteria("lastName").is("Smith").subCriteria(new Criteria().or("firstName").is("Emma").or("firstName").is("Lucy")));
    String query = queryProcessor.createQuery(criteria).toString();
    assertEquals(expected, query, false);
}
Also used : Criteria(org.springframework.data.elasticsearch.core.query.Criteria) Test(org.junit.jupiter.api.Test)

Aggregations

Criteria (org.springframework.data.elasticsearch.core.query.Criteria)44 Test (org.junit.jupiter.api.Test)36 CriteriaQuery (org.springframework.data.elasticsearch.core.query.CriteriaQuery)30 SpringIntegrationTest (org.springframework.data.elasticsearch.junit.jupiter.SpringIntegrationTest)16 DisplayName (org.junit.jupiter.api.DisplayName)12 Query (org.springframework.data.elasticsearch.core.query.Query)7 SearchRequest (org.elasticsearch.action.search.SearchRequest)4 MemberDocument (com.example.elasticsearch.member.domain.MemberDocument)3 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 Nullable (org.springframework.lang.Nullable)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 StringQuery (org.springframework.data.elasticsearch.core.query.StringQuery)2 LinkedList (java.util.LinkedList)1 GeoBoundingBoxQueryBuilder (org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder)1 GeoDistanceQueryBuilder (org.elasticsearch.index.query.GeoDistanceQueryBuilder)1 SearchHits (org.springframework.data.elasticsearch.core.SearchHits)1 SearchPage (org.springframework.data.elasticsearch.core.SearchPage)1 GeoJsonPoint (org.springframework.data.elasticsearch.core.geo.GeoJsonPoint)1