示例#1
0
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            TermsFilter term = (TermsFilter)value;

            if (term != null)
            {
                var stringBuilder = new StringBuilder();

                stringBuilder.Append("{    \"terms\" : {        \"" + term.Field + "\" : [");

                var i = 0;
                foreach (var t in term.Values)
                {
                    if (i > 0)
                    {
                        stringBuilder.Append(",");
                    }
                    stringBuilder.Append("\"" + t + "\"");
                    i++;
                }

                stringBuilder.Append("]");
//                stringBuilder.Append(",\"_cache\": " + term.Cache.ToString().ToLower());
                stringBuilder.Append("}}");

                writer.WriteRawValue(stringBuilder.ToString());
            }
        }
        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);
        }
        public void TestTermsFilter()
        {
            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());

            termsFilter = new TermsFilter("age", 22);
            constantQ = new ConstantScoreQuery(termsFilter);
            result = client.Search(index, "type" , constantQ, 0, 5);
            Assert.AreEqual(1, result.GetTotalCount());
        }