public void TestConstantSocreWithWildQuery()
        {
            var query  = new WildcardQuery("name","张*");

            var constanQuery = new ConstantScoreQuery(query);
            
            var result = client.Search(index, "type" , constanQuery, 0, 5);
            
            Assert.AreEqual(3, result.GetTotalCount());
            Assert.AreEqual(3, result.GetHits().Hits.Count);
        }    
        public void TestConstantSocreWithStringQuery()
        {
            var query = new QueryStringQuery("gender:true");

            var constanQuery = new ConstantScoreQuery(query);
            
            var result = client.Search(index, "type" , constanQuery, 0, 5);
            
            Assert.AreEqual(50, result.GetTotalCount());
            Assert.AreEqual(5, result.GetHits().Hits.Count);
        }  
        public void CompareQueryWithFilter()
        {
            var query = new TermQuery("gender", "true");

            var constanQuery = new ConstantScoreQuery(query);

            var result = client.Search(index, "type" , constanQuery, 0, 5);
            Assert.AreEqual(50, result.GetTotalCount());
            Assert.AreEqual(5, result.GetHits().Hits.Count);

            var termQuery = new TermQuery("gender", "true");
            var result1 = client.Search(index, "type" , termQuery, 0, 5);
            Assert.AreEqual(50, result1.GetTotalCount());
            Assert.AreEqual(5, result1.GetHits().Hits.Count);

            var termFilter = new TermFilter("gender", "true");
            var constanFilter = new ConstantScoreQuery(termFilter);
            var result2 = client.Search(index, "type" , constanFilter, 0, 5);
            Assert.AreEqual(50, result2.GetTotalCount());
            Assert.AreEqual(5, result2.GetHits().Hits.Count);


            //perf test
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000; i++)
            {
                client.Search(index, "type" , termQuery, 0, 5);
            }
            stopwatch.Stop();
            var time1 = stopwatch.ElapsedMilliseconds;


            stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000; i++)
            {
                client.Search(index, "type" , constanQuery, 0, 5);
            }
            stopwatch.Stop();
            var time2 = stopwatch.ElapsedMilliseconds;

            stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000; i++)
            {
                client.Search(index, "type" , constanFilter, 0, 5);
            }
            stopwatch.Stop();
            var time3 = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("TermQuery               Time1:" + time1);
            Console.WriteLine("ConstantQueryWithQuery  Time2:" + time2);
            Console.WriteLine("ConstantQueryWithFilter Time3:" + time3);

        }
 public void TestConstantQueryWithQueryFilter()
 {
     var termQuery = new TermQuery("gender", "true");
     var queryFilter = new QueryFilter(termQuery);
     var constanFilter = new ConstantScoreQuery(queryFilter);
     var result2 = client.Search(index, "type" , constanFilter, 0, 5);
     Assert.AreEqual(50, result2.GetTotalCount());
     Assert.AreEqual(5, result2.GetHits().Hits.Count);
 }
        public void TestConstantScoreNestedAndFilter()
        {

            IFilter termFilter = new TermFilter("gender", "true");

            var andFilter = new AndFilter(termFilter);
            
            var constanFilter = new ConstantScoreQuery(andFilter);
            
            var result2 = client.Search(index, "type", constanFilter, 0, 5);
            Assert.AreEqual(50, result2.GetTotalCount());
            Assert.AreEqual(5, result2.GetHits().Hits.Count);


            //test and filter and range filter

            termFilter = new RangeFilter("age", "22", "25", true, true);

            andFilter = new AndFilter(termFilter);

            constanFilter = new ConstantScoreQuery(andFilter);
            result2 = client.Search(index, "type", constanFilter, 0, 5);
            Assert.AreEqual(4, result2.GetTotalCount());
            Assert.AreEqual(4, result2.GetHits().Hits.Count);


            //test bool filter and range filter
            termFilter = new RangeFilter("age", "22", "25", true, true);

            var boolfilter = new BoolFilter();
            boolfilter.Must(termFilter);

            constanFilter = new ConstantScoreQuery(boolfilter);
            result2 = client.Search(index, "type", constanFilter, 0, 5);
            Assert.AreEqual(4, result2.GetTotalCount());
            Assert.AreEqual(4, result2.GetHits().Hits.Count);


        }
 public void TestConstantScoreWithRangeFilter()
 {
     var termFilter = new RangeFilter("age", "22","25",true,true);
     var constanFilter = new ConstantScoreQuery(termFilter);
     var result2 = client.Search(index, "type", constanFilter, 0, 5);
     Assert.AreEqual(4, result2.GetTotalCount());
     Assert.AreEqual(4, result2.GetHits().Hits.Count);
 }
		public void TestRangeFilter()
		{
			var rangefilter = new RangeFilter("age", "22", "25", true, true);
			ConstantScoreQuery query = new ConstantScoreQuery(rangefilter);

			var result = client.Search(index, "type" , query, 0, 5);

			Assert.AreEqual(4, result.GetTotalCount());


			rangefilter = new RangeFilter("age", "22", "25", false, true);
			query = new ConstantScoreQuery(rangefilter);

			result = client.Search(index, "type" , query, 0, 5);

			Assert.AreEqual(3, result.GetTotalCount());

			rangefilter = new RangeFilter("age", "22", "25", false, false);
			query = new ConstantScoreQuery(rangefilter);

			result = client.Search(index, "type" , query, 0, 5);

			Assert.AreEqual(2, result.GetTotalCount());
		}
		public void TestIdsQuery()
		{
			var constantScoreQuery = new ConstantScoreQuery(new IdsQuery("type", "1", "2", "3"));
			var result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);

			constantScoreQuery = new ConstantScoreQuery(new IdsQuery("type", "1", "2", "3", "1121"));
			result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);


			var item = new IndexItem("type1", "uk111");
			item.Add("iid", 1);
			client.Index(index, item);
			item = new IndexItem("type1", "dk222");
			item.Add("iid", 2);
			client.Index(index, item);

			constantScoreQuery = new ConstantScoreQuery(new IdsQuery("type", "1", "2", "3", "1121", "uk111"));
			result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);


			//ids can't query corss type
			constantScoreQuery = new ConstantScoreQuery(new IdsQuery(new string[] { "type", "type1" }, "1", "2", "3", "1121", "uk111"));
			result = client.Search(index, "type" , constantScoreQuery, 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());
			Assert.AreEqual(3, result.GetHits().Hits.Count);

			//waiting for refresh
			Thread.Sleep(1000);

			constantScoreQuery = new ConstantScoreQuery(new IdsQuery(new string[] { "type", "type1" }, "1", "2", "3", "1121", "uk111"));
			result = client.Search(index, null, constantScoreQuery, 0, 5);
			Assert.AreEqual(4, result.GetTotalCount());
			Assert.AreEqual(4, result.GetHits().Hits.Count);


			constantScoreQuery = new ConstantScoreQuery(new IdsQuery(new string[] { "type", "type1" }, "1", "2", "3", "1121", "uk111", "dk222"));
			result = client.Search(index, null, constantScoreQuery, 0, 5);
			Assert.AreEqual(5, result.GetTotalCount());
			Assert.AreEqual(5, result.GetHits().Hits.Count);
		}
		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);
		}
        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  TestWithSort()
		{
			var termq = new TermQuery("type", "common");
			var conq = new ConstantScoreQuery(termq);
			var result= client.Search(index, new string[] {"type"}, conq, new SortItem("age", SortType.Asc), 0, 5);
			Assert.AreEqual(3,result.GetTotalCount());

			Assert.AreEqual(23,result.GetHits().Hits[0].Source["age"]);
			Assert.AreEqual(24,result.GetHits().Hits[1].Source["age"]);
			Assert.AreEqual(25,result.GetHits().Hits[2].Source["age"]);

			result = client.Search(index, new string[] { "type" }, conq, new SortItem("age", SortType.Desc), 0, 5);
			Assert.AreEqual(3, result.GetTotalCount());

			Assert.AreEqual(25, result.GetHits().Hits[0].Source["age"]);
			Assert.AreEqual(24, result.GetHits().Hits[1].Source["age"]);
			Assert.AreEqual(23, result.GetHits().Hits[2].Source["age"]);
		}
        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());
        }
        public void TestTypeFilter()
        {
            string testForShard = "test_for_shard_123";
            client.CreateIndex(testForShard, new IndexSetting(6, 0));
            for (int i = 0; i < 10; i++)
            {
                Dictionary<string, object> dict = new Dictionary<string, object>();
                dict["a"] = i;
                client.Index(testForShard, "type1", i.ToString(), dict);
            }

            for (int i = 0; i < 10; i++)
            {
                Dictionary<string, object> dict = new Dictionary<string, object>();
                dict["a"] = i;
                client.Index(testForShard, "type2", i.ToString(), dict);
            }

            Thread.Sleep(1000);
            var constantScoreQuery = new ConstantScoreQuery(new TypeFilter("type2"));
            var result = client.Search(testForShard, null, constantScoreQuery, 0, 25);
            Assert.AreEqual(10, result.GetTotalCount());
            Assert.AreEqual(10, result.GetHits().Hits.Count);

            client.DeleteIndex(testForShard);
        }
        public void TestMissingFiledFilter()
        {
            var constantScoreQuery = new ConstantScoreQuery(new MissingFilter("age"));
            var result = client.Search(index, "type" , constantScoreQuery, 0, 5);
            Assert.AreEqual(100, result.GetTotalCount());
            Assert.AreEqual(5, result.GetHits().Hits.Count); 



            constantScoreQuery = new ConstantScoreQuery(new MissingFilter("ids"));
            result = client.Search(index, "type" , constantScoreQuery, 0, 5);
         Assert.AreEqual(4, result.GetTotalCount());
            Assert.AreEqual(4, result.GetHits().Hits.Count);
        }
        public void TestBoolFilterWithTwoCondition()
        {
            var boolFilter = new BoolFilter();
            boolFilter.Must(new TermFilter("type", "common"));
            boolFilter.Must(new TermFilter("age", "23"));

            var constantScoreQuery = new ConstantScoreQuery(boolFilter);
            var result = client.Search(index, "type" , constantScoreQuery, 0, 5);
            Assert.AreEqual(1, result.GetTotalCount());
            Assert.AreEqual(1, result.GetHits().Hits.Count);


            boolFilter = new BoolFilter();
            boolFilter.Must(new TermFilter("type", "common"));
            boolFilter.MustNot(new TermFilter("age", "23"));
            constantScoreQuery = new ConstantScoreQuery(boolFilter);
            result = client.Search(index, "type" , constantScoreQuery, 0, 5);
            Assert.AreEqual(2, result.GetTotalCount());
            Assert.AreEqual(2, result.GetHits().Hits.Count);
        }
        public void TestAndFilter()
        {
            var termFilter = new TermFilter("age", 24);
//            var termFilter1 = new TermFilter("name", "张三");
            var andFilter = new AndFilter(termFilter);

            var termQuery = new TermQuery("type", "common");

            var q = new FilteredQuery(termQuery,andFilter);

            var result2 = client.Search(index, "type" , q, 0, 5);
            Assert.AreEqual(1, result2.GetTotalCount());
            Assert.AreEqual(1, result2.GetHits().Hits.Count);
            Assert.AreEqual("张三", result2.GetHits().Hits[0].Source["name"]);



            var constantQuery = new ConstantScoreQuery(andFilter);

            result2 = client.Search(index, "type" , constantQuery, 0, 5);
            Assert.AreEqual(1, result2.GetTotalCount());
            Assert.AreEqual(1, result2.GetHits().Hits.Count);
            Assert.AreEqual("张三", result2.GetHits().Hits[0].Source["name"]);

        }