The table below shows all combinations of nesting search queries in object form with an indication of their possibility (+) or impossibility (–).
Nested query type
Parent query type
Field
PhraseSearch
Not
Or
And
Word
+
+
+
+
+
WordPattern
+
+
+
+
+
NumericRange
+
+
+
+
+
Regex
+
+
+
+
+
Field
+
–
+
+
+
DateRange
+
+
+
+
+
PhraseSearch
+
+
+
+
+
Not
+
–
+
+
+
Or
+
–
+
+
+
And
+
–
+
+
+
Wildcard
–
+
–
–
–
An attempt to create an impossible combination throws ArgumentException with the message “Query of type ‘…’ can not be a child of ‘…’.”. A wildcard query created with the CreateWildcardQuery method can be used only as a child of a phrase search query; passing it to the Search method directly throws ArgumentException.
A created query can be examined with the following members of the SearchQuery class:
The ChildCount property returns the number of child queries.
The GetChild method returns a child query by its index; the FirstChild and SecondChild properties return the first and the second child queries.
The FieldName property returns the name of the field to search in, specified by a field query that contains this query, or null.
The SearchOptions property gets or sets the search options of this query.
The ToString method returns the query in text form.
The following example demonstrates the creation of a nested query and the traversal of the query tree.
C#
stringindexFolder=@"c:\MyIndex\";stringdocumentsFolder=@"c:\MyDocuments\";// Creating an index in the specified folderIndexindex=newIndex(indexFolder);// Indexing documents from the specified folderindex.Add(documentsFolder);// Creating a query that searches for the phrase 'theory of * relativity' in the content fieldSearchQueryphraseQuery=SearchQuery.CreatePhraseSearchQuery(SearchQuery.CreateWordQuery("theory"),SearchQuery.CreateWordQuery("of"),SearchQuery.CreateWildcardQuery(0,1),SearchQuery.CreateWordQuery("relativity"));SearchQueryfieldQuery=SearchQuery.CreateFieldQuery(CommonFieldNames.Content,phraseQuery);// Printing the query treeConsole.WriteLine(fieldQuery.ToString());for(inti=0;i<phraseQuery.ChildCount;i++){SearchQuerychild=phraseQuery.GetChild(i);Console.WriteLine("\t"+child.ToString()+" (field: "+child.FieldName+")");}// Searching in the indexSearchResultresult=index.Search(fieldQuery);
More resources
GitHub examples
You may easily run the code from documentation articles and see the features in action in our GitHub examples: