forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuzzyQueryJsonConverter.cs
117 lines (98 loc) · 3.34 KB
/
FuzzyQueryJsonConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using System.Linq;
using Elasticsearch.Net;
using Nest.DSL.Query.Behaviour;
using Nest.Resolvers;
using Nest.Resolvers.Converters;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Nest
{
/// <summary>
/// JSON converter for IDictionary that ignores the contract resolver (e.g. CamelCasePropertyNamesContractResolver)
/// when converting dictionary keys to property names.
/// </summary>
public class FuzzyQueryJsonConverter: JsonConverter
{
public override bool CanConvert(Type objectType)
{
return true;
}
public override bool CanRead { get { return true; } }
public override bool CanWrite { get { return true; } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var j = JObject.Load(reader);
if (!j.HasValues) return null;
var firstProp = j.Properties().FirstOrDefault();
if (firstProp == null) return null;
var field = firstProp.Name;
var jo = firstProp.Value.Value<JObject>();
if (jo == null) return null;
JToken v = null;
if (!jo.TryGetValue("value", out v)) return null;
IFuzzyQuery fq = null;
if (v.Type == JTokenType.Date) fq = new FuzzyDateQueryDescriptor<object>();
else if (v.Type == JTokenType.String) fq = new FuzzyQueryDescriptor<object>();
else if (v.Type == JTokenType.Integer || v.Type == JTokenType.Float) fq = new FuzzyNumericQueryDescriptor<object>();
else return null;
fq.Field = field;
fq.Boost = GetPropValue<double?>(jo, "boost");
fq.Fuzziness = GetPropValue<string>(jo, "fuzziness");
fq.MaxExpansions = GetPropValue<int?>(jo, "max_expansions");
fq.UnicodeAware = GetPropValue<bool?>(jo, "unicode_aware");
fq.Transpositions = GetPropValue<bool?>(jo, "transpositions");
var rewriteString = GetPropValue<string>(jo, "rewrite");
if (!rewriteString.IsNullOrEmpty())
fq.Rewrite = Enum.Parse(typeof(RewriteMultiTerm), rewriteString) as RewriteMultiTerm?;
if (fq is IStringFuzzyQuery)
{
var fqs = fq as IStringFuzzyQuery;
fqs.PrefixLength = GetPropValue<int?>(jo, "prefix_length");
fqs.Value = GetPropValue<string>(jo, "value");
}
if (fq is IFuzzyDateQuery)
{
var fdq = fq as IFuzzyDateQuery;
fdq.Value = GetPropValue<DateTime?>(jo, "value");
}
if (fq is IFuzzyNumericQuery)
{
var fnq = fq as IFuzzyNumericQuery;
fnq.Value = GetPropValue<double?>(jo, "value");
}
return fq;
}
public TReturn GetPropValue<TReturn>(JObject jObject, string propertyName)
{
JToken jToken = null;
return !jObject.TryGetValue(propertyName, out jToken)
? default(TReturn)
: jToken.Value<TReturn>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var v = value as IFieldNameQuery;
if (v == null || v.IsConditionless)
return;
var fieldName = v.GetFieldName();
if (fieldName == null)
return;
var contract = serializer.ContractResolver as SettingsContractResolver;
if (contract == null)
return;
var field = contract.Infer.PropertyPath(fieldName);
if (field.IsNullOrEmpty())
return;
writer.WriteStartObject();
{
writer.WritePropertyName(field);
serializer.Serialize(writer, value);
}
writer.WriteEndObject();
}
}
}