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 (ValueError in Python) with the message “Query of type ‘…’ can not be a child of ‘…’.”. A wildcard query created with the create_wildcard_query method can be used only as a child of a phrase search query; passing it to the search method directly throws ArgumentException (ValueError in Python).
A created query can be examined with the following members of the SearchQuery class:
The child_count property returns the number of child queries.
The get_child method returns a child query by its index; the first_child and second_child properties return the first and the second child queries.
The field_name property returns the name of the field to search in, specified by a field query that contains this query, or None.
The search_options property gets or sets the search options of this query.
The to_string method returns the query in text form.
The following example demonstrates the creation of a nested query and the traversal of the query tree.
importosfromgroupdocs.searchimportIndex,SearchQueryfromgroupdocs.search.optionsimportCommonFieldNames# Creating an index in the specified folderindex=Index("./index")# Indexing documents from the specified folderindex.add("./documents")# Creating a query that searches for the phrase 'theory of * relativity' in the content fieldphrase_query=SearchQuery.create_phrase_search_query(SearchQuery.create_word_query("theory"),SearchQuery.create_word_query("of"),SearchQuery.create_wildcard_query(0,1),SearchQuery.create_word_query("relativity"))field_query=SearchQuery.create_field_query(CommonFieldNames.CONTENT,phrase_query)# Printing the query treeprint(field_query.to_string())foriinrange(phrase_query.child_count):child=phrase_query.get_child(i)print("\t"+child.to_string()+" (field: "+str(child.field_name)+")")# Searching in the indexresult=index.search(field_query)# Printing the documents and the phrases found# (stop words and the words matched by a wildcard are reported as *)print("Documents found:",result.document_count)fordocumentinresult:print(" ",os.path.basename(document.document_info.file_path))forfieldindocument.found_fields:forsequenceinfield.term_sequences:print(" "," ".join(sequence))