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

            if (term != null)
            {
                writer.WriteStartObject();
                writer.WritePropertyName("and");
                writer.WriteStartObject();
                writer.WritePropertyName("filters");
                writer.WriteStartArray();
                foreach (var filter in term.Filters)
                {
                    serializer.Serialize(writer, filter);
                }
                writer.WriteEndArray();
                writer.WriteEndObject();
//                writer.WriteRaw(",\"_cache\": " + term.Cache.ToString().ToLower());
                writer.WriteEndObject();
            }
        }
        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 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"]);

        }