Querying

  •  A query is made by building up the structure defining the criteria values in the Simples that are a part of the defined structure.
  • Simples that are left empty will be populated by the matching results of the executed query.
  • Multiple query values can be provided in each Simple object. 
  • The comparison operator is by default Equals but the user can also specify inequalities for each query value.


Default comparer

Consider a Nest structure according to the picture.

Then create a query to find film titles together with its year directed by Christopher Nolan:

var qTitleSimple = new Simple<string>();                                          // leave empty for querying all matching values

var qTitle = new Part("Title").Add(qTitleSimple);

var qName2 = new Part("Name").Add(new Simple<string>("Christopher Nolan"));     // The Simple to be matched

var qDirector2 = new Part("Director").Add(qName2);

var qYearSimple = new Simple<int>();                                                // leave empty for querying all matching values

var qYear = new Part("Year").Add(qYearSimple);

var qFilm = new Part("Film").Add(qTitle).Add(qDirector2).Add(qYear);

nest.Query(qFilm);

       

var yearResults = qYearSimple.GetMatches().ToArray();

var titleResults = qTitleSimple.GetMatches().ToArray();


Console.WriteLine("Query result:");

for (int i = 0; i < yearResults.Length; i++)

{

   Console.WriteLine( $"Title: {titleResults[i]} Year: {yearResults[i]}");

}


//------------------------

//  Standard Output: 

//    Query result:

//    Title: Inception, Year: 2010

//    Title: Memento, Year: 2001

Add children to queried structure

It is possible to add new, or queried, children to another queried structure. The only requirement is that the part of the structure where the child is to be added is uniquely determined. That is if the parent, or any other child of the parent, has the state QueriedUniqueHit or Owned.


For example, the birth year can be added as a child to the Director, even though it has the state QueriedMultipleHits from the query above. Here this is possible since its intended sibling, "Name", has the state QueriedUniqueHit. I.e. all hits to the Director query (2 of them) refers to the same "Name" instance.

var qYearOfBirth = new Part("YearOfBirth").Add(new Simple<int>().AddValue(1970));

qDirector2.Add(qYearOfBirth);

nest.Store(qDirector2);