forked from elastic/elasticsearch-net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateIndexDescriptor.cs
200 lines (169 loc) · 6 KB
/
CreateIndexDescriptor.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Elasticsearch.Net;
using Newtonsoft.Json;
using System.Linq.Expressions;
using Nest.Resolvers;
using Nest.Domain;
namespace Nest
{
[DescriptorFor("IndicesCreate")]
public partial class CreateIndexDescriptor : IPathInfo<CreateIndexRequestParameters>
{
internal string _Index { get; set; }
internal IndexSettings _IndexSettings = new IndexSettings();
private readonly IConnectionSettingsValues _connectionSettings;
public CreateIndexDescriptor(IConnectionSettingsValues connectionSettings)
{
this._connectionSettings = connectionSettings;
}
/// <summary>
/// Initialize the descriptor using the values from for instance a previous Get Index Settings call.
/// </summary>
public CreateIndexDescriptor InitializeUsing(IndexSettings indexSettings)
{
this._IndexSettings = indexSettings;
return this;
}
/// <summary>
/// Set the number of shards (if possible) for the new index.
/// </summary>
/// <param name="shards"></param>
/// <returns></returns>
public CreateIndexDescriptor NumberOfShards(int shards)
{
this._IndexSettings.NumberOfShards = shards;
return this;
}
/// <summary>
/// Set the number of replicas (if possible) for the new index.
/// </summary>
/// <param name="replicas"></param>
/// <returns></returns>
public CreateIndexDescriptor NumberOfReplicas(int replicas)
{
this._IndexSettings.NumberOfReplicas = replicas;
return this;
}
/// <summary>
/// Set/Update settings, the index.* prefix is not needed for the keys.
/// </summary>
public CreateIndexDescriptor Settings(Action<FluentDictionary<string, object>> settingsSelector)
{
settingsSelector.ThrowIfNull("settingsSelector");
var dict = new FluentDictionary<string, object>();
settingsSelector(dict);
foreach (var kv in dict)
{
this._IndexSettings.Settings.Add(kv.Key, kv.Value);
}
return this;
}
/// <summary>
/// Remove an existing mapping by name
/// </summary>
public CreateIndexDescriptor RemoveMapping(string typeName)
{
TypeNameMarker marker = typeName;
return this.RemoveMapping(marker);
}
/// <summary>
/// Remove an exisiting mapping by inferred type name
/// </summary>
public CreateIndexDescriptor RemoveMapping<T>() where T : class
{
TypeNameMarker marker = typeof(T);
return this.RemoveMapping(marker);
}
/// <summary>
/// Add an alias for this index upon index creation
/// </summary>
public CreateIndexDescriptor AddAlias(string aliasName, Func<CreateAliasDescriptor, CreateAliasDescriptor> addAliasSelector = null)
{
aliasName.ThrowIfNullOrEmpty("aliasName");
addAliasSelector = addAliasSelector ?? (a => a);
var alias = addAliasSelector(new CreateAliasDescriptor());
if (this._IndexSettings.Aliases == null)
this._IndexSettings.Aliases = new Dictionary<string, CreateAliasDescriptor>();
this._IndexSettings.Aliases.Add(aliasName, alias);
return this;
}
private CreateIndexDescriptor RemoveMapping(TypeNameMarker marker)
{
this._IndexSettings.Mappings = this._IndexSettings.Mappings.Where(m => m.Type != marker).ToList();
return this;
}
/// <summary>
/// Add a new mapping for T
/// </summary>
public CreateIndexDescriptor AddMapping<T>(Func<PutMappingDescriptor<T>, PutMappingDescriptor<T>> typeMappingDescriptor) where T : class
{
typeMappingDescriptor.ThrowIfNull("typeMappingDescriptor");
var d = typeMappingDescriptor(new PutMappingDescriptor<T>(this._connectionSettings));
var typeMapping = d._Mapping;
if (d._Type != null)
{
typeMapping.Name = d._Type.Name != null ? (PropertyNameMarker)d._Type.Name : d._Type.Type;
}
else
{
typeMapping.Name = typeof(T);
}
this._IndexSettings.Mappings.Add(typeMapping);
return this;
}
/// <summary>
/// Add a new mapping using the first rootObjectMapping parameter as the base to construct the new mapping.
/// Handy if you wish to reuse a mapping.
/// </summary>
public CreateIndexDescriptor AddMapping<T>(RootObjectMapping rootObjectMapping, Func<PutMappingDescriptor<T>, PutMappingDescriptor<T>> typeMappingDescriptor) where T : class
{
typeMappingDescriptor.ThrowIfNull("typeMappingDescriptor");
var d = typeMappingDescriptor(new PutMappingDescriptor<T>(this._connectionSettings) { _Mapping = rootObjectMapping,});
var typeMapping = d._Mapping;
if (d._Type != null)
{
typeMapping.Name = d._Type.Name != null ? (PropertyNameMarker)d._Type.Name : d._Type.Type;
}
else
{
typeMapping.Name = typeof (T);
}
this._IndexSettings.Mappings.Add(typeMapping);
return this;
}
public CreateIndexDescriptor AddWarmer(Func<CreateWarmerDescriptor, CreateWarmerDescriptor> warmerSelector)
{
warmerSelector.ThrowIfNull("warmerSelector");
var descriptor = warmerSelector(new CreateWarmerDescriptor());
var mapping = new WarmerMapping { Name = descriptor._WarmerName, Types = descriptor._Types, Source = descriptor._SearchDescriptor };
this._IndexSettings.Warmers.Add(descriptor._WarmerName, mapping);
return this;
}
public CreateIndexDescriptor DeleteWarmer(string warmerName)
{
this._IndexSettings.Warmers.Remove(warmerName);
return this;
}
/// <summary>
/// Set up analysis tokenizers, filters, analyzers
/// </summary>
public CreateIndexDescriptor Analysis(Func<AnalysisDescriptor, AnalysisDescriptor> analysisSelector)
{
analysisSelector.ThrowIfNull("analysisSelector");
var analysis = analysisSelector(new AnalysisDescriptor(this._IndexSettings.Analysis));
this._IndexSettings.Analysis = analysis == null ? null : analysis._AnalysisSettings;
return this;
}
ElasticsearchPathInfo<CreateIndexRequestParameters> IPathInfo<CreateIndexRequestParameters>.ToPathInfo(IConnectionSettingsValues settings)
{
var pathInfo = new ElasticsearchPathInfo<CreateIndexRequestParameters>();
pathInfo.HttpMethod = PathInfoHttpMethod.POST;
pathInfo.Index = this._Index;
pathInfo.RequestParameters = this._QueryString;
return pathInfo;
}
}
}