public void TestTermsFilterCache()
{
for (int i = 50; i < 10000; i++)
{
var dict = new Dictionary<string, object>();
dict["age"] = i;
client.Index(index, "type", i.ToString(),dict);
}
var termsFilter = new TermsFilter("age", 22, 23, 24, 25);
var constantQ = new ConstantScoreQuery(termsFilter);
var result = client.Search(index, "type" , constantQ, 0, 5);
Assert.AreEqual(4, result.GetTotalCount());
Stopwatch stopwatch=new Stopwatch();
stopwatch.Start();
for (int i = 0; i < 30000; i++)
{
client.Search(index, "type" , constantQ, 0, 5);
}
stopwatch.Stop();
var time1 = stopwatch.ElapsedMilliseconds;
termsFilter = new TermsFilter("age", 22, 23, 24, 25);
termsFilter.SetCache(true);
constantQ = new ConstantScoreQuery(termsFilter);
result = client.Search(index, "type" , constantQ, 0, 5);
Assert.AreEqual(4, result.GetTotalCount());
stopwatch = new Stopwatch();
stopwatch.Start();
for (int i = 0; i < 30000; i++)
{
client.Search(index, "type" , constantQ, 0, 5);
}
stopwatch.Stop();
var time2 = stopwatch.ElapsedMilliseconds;
Console.WriteLine("NoCache:"+time1);
Console.WriteLine("Cache:"+time2);
}