forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWritePropertiesFromAttributeVisitor.cs
160 lines (153 loc) · 6.2 KB
/
WritePropertiesFromAttributeVisitor.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Globalization;
using Newtonsoft.Json;
using Elasticsearch.Net;
namespace Nest.Resolvers.Writers {
public class WritePropertiesFromAttributeVisitor : IElasticPropertyVisitor
{
private readonly JsonWriter _jsonWriter;
private readonly string _propertyName;
private readonly string _type;
public WritePropertiesFromAttributeVisitor(JsonWriter jsonWriter, string propertyName, string type)
{
this._jsonWriter = jsonWriter;
this._propertyName = propertyName;
this._type = type;
}
public void Visit(ElasticPropertyAttribute attribute)
{
this.VisitBaseAttribute(attribute);
}
public void VisitBaseAttribute(IElasticPropertyAttribute att) {
if (att.AddSortField)
{
this._jsonWriter.WritePropertyName("type");
this._jsonWriter.WriteValue("multi_field");
this._jsonWriter.WritePropertyName("fields");
this._jsonWriter.WriteStartObject();
this._jsonWriter.WritePropertyName(this._propertyName);
this._jsonWriter.WriteStartObject();
}
if (att.NumericType != NumericType.Default)
{
this._jsonWriter.WritePropertyName("type");
string numericType = Enum.GetName(typeof (NumericType), att.NumericType);
this._jsonWriter.WriteValue(numericType.ToLowerInvariant());
}
else
{
this._jsonWriter.WritePropertyName("type");
this._jsonWriter.WriteValue(this._type);
}
if (!att.Analyzer.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("analyzer");
this._jsonWriter.WriteValue(att.Analyzer);
}
if (!att.IndexAnalyzer.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("index_analyzer");
this._jsonWriter.WriteValue(att.IndexAnalyzer);
}
if (!att.IndexAnalyzer.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("index_analyzer");
this._jsonWriter.WriteValue(att.IndexAnalyzer);
}
if (!att.NullValue.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("null_value");
this._jsonWriter.WriteValue(att.NullValue);
}
if (!att.SearchAnalyzer.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("search_analyzer");
this._jsonWriter.WriteValue(att.SearchAnalyzer);
}
if (!att.DateFormat.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("format");
this._jsonWriter.WriteValue(att.DateFormat);
}
if (att.Index != FieldIndexOption.analyzed)
{
this._jsonWriter.WritePropertyName("index");
this._jsonWriter.WriteValue(Enum.GetName(typeof (FieldIndexOption), att.Index));
}
if (att.TermVector != TermVectorOption.no)
{
this._jsonWriter.WritePropertyName("term_vector");
this._jsonWriter.WriteValue(Enum.GetName(typeof (TermVectorOption), att.TermVector));
}
if (att.OmitNorms)
{
this._jsonWriter.WritePropertyName("omit_norms");
this._jsonWriter.WriteValue("true");
}
if (att.DocValues)
{
this._jsonWriter.WritePropertyName("doc_values");
this._jsonWriter.WriteValue("true");
}
if (att.OmitTermFrequencyAndPositions)
{
this._jsonWriter.WritePropertyName("omit_term_freq_and_positions");
this._jsonWriter.WriteValue("true");
}
if (!att.IncludeInAll)
{
this._jsonWriter.WritePropertyName("include_in_all");
this._jsonWriter.WriteValue("false");
}
if (att.Store)
{
this._jsonWriter.WritePropertyName("store");
this._jsonWriter.WriteValue("yes");
}
if (att.Boost != 1)
{
this._jsonWriter.WritePropertyName("boost");
this._jsonWriter.WriteRawValue(att.Boost.ToString(CultureInfo.InvariantCulture));
}
if (att.PrecisionStep != 4)
{
this._jsonWriter.WritePropertyName("precision_step");
this._jsonWriter.WriteRawValue(att.PrecisionStep.ToString(CultureInfo.InvariantCulture));
}
if (!att.Similarity.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("similarity");
this._jsonWriter.WriteValue(att.Similarity);
}
if (att.AddSortField)
{
this._jsonWriter.WriteEnd();
this._jsonWriter.WritePropertyName("sort");
this._jsonWriter.WriteStartObject();
if (att.NumericType != NumericType.Default)
{
this._jsonWriter.WritePropertyName("type");
string numericType = Enum.GetName(typeof (NumericType), att.NumericType);
this._jsonWriter.WriteValue(numericType.ToLowerInvariant());
}
else
{
this._jsonWriter.WritePropertyName("type");
this._jsonWriter.WriteValue(this._type);
}
if (att.SortAnalyzer.IsNullOrEmpty())
{
this._jsonWriter.WritePropertyName("index");
this._jsonWriter.WriteValue(Enum.GetName(typeof(FieldIndexOption), FieldIndexOption.not_analyzed));
}
else
{
this._jsonWriter.WritePropertyName("index_analyzer");
this._jsonWriter.WriteValue(att.SortAnalyzer);
}
this._jsonWriter.WriteEnd();
this._jsonWriter.WriteEnd();
}
}
}
}