Skip to content

Instantly share code, notes, and snippets.

@Yegoroff
Created April 10, 2014 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Yegoroff/10422763 to your computer and use it in GitHub Desktop.
Save Yegoroff/10422763 to your computer and use it in GitHub Desktop.
PlainElastic.Net Parent-Child Sample
using System;
using PlainElastic.Net;
using PlainElastic.Net.Mappings;
using PlainElastic.Net.Queries;
using PlainElastic.Net.Serialization;
using PlainElastic.Net.Utils;
namespace ElasticParentChildSample
{
class Program
{
static void Main(string[] args)
{
var parent1 = new Parent {Id = "1", Title = "First"};
var parent2 = new Parent {Id = "2", Title = "Second"};
var p1Child1 = new Child {Id = "1", ParentId = "1", Text = "One"};
var p1Child2 = new Child {Id = "2", ParentId = "1", Text = "Two"};
var p2Child1 = new Child { Id = "3", ParentId = "2", Text = "Three" };
var connection = new ElasticConnection("localhost", 9200);
var serializer = new JsonNetSerializer();
// drop/create test index
try
{
connection.Delete(Commands.Index(index: "test"));
}
// swallow case when no index found
catch (OperationException) {}
connection.Post(Commands.Index(index: "test"));
// put parent and child mapping
var jsonParentMapping = GetParentMapping();
connection.Put(new PutMappingCommand("test", "parent"), jsonParentMapping);
var jsonChildMapping = GetChildMapping();
connection.Put(new PutMappingCommand("test", "child"), jsonChildMapping);
// Add parent documents
connection.Put(Commands.Index(index: "test", type: "parent", id: parent1.Id), serializer.Serialize(parent1));
connection.Put(Commands.Index(index: "test", type: "parent", id: parent2.Id), serializer.Serialize(parent2));
// Add child documents
connection.Put(Commands.Index(index: "test", type: "child", id: p1Child1.Id).Parent(p1Child1.ParentId), serializer.Serialize(p1Child1));
connection.Put(Commands.Index(index: "test", type: "child", id: p1Child2.Id).Parent(p1Child2.ParentId), serializer.Serialize(p1Child2));
connection.Put(Commands.Index(index: "test", type: "child", id: p2Child1.Id).Parent(p2Child1.ParentId), serializer.Serialize(p2Child1));
// wait till documents indexed
connection.Post(Commands.Refresh());
// Query child documents with parent title equal to "first"
var childJsonQuery = new QueryBuilder<Child>()
.Query(q => q
.HasParent<Parent>(hp => hp
.ParentType("parent")
.Query(qry => qry
.Term(t=>t.Field(parent => parent.Title).Value("first"))
)
)
)
.BuildBeautified();
Console.WriteLine("Child query:");
Console.WriteLine(childJsonQuery);
Console.WriteLine();
string result = connection.Post(Commands.Search(index: "test", type: "child"), childJsonQuery);
Console.WriteLine("Child query results JSON:");
Console.WriteLine(result.BeautifyJson());
Console.WriteLine();
var foundChildren = serializer.ToSearchResult<Child>(result).Documents;
Console.WriteLine("Found Child documents:");
foreach (var child in foundChildren)
{
Console.WriteLine(child.Id + " " + child.ParentId + " " + child.Text);
}
Console.WriteLine();
// Query parent documents with child text equal to "one"
var parentJsonQuery = new QueryBuilder<Parent>()
.Filter(f => f
.HasChild<Child>(hc => hc
.Type("child")
.Filter(fl => fl
.Term(t => t.Field(child => child.Text).Value("one"))
)
)
)
.BuildBeautified();
Console.WriteLine("Parent query:");
Console.WriteLine(parentJsonQuery);
Console.WriteLine();
string parentResult = connection.Post(Commands.Search(index: "test", type: "parent"), parentJsonQuery);
Console.WriteLine("Parent query results JSON:");
Console.WriteLine(parentResult.BeautifyJson());
Console.WriteLine();
var foundParents = serializer.ToSearchResult<Parent>(parentResult).Documents;
Console.WriteLine("Found Parent documents:");
foreach (var parent in foundParents)
{
Console.WriteLine(parent.Id + " " + parent.Title);
}
Console.ReadKey();
}
static string GetChildMapping()
{
return new MapBuilder<Child>().
RootObject("child", r => r
// Here we define parent relation, see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping-parent-field.html
.Parent(parentMap: map => map
.Type("parent")
)
.Properties(properties => properties
.String(child => child.Id, opt => opt.Analyzer(DefaultAnalyzers.keyword))
.String(child => child.ParentId, opt => opt.Analyzer(DefaultAnalyzers.keyword))
.String(child => child.Text)
)
).Build();
}
static string GetParentMapping()
{
return new MapBuilder<Parent>().
RootObject("parent", r => r
.Properties(properties => properties
.String(parent => parent.Id, opt => opt.Analyzer(DefaultAnalyzers.keyword))
.String(parent=> parent.Title)
)
).Build();
}
}
class Parent
{
public string Id { get; set; }
public string Title { get; set; }
}
class Child
{
public string Id { get; set; }
public string ParentId { get; set; }
public string Text { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment