-
-
Notifications
You must be signed in to change notification settings - Fork 124
Grouped Operations
*** This is Legacy documentation *** here's the links to the current docs:
Tip: There are many unit tests in the source code that can be used as Examples of how to do things. There is also a test web project that has plenty of examples of how to configure indexes and search them.
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
criteria.GroupedAnd(new[] { "name" }.ToList(), new[] { "hello" });
outputs:
+(+name:hello)
That one is pretty straight forward
criteria.GroupedAnd(new[] { "name", "nodeTypeAlias" }.ToList(), new[] { "hello" });
outputs:
+(+name:hello +nodeTypeAlias:hello)
Cool, that makes sense
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"
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.
criteria.GroupedAnd(new[] { "name", "nodeTypeAlias" }.ToList(), new[] { "hello", "world", "lucene" });
outputs:
+(+name:hello +name:world +name:lucene +nodeTypeAlias:hello +nodeTypeAlias:world +nodeTypeAlias:lucene)
criteria.GroupedOr(new[] { "id" }.ToList(), new[] { "1" });
outputs:
+(id:1)
That one is pretty straight forward
criteria.GroupedOr(new[] { "id", "parentID" }.ToList(), new[] { "1" });
outputs:
+(id:1 parentID:1)
Cool, that makes sense
criteria.GroupedOr(new[] { "id" }.ToList(), new[] { "1", "2", "3" });
outputs:
+(id:1 id:2 id:3)
Still making sense
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
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 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.
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.
criteria.GroupedNot(new[] { "id", "parentID" }.ToList(), new[] { "1" });
outputs:
(-id:1 -parentID:1)
Cool, that makes sense
criteria.GroupedNot(new[] { "id" }.ToList(), new[] { "1", "2", "3" });
outputs:
(-id:1 -id:2 -id:3)
Cool, that makes sense
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.
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'