Ejemplo n.º 1
0
	    public void TestTermQueryWithBoost()
	    {
	        var query = new TermQuery("gender", "true",2);
	        var result = client.Search(index, "type" , query, 0, 5);
	        Assert.AreEqual(50, result.GetTotalCount());
	        Assert.AreEqual(5, result.GetHits().Hits.Count);
	    }
Ejemplo n.º 2
0
        public void TestConstantSocreWithTermQuery()
        {
            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);
        }  
Ejemplo n.º 3
0
        public void TestFilterdQuery()
        {

            var termQuery = new TermQuery("type", "common");
            var termFilter = new TermFilter("age", "24");
            var filteredQuery = new FilteredQuery(termQuery,termFilter);
            var result2 = client.Search(index, "type" , filteredQuery, 0, 5);
            Assert.AreEqual(1, result2.GetTotalCount());
            Assert.AreEqual(1, result2.GetHits().Hits.Count);

            var wildQuery = new WildcardQuery("name", "张三*");
            termFilter = new TermFilter("age", "23");
            filteredQuery = new FilteredQuery(wildQuery, termFilter);
            result2 = client.Search(index, "type" , filteredQuery, 0, 5);
            Assert.AreEqual(1, result2.GetTotalCount());
            Assert.AreEqual(1, result2.GetHits().Hits.Count);
            Assert.AreEqual("张三丰",result2.GetHits().Hits[0].Source["name"]);


            var boolQuery = new BoolQuery();
            boolQuery.Must(new TermQuery("type", "common"));
            boolQuery.Must(new WildcardQuery("name", "张三*"));
            boolQuery.Should(new TermQuery("age", 24));

            filteredQuery=new FilteredQuery(boolQuery,new TermFilter("age","23"));

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



            filteredQuery = new FilteredQuery(boolQuery, new TermFilter("age", "24"));

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


            filteredQuery = new FilteredQuery(boolQuery, new TermFilter("type", "common"));

            result2 = client.Search(index, "type" , filteredQuery, 0, 5);
            Assert.AreEqual(2, result2.GetTotalCount());
            Assert.AreEqual(2, result2.GetHits().Hits.Count);

            
            //test should

            boolQuery = new BoolQuery();
            boolQuery.Must(new TermQuery("type", "common"));
            boolQuery.Must(new WildcardQuery("name", "张*"));
            boolQuery.Should(new TermQuery("age", 24));

            filteredQuery = new FilteredQuery(boolQuery, new TermFilter("type", "common"));

            result2 = client.Search(index, "type" , filteredQuery, 0, 5);
            Assert.AreEqual(3, result2.GetTotalCount());
            Assert.AreEqual(3, result2.GetHits().Hits.Count);

            boolQuery = new BoolQuery();
            boolQuery.Must(new TermQuery("type", "common"));
            boolQuery.Must(new WildcardQuery("name", "张*"));
            boolQuery.Should(new TermQuery("age", 24));
            boolQuery.Should(new TermQuery("age", 28));
            boolQuery.Should(new TermQuery("age", 22));
            //must+should ->[should] make nonsense
            filteredQuery = new FilteredQuery(boolQuery, new TermFilter("type", "common"));

            result2 = client.Search(index, "type" , filteredQuery, 0, 5);
            Assert.AreEqual(3, result2.GetTotalCount());
            Assert.AreEqual(3, result2.GetHits().Hits.Count);


            boolQuery = new BoolQuery();
            boolQuery.Should(new TermQuery("age", 24));
            boolQuery.Should(new TermQuery("age", 28));
            boolQuery.Should(new TermQuery("age", 22));
            filteredQuery = new FilteredQuery(boolQuery, new TermFilter("type", "common"));

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


            boolQuery.Should(new TermQuery("age", 25));
            filteredQuery = new FilteredQuery(boolQuery, new TermFilter("type", "common"));

            result2 = client.Search(index, "type" , filteredQuery, 0, 5);
            Assert.AreEqual(2, result2.GetTotalCount());
            Assert.AreEqual(2, result2.GetHits().Hits.Count);
        }
Ejemplo n.º 4
0
        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);

        }
Ejemplo n.º 5
0
 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);
 }
Ejemplo n.º 6
0
 public void TestSettingReturnFileds()
 {
     var q = new TermQuery("gender","true");
     
     client.Search(index, "type" , q,null, 0, 5,new string[]{"_id"});
 }
Ejemplo n.º 7
0
		public void TestCustomScoreQuery()
		{
			//age 23 24 25
			var query = new TermQuery("type", "common");
			var dict = new Dictionary<string, object>();
			dict["param1"] = 0.2;
			var script = "_score + doc['age'].value - param1";
			var q = new CustomScoreQuery(query, script, dict);
			var result = client.Search(index, "type", q, 0, 5);
			foreach (var o in result.GetHits().Hits)
			{
				Console.WriteLine(o.ToString());
			}
			Assert.AreEqual("张",result.GetHits().Hits[0].Source["name"]);
		}
Ejemplo n.º 8
0
        public void TestScriptFilter()
        {
            //age 23 24 25
            var query = new TermQuery("type", "common");
            var dict = new Dictionary<string, object>();
            dict["param1"] = 20;
            var filter = new ScriptFilter("doc['age'].value > param1",dict);
            var filterQ = new FilteredQuery(query, filter);
            var result= client.Search(index, "type" , filterQ, 0, 5);
            Assert.AreEqual(3,result.GetTotalCount());


            dict = new Dictionary<string, object>();
            dict["param1"] = 23;
            filter = new ScriptFilter("doc['age'].value > param1", dict);
            filterQ = new FilteredQuery(query, filter);
            result = client.Search(index, "type" , filterQ, 0, 5);
            Assert.AreEqual(2, result.GetTotalCount());

            dict = new Dictionary<string, object>();
            dict["param1"] = 24;
            filter = new ScriptFilter("doc['age'].value >= param1", dict);
            filterQ = new FilteredQuery(query, filter);
            result = client.Search(index, "type" , filterQ, 0, 5);
            Assert.AreEqual(2, result.GetTotalCount());

            dict = new Dictionary<string, object>();
            dict["param1"] = 25;
            filter = new ScriptFilter("doc['age'].value >= param1", dict);
            filterQ = new FilteredQuery(query, filter);
            result = client.Search(index, "type" , filterQ, 0, 5);
            Assert.AreEqual(1, result.GetTotalCount());

            dict = new Dictionary<string, object>();
            dict["param1"] = 25;
            filter = new ScriptFilter("doc['age'].value < param1", dict);
            filterQ = new FilteredQuery(query, filter);
            result = client.Search(index, "type" , filterQ, 0, 5);
            Assert.AreEqual(2, result.GetTotalCount());

            dict = new Dictionary<string, object>();
            dict["param1"] = 20;
            filter = new ScriptFilter("doc['age'].value < param1", dict);
            filterQ = new FilteredQuery(query, filter);
            result = client.Search(index, "type" , filterQ, 0, 5);
            Assert.AreEqual(0, result.GetTotalCount());
        }
Ejemplo n.º 9
0
		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"]);
		}
Ejemplo n.º 10
0
        public void TestMatchAllFilter()
        {
            var query = new TermQuery("gender","true");//new TermsQuery("gender", "true", "false");
            var filterQuery = new FilteredQuery(query, new MatchAllFilter());
            var result = client.Search(index, "type" , filterQuery, 0, 5);
            Assert.AreEqual(50, result.GetTotalCount());
            Assert.AreEqual(5, result.GetHits().Hits.Count);

            var query1 = new TermsQuery("gender", "true");
            filterQuery = new FilteredQuery(query1, new MatchAllFilter());
            result = client.Search(index, "type" , filterQuery, 0, 5);
            Assert.AreEqual(50, result.GetTotalCount());
            Assert.AreEqual(5, result.GetHits().Hits.Count);
        }
Ejemplo n.º 11
0
        public void TestNotFilter()
        {
            var termFilter = new TermFilter("age", 24);
            var notFilter = new NotFilter(termFilter);

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

            var q = new FilteredQuery(termQuery, notFilter);

            var result2 = client.Search(index, "type" , q, 0, 5);
            Assert.AreEqual(2, result2.GetTotalCount());
            Assert.AreEqual(2, result2.GetHits().Hits.Count);
        }
Ejemplo n.º 12
0
        public void TestPrefixFilter()
        {
            var prefixfilter = new PrefixFilter("name", "张");
            var termQuery = new TermQuery("type", "common");
            var q = new FilteredQuery(termQuery, prefixfilter);
            var result = client.Search(index, "type" , q, 0, 5);

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

            prefixfilter = new PrefixFilter("name", "张三");
            termQuery = new TermQuery("type", "common");
            q = new FilteredQuery(termQuery, prefixfilter);
            result = client.Search(index, "type" , q, 0, 5);

            Assert.AreEqual(2, result.GetTotalCount());
        }
Ejemplo n.º 13
0
        public void TestOrFilter()
        {
            var termFilter = new TermFilter("age", 24);
            var termFilter1 = new TermFilter("name", "张三丰");

            var orFilter = new OrFilter(termFilter,termFilter1);

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

            var q = new FilteredQuery(termQuery, orFilter);

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

            Assert.AreEqual(2, result.GetTotalCount());
        }
Ejemplo n.º 14
0
        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"]);

        }