Greg
Greg

Reputation: 1387

How to use Example Matcher with nested fields not fully filled

I've got the following structure:

@Document
class A {
String value1;
B b;
}

class B {
String value1;
String value2;
Integer value3;
}

Then I save a document with the following values:

{
value1: "val",
b: {
value1: "val1",
value2: "val2"
value3: 156
}
}

When I create an example with both values in B filled I get a match, when I fill only A.value1 and B.value1 I don't get a match. How can I make the match ingore null values in nested fields?

Search:

repository.findAll(Example.of(new A("val", new B("val1", null, null)))

Doesn't match because some of the B fields are not filled.

Search:

repository.findAll(Example.of(new A("val", new B("val1", "val2", 156)))

Matches as all the fields are filled.

Upvotes: 0

Views: 1439

Answers (1)

pirho
pirho

Reputation: 12285

This should work by definining ExampleMatcher of your own:

ExampleMatcher exampleMatcher = ExampleMatcher.matchingAll();

Then query would be like:

repository.findAll(
    Example.of(new A("val", new B("val1", null, null)), exampleMatcher));

See API docs:

Create a new ExampleMatcher including all non-null properties by default matching all predicates derived from the example.

Upvotes: 1

Related Questions