public void TestTopChildrenQuery()
{
var index = "index_test_parent_child_type123_with_top_child_query_query";
// client.DeleteIndex(index);
#region preparing mapping
var parentType = new TypeSetting("blog");
parentType.AddStringField("title");
client.CreateIndex(index);
var op = client.PutMapping(index, parentType);
Assert.AreEqual(true, op.Acknowledged);
var childType = new TypeSetting("comment", parentType);
childType.AddStringField("comments");
childType.AddStringField("author").Analyzer = "keyword";
op = client.PutMapping(index, childType);
Assert.AreEqual(true, op.Acknowledged);
var mapping = client.GetMapping(index, "comment");
Assert.True(mapping.IndexOf("_parent") > 0);
client.Refresh();
#endregion
#region preparing test data
Dictionary<string, object> dict = new Dictionary<string, object>();
dict["title"] = "this is the blog title";
client.Index(index, "blog", "1", dict);
dict = new Dictionary<string, object>();
dict["title"] = "hello.elasticsearch";
client.Index(index, "blog", "2", dict);
//child docs
dict = new Dictionary<string, object>();
dict["title"] = "awful,that's bullshit";
dict["author"] = "lol";
client.Index(index, "comment", "c1", dict, "1");
dict = new Dictionary<string, object>();
dict["title"] = "hey,lol,i can't agree more!";
dict["author"] = "laday-guagua";
client.Index(index, "comment", "c2", dict, "1");
dict = new Dictionary<string, object>();
dict["title"] = "it rocks";
dict["author"] = "laday-guagua";
client.Index(index, "comment", "c3", dict, "2");
#endregion
client.Refresh();
var q = new ConstantScoreQuery(new TopChildrenQuery("comment", new TermQuery("author", "lol")));
var result = client.Search(index, "blog" , q, 0, 5);
Assert.AreEqual(1, result.GetTotalCount());
Assert.AreEqual("1", result.GetHitIds()[0]);
q = new ConstantScoreQuery(new TopChildrenQuery("comment", new TermQuery("author", "laday-guagua")));
result = client.Search(index, "blog" , q, 0, 5);
Assert.AreEqual(2, result.GetTotalCount());
client.DeleteIndex(index);
}