Skip to content

CriteriaQuery must support nested queries #1753

Closed
@sothawo

Description

@sothawo

When we have a nested object structure like the following (getter and setter omitted):

@Document(indexName = "cities")
class City {
	@Id
	private String name;
	@Field(type = FieldType.Nested)
	private Collection<House> houses = new ArrayList<>();
}

class House {
	@Field(type = FieldType.Text)
	private String street;
	@Field(type = FieldType.Text)
	private String streetNumber;
	@Field(type = FieldType.Nested)
	private List<Inhabitant> inhabitants = new ArrayList<>();
}

class Inhabitant {
	@Field(type = FieldType.Text)
	private String firstName;
	@Field(type = FieldType.Text)
	private String lastName;
}

CriteriaQuery should be able to search for cities, houses and persons like this:

CriteriaQuery cityQuery = new CriteriaQuery(new Criteria("name").is("london"))
CriteriaQuery houseQuery = new CriteriaQuery(new Criteria("houses.street").is("baker"))
CriteriaQuery personQuery = new CriteriaQuery(new Criteria("houses.inhabitants.lastName").is("holmes"))

This currently works for the first case, but not the others. The created queries that are sent to elasticsearch are:

{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "london",
            "fields": [
              "name"
            ]
          }
        }
      ]
    }
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "baker",
            "fields": [
              "houses.street"
            ]
          }
        }
      ]
    }
  }
}
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "holmes",
            "fields": [
              "houses.inhabitants.lastName"
            ]
          }
        }
      ]
    }
  }
}

For the second and third case a nested query specifying the nested path must be constructed:

{
  "query": {
    "nested": {
      "path": "houses",
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "baker",
                "fields": [
                  "houses.street"
                ]
              }
            }
          ]
        }
      }
    }
  }
}
{
  "query": {
    "nested": {
      "path": "houses.inhabitants",
      "query": {
        "bool": {
          "must": [
            {
              "query_string": {
                "query": "barone",
                "fields": [
                  "houses.inhabitants.lastName"
                ]
              }
            }
          ]
        }
      }
    }
  }
}

CriteriaQueryProcessor must be adapted to provide these nested queries.

Also see #1752 and https://stackoverflow.com/questions/66637914/how-to-construct-nested-query-using-criteria-and-criteriaquery-in-spring-data-el for the same issue

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions