public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { ScriptFilter term = (ScriptFilter)value; if (term != null) { writer.WriteStartObject(); writer.WritePropertyName("script"); writer.WriteStartObject(); writer.WritePropertyName("script"); writer.WriteValue(term.Script); if (term.Params != null && term.Params.Count > 0) { writer.WritePropertyName("params"); writer.WriteStartObject(); foreach (var filter in term.Params) { writer.WritePropertyName(filter.Key); writer.WriteValue(filter.Value); } writer.WriteEndObject(); } writer.WriteEndObject(); // writer.WriteRaw(",\"_cache\": " + term.Cache.ToString().ToLower()); writer.WriteEndObject(); } }
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()); }