Nesting search queries in object form

The table below shows all combinations of nesting search queries in object form with an indication of their possibility (+) or impossibility (–).

Nested query typeParent query type
FieldPhraseSearchNotOrAnd
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.

The CreatePhraseSearchQuery, CreateAndQuery and CreateOrQuery methods take an array of child queries (params), which must contain at least two queries. The CreateAndQuery and CreateOrQuery methods also have overloads with two child queries.

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#

string indexFolder = @"c:\MyIndex\";
string documentsFolder = @"c:\MyDocuments\";

// Creating an index in the specified folder
Index index = new Index(indexFolder);

// Indexing documents from the specified folder
index.Add(documentsFolder);

// Creating a query that searches for the phrase 'theory of * relativity' in the content field
SearchQuery phraseQuery = SearchQuery.CreatePhraseSearchQuery(
    SearchQuery.CreateWordQuery("theory"),
    SearchQuery.CreateWordQuery("of"),
    SearchQuery.CreateWildcardQuery(0, 1),
    SearchQuery.CreateWordQuery("relativity"));
SearchQuery fieldQuery = SearchQuery.CreateFieldQuery(CommonFieldNames.Content, phraseQuery);

// Printing the query tree
Console.WriteLine(fieldQuery.ToString());
for (int i = 0; i < phraseQuery.ChildCount; i++)
{
    SearchQuery child = phraseQuery.GetChild(i);
    Console.WriteLine("\t" + child.ToString() + " (field: " + child.FieldName + ")");
}

// Searching in the index
SearchResult result = 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:

Free online document search App

Along with full featured .NET library we provide simple, but powerful free Apps.

You are welcome to search over your PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX and more with our free online Free Online Document Search App.

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.