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

            if (term != null)
            {
                writer.WriteStartObject();
                writer.WritePropertyName("term");
                writer.WriteStartObject();
                writer.WritePropertyName(term.Field);
                writer.WriteValue(term.Value);
                writer.WriteEndObject();
                writer.WriteEndObject();
            }
        }
        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);
        }
        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 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 TestConstantSocreWithTermFilter()
        {

            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);
        }
		public void TestPrefixQuery()
		{
			var prefixQuery = new PrefixQuery("name", "张");
			var termFilter = new TermFilter("type", "common");
			var q = new FilteredQuery(prefixQuery,termFilter);
			var result = client.Search(index, "type" , q, 0, 5);

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

			prefixQuery = new PrefixQuery("name", "张三");
			termFilter = new TermFilter("type", "common");
			q = new FilteredQuery(prefixQuery, termFilter);
			result = client.Search(index, "type" , q, 0, 5);

			Assert.AreEqual(2, result.GetTotalCount());
		}
		public void TestMatchAllQuery()
		{
			var filter = new TermFilter("gender", "true");//new TermsQuery("gender", "true", "false");
			var filterQuery = new FilteredQuery( new MatchAllQuery(),filter);
			var result = client.Search(index, "type" , filterQuery, 0, 5);
			Assert.AreEqual(50, result.GetTotalCount());
			Assert.AreEqual(5, result.GetHits().Hits.Count);

			var filter1 = new TermFilter("gender", "true");
			filterQuery = new FilteredQuery(new MatchAllQuery(), filter1);
			result = client.Search(index, "type" , filterQuery, 0, 5);
			Assert.AreEqual(50, result.GetTotalCount());
			Assert.AreEqual(5, result.GetHits().Hits.Count);
		}
        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);
        }
        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());
        }
        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"]);

        }