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

            if (term != null)
            {
                writer.WriteStartObject();
                writer.WritePropertyName("nested");
                writer.WriteStartObject();

                writer.WritePropertyName("path");
                writer.WriteValue(term.Path);
                writer.WritePropertyName("query");
                if (term.Query != null)
                {
                    serializer.Serialize(writer, term.Query);
                }
                else
                {
                    serializer.Serialize(writer, term.Filter);
                }
//                writer.WritePropertyName("_cache");
//                writer.WriteValue(term.Cache);
                writer.WriteEndObject();
                writer.WriteEndObject();
            }
        }
        public void TestNestedFilter()
        {
//    {
//    "name": "jackson",
//    "resume": {
//        "age": 22,
//        "gender": "male",
//        "mail": "*****@*****.**"
//    }
//}
            var obj = "{     \"type\": \"vip\", \"name\": \"jackson\",    \"resume\": {        \"age\": 22,        \"gender\": \"male\",        \"mail\": \"[email protected]\"    }}";
            var op= client.Index(index, "person", "key1", obj);
            Assert.True(op.Success);

            obj = "{     \"type\": \"vip\", \"name\": \"tom\",    \"resume\": {        \"age\": 24,        \"gender\": \"female\",        \"mail\": \"[email protected]\"    }}";
            op = client.Index(index, "person", "key2", obj);
            Assert.True(op.Success);
        	client.Refresh();
            var nestFilter = new NestedFilter("person", new TermQuery("resume.age", 22), true);

            var q = new FilteredQuery(new TermQuery("type","vip"), nestFilter);
            var result=client.Search(index, "person", q, 0, 5);
            Assert.AreEqual(1,result.GetTotalCount());
            Assert.AreEqual("jackson", result.GetHits().Hits[0].Source["name"]);
        
            
        }