3

I need to implement the following in a Spring Data Solr custom repository:

(X OR Y) AND Z

My current code is as follows:

Criteria criteria = new Criteria("x").is(X_VALUE);
criteria = criteria.or(new Criteria("y").is(Y_VALUE);
criteria = criteria.and(new Criteria("z").is(Z_VALUE);

But running this code I get the following precedence:

X OR (Y AND Z)

Any ideas?

checklist
  • 12,340
  • 15
  • 58
  • 102

1 Answers1

7

The current API does not allow this combination of criteria. There's a patch attached to DATASOLR-105 which could help though it does not solve the problem fully.

Usage of SimpleStringCriteria might help for now. Please be aware of the fact, that values have to be converted to a Solr readable format.

new SimpleQuery(new SimpleStringCriteria("(x or y) and z"));

Update

Version 1.2 will have this issue fixed, so that its possible to create the query as follows.

Criteria orPart = Criteria.where("x").is("foo").or("y").is("bar");
Criteria andPart = Criteria.where("z").is("roo");
Query query = new SimpleQuery(orPart.and(andPart));
Christoph Strobl
  • 6,491
  • 25
  • 33
  • Thanks. I indeed ended up building the string Query and then using SimpleStringCriteria to create the criteria from it. Let's hope the patch can solve at a later stage. – checklist Jan 30 '14 at 16:22
  • But even your hint with SimpleQuery results in : x:foo OR y:bar AND z:roo, I am using spring data solr v. 1.3.0.RELEASE – Cipous Oct 20 '14 at 18:11
  • 1
    hmm... you're right there still seems to be an issue. It works for `andPart.and(orPart)` which produces `z:roo AND (x:foo OR y:bar)`. Will check that and let you know once this is resolved. – Christoph Strobl Oct 21 '14 at 05:19
  • The issue will be fixed in 1.4 with an enhanced concept of concatenating `Criteria` using `connect()` so that `(x OR y) AND z` can be created as `where("x").is("foo").or("y").is("bar").connect().and(where("z").is("roo"))` – Christoph Strobl Oct 21 '14 at 10:18
  • Thanks. `new SimpleStringCriteria("(x or y) and z")` solved our problem. – Gaurav May 07 '20 at 22:30