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

            if (term != null)
            {
                writer.WriteStartObject();
                writer.WritePropertyName("bool");
                writer.WriteStartObject();
                bool pre = false;
                if (term.MustFilters != null && term.MustFilters.Count > 0)
                {
                    writer.WritePropertyName("must");
                    writer.WriteStartArray();
                    foreach (var VARIABLE in term.MustFilters)
                    {
                        serializer.Serialize(writer, VARIABLE);
                    }
                    writer.WriteEndArray();
                }
                if (term.MustNotFilters != null && term.MustNotFilters.Count > 0)
                {
                    writer.WritePropertyName("must_not");
                    writer.WriteStartArray();
                    foreach (var VARIABLE in term.MustNotFilters)
                    {
                        serializer.Serialize(writer, VARIABLE);
                    }
                    writer.WriteEndArray();
                }
                if (term.ShouldFilters != null && term.ShouldFilters.Count > 0)
                {
                    writer.WritePropertyName("should");
                    writer.WriteStartArray();
                    foreach (var shouldQuery in term.ShouldFilters)
                    {
                        serializer.Serialize(writer, shouldQuery);
                    }
                    writer.WriteEndArray();
                }
                writer.WriteEndObject();
                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 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);
        }