Skip to content

Grouped Operations

Shannon Deminick edited this page Mar 5, 2019 · 6 revisions

There's 3 'Grouped' methods: GroupedAnd, GroupedOr, GroupedNot. Each of these methods produces slightly different output due to the way boolean logic works.

Each of these methods accepts 2 collections: IEnumerable<string> fields, IEnumerable<string> vals

GroupedAnd

Single field + single val, example:

criteria.GroupedAnd(new[] { "name" }.ToList(), new[] { "hello" });

outputs:

+(+name:hello)

That one is pretty straight forward

Multi field + single val, example:

criteria.GroupedAnd(new[] { "name", "nodeTypeAlias" }.ToList(), new[] { "hello" });

outputs:

+(+name:hello +nodeTypeAlias:hello)

Cool, that makes sense

Single field + multiple vals, example:

criteria.GroupedAnd(new[] { "name" }.ToList(), new[] { "hello", "world", "lucene" });

outputs:

+(+name:hello +name:world +name:lucene)

You might wonder how name can equal all of those at once? It's actually not going to match the exact word/phrase in that field but this is going to match a name that matches all three of these words and this would depend on the Analyzer you are using. For example if using Whitespace analyzer, this would match a name of "hello world earth, welcome to lucene"

Multi field + equivalent number of vals, example:

criteria.GroupedAnd(new[] { "name", "nodeTypeAlias" }.ToList(), new[] { "hello", "world" });

outputs:

+(+name:hello +nodeTypeAlias:world)

Since the arrays match length, it will correspond the name/value pairs.

Multi field + different number of vals, example:

criteria.GroupedAnd(new[] { "name", "nodeTypeAlias" }.ToList(), new[] { "hello", "world", "lucene" });

outputs:

+(+name:hello +name:world +name:lucene +nodeTypeAlias:hello +nodeTypeAlias:world +nodeTypeAlias:lucene)

GroupedOr

Single field + single val, example:

criteria.GroupedOr(new[] { "id" }.ToList(), new[] { "1" });

outputs:

+(id:1)

That one is pretty straight forward

Multi field + single val, example:

criteria.GroupedOr(new[] { "id", "parentID" }.ToList(), new[] { "1" });

outputs:

+(id:1 parentID:1)

Cool, that makes sense

Single field + multiple vals, example:

criteria.GroupedOr(new[] { "id" }.ToList(), new[] { "1", "2", "3" });

outputs:

+(id:1 id:2 id:3)

Still making sense

Multi field + equivalent number of vals, example:

criteria.GroupedOr(new[] { "id", "parentID" }.ToList(), new[] { "1", "2" });

outputs:

+(id:1 id:2 parentID:1 parentID:2)

Unlike AND, this is matching all combinations whereas AND matches the key/value pairs if the array lengths are the same

Multi field + different number of vals, example:

criteria.GroupedOr(new[] { "id", "parentID" }.ToList(), new[] { "1", "2", "3" });

outputs:

+(id:1 id:2 id:3 parentID:1 parentID:2 parentID:3)

Right, just like the previous one, makes sense

GroupedNot

GroupedNot is an interesting one and it's very important to know that you cannot have a 'Not' query without a previous query. For example, you cannot just do:

-id:1

Since that would force Lucene to lookup every single record and then reduce it (you can force this but it's not supported in the fluent api). You need a query like:

+name:hello -name:world

which would match all names with hello and then reduce them to ensure there's not a 'world' in the name.

Single field + single val, example:

criteria.GroupedNot(new[] { "id" }.ToList(), new[] { "1" });

outputs:

(-id:1)

That one is pretty straight forward but note the lack of the "+" before the braces which was there in both the GroupedAnd and the GroupedOr. That's because it doesn't really make sense, a 'Not' is more of a filter.

Multi field + single val, example:

criteria.GroupedNot(new[] { "id", "parentID" }.ToList(), new[] { "1" });

outputs:

(-id:1 -parentID:1)

Cool, that makes sense

Single field + multiple vals, example:

criteria.GroupedNot(new[] { "id" }.ToList(), new[] { "1", "2", "3" });

outputs:

(-id:1 -id:2 -id:3)

Cool, that makes sense

Multi field + equivalent number of vals, example:

criteria.GroupedNot(new[] { "id", "parentID" }.ToList(), new[] { "1", "2" });

outputs:

(-id:1 -id:2 -parentID:1 -parentID:2)

Very similar to the 'Or', it is just filtering out anything that has id or parentID equal to 1 or 2.

Multi field + different number of vals, example:

criteria.GroupedNot(new[] { "id", "parentID" }.ToList(), new[] { "1", "2", "3" });

outputs:

(-id:1 -id:2 -id:3 -parentID:1 -parentID:2 -parentID:3)

Yup, basically the same as 'Or'

You can’t perform that action at this time.