Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CriteriaQuery must support nested queries #1753

Closed
sothawo opened this issue Apr 1, 2021 · 0 comments · Fixed by #1757
Closed

CriteriaQuery must support nested queries #1753

sothawo opened this issue Apr 1, 2021 · 0 comments · Fixed by #1757
Assignees
Labels
type: bug A general bug

Comments

@sothawo
Copy link
Collaborator

sothawo commented Apr 1, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant