public void TestDisMaxQuery()
{
string textType = "dismax";
var typeSetting = new TypeSetting(textType);
// hed is the most important field, dek is secondary
typeSetting.AddStringField("hed").Analyzer = "standard";
typeSetting.AddStringField("dek").Analyzer = "standard";
client.PutMapping(index, typeSetting);
client.Refresh();
// d1 is an "ok" match for: albino elephant
var
dict = new Dictionary<string, object>();
dict["id"] = "d1";
dict["hed"] = "elephant";
dict["dek"] = "elephant";
var op = client.Index(index, textType, "d1", dict);
Assert.True(op.Success);
// d2 is a "good" match for: albino elephant
IndexItem
item = new IndexItem(textType, "d2");
item.Add("id", "d2");
item.Add("hed", "elephant");
item.Add("dek", "albino");
item.Add("dek", "elephant");
op = client.Index(index, item);
Assert.True(op.Success);
//d3 is a "better" match for: albino elephant
item = new IndexItem(textType, "d3");
item.Add("id", "d3");
item.Add("hed", "albino");
item.Add("hed", "elephant");
op = client.Index(index, item);
Assert.True(op.Success);
// d4 is the "best" match for: albino elephant
item = new IndexItem(textType, "d4");
item.Add("id", "d4");
item.Add("hed", "albino");
item.Add("hed", "elephant");
item.Add("dek", "albino");
op = client.Index(index, item);
Assert.True(op.Success);
client.Refresh();
var dismaxQuery = new DisjunctionMaxQuery(0.0f);
dismaxQuery.AddQuery(new TermQuery("hed", "albino"));
dismaxQuery.AddQuery(new TermQuery("hed", "elephant"));
var result = client.Search(index, textType, dismaxQuery);
Console.WriteLine("all docs should match");
Assert.AreEqual(4,result.GetTotalCount());
foreach (var o in result.GetHits().Hits)
{
Console.WriteLine(o.ToString());
}
dismaxQuery = new DisjunctionMaxQuery(0.0f);
dismaxQuery.AddQuery(new TermQuery("dek", "albino"));
dismaxQuery.AddQuery(new TermQuery("dek", "elephant"));
result = client.Search(index, textType, dismaxQuery);
Console.WriteLine("3 docs should match");
Assert.AreEqual(3, result.GetTotalCount());
foreach (var o in result.GetHits().Hits)
{
Console.WriteLine(o.ToString());
}
dismaxQuery = new DisjunctionMaxQuery(0.0f);
dismaxQuery.AddQuery(new TermQuery("dek", "albino"));
dismaxQuery.AddQuery(new TermQuery("dek", "elephant"));
dismaxQuery.AddQuery(new TermQuery("hed", "albino"));
dismaxQuery.AddQuery(new TermQuery("hed", "elephant"));
result = client.Search(index, textType, dismaxQuery);
Console.WriteLine("all docs should match");
Assert.AreEqual(4, result.GetTotalCount());
foreach (var o in result.GetHits().Hits)
{
Console.WriteLine(o.ToString());
}
dismaxQuery = new DisjunctionMaxQuery(0.01f);
dismaxQuery.AddQuery(new TermQuery("dek", "albino"));
dismaxQuery.AddQuery(new TermQuery("dek", "elephant"));
result = client.Search(index, textType, dismaxQuery);
Console.WriteLine("3 docs should match");
float score0 = Convert.ToSingle(result.GetHits().Hits[0].Score);
float score1 = Convert.ToSingle(result.GetHits().Hits[1].Score);
float score2 = Convert.ToSingle(result.GetHits().Hits[2].Score);
foreach (var o in result.GetHits().Hits)
{
Console.WriteLine(o.ToString());
}
Assert.IsTrue(score0 > score1);
Assert.AreEqual(score1, score2);
Assert.AreEqual(3, result.GetTotalCount());
Assert.AreEqual("d2", result.GetHits().Hits[0].Source["id"]);
}