Skip to content

Commit 7380dbe

Browse files
committed
Fix #1441: Support for murmur3 mapping type
1 parent 6811958 commit 7380dbe

13 files changed

+128
-14
lines changed

Diff for: src/Nest/Domain/Mapping/Descriptors/CorePropertiesDescriptor.cs

+10
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public CorePropertiesDescriptor<T> Generic(Func<GenericMappingDescriptor<T>, Gen
8181
return this;
8282
}
8383

84+
public CorePropertiesDescriptor<T> Murmur3Hash(Func<MurmurHashMappingDescriptor<T>, MurmurHashMappingDescriptor<T>> selector)
85+
{
86+
selector.ThrowIfNull("selector");
87+
var d = selector(new MurmurHashMappingDescriptor<T>());
88+
if (d == null || d._Mapping.Name.IsConditionless())
89+
throw new Exception("Could not get field name for murmur3 mapping");
90+
this.Properties.Add(d._Mapping.Name, d._Mapping);
91+
return this;
92+
}
93+
8494
//Reminder if you are adding a new mapping type, may one appear in the future
8595
//Add them to PropertiesDescriptor, CorePropertiesDescriptor (if its a new core type), SingleMappingDescriptor
8696
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Linq.Expressions;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
public class MurmurHashMappingDescriptor<T> where T : class
10+
{
11+
internal Murmur3HashMapping _Mapping = new Murmur3HashMapping();
12+
13+
public MurmurHashMappingDescriptor<T> Name(string name)
14+
{
15+
this._Mapping.Name = name;
16+
return this;
17+
}
18+
19+
public MurmurHashMappingDescriptor<T> Name(Expression<Func<T, object>> objectPath)
20+
{
21+
this._Mapping.Name = objectPath;
22+
return this;
23+
}
24+
}
25+
}

Diff for: src/Nest/Domain/Mapping/Descriptors/PropertiesDescriptor.cs

+11
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,17 @@ public PropertiesDescriptor<T> Custom(IElasticType customMapping)
173173
this.Properties.Add(customMapping.Name, customMapping);
174174
return this;
175175
}
176+
177+
public PropertiesDescriptor<T> Murmur3Hash(Func<MurmurHashMappingDescriptor<T>, MurmurHashMappingDescriptor<T>> selector)
178+
{
179+
selector.ThrowIfNull("selector");
180+
var d = selector(new MurmurHashMappingDescriptor<T>());
181+
if (d == null || d._Mapping.Name.IsConditionless())
182+
throw new Exception("Could not get field name for mumur hash mapping");
183+
this.Properties.Add(d._Mapping.Name, d._Mapping);
184+
return this;
185+
}
186+
176187
//Reminder if you are adding a new mapping type, may one appear in the future
177188
//Add them to PropertiesDescriptor, CorePropertiesDescriptor (if its a new core type), SingleMappingDescriptor
178189
}

Diff for: src/Nest/Domain/Mapping/Descriptors/SingleMappingDescriptor.cs

+9
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ public IElasticType Completion(Func<CompletionMappingDescriptor<T>, CompletionMa
137137
return d._Mapping;
138138
}
139139

140+
public IElasticType Murmur3Hash(Func<MurmurHashMappingDescriptor<T>, MurmurHashMappingDescriptor<T>> selector)
141+
{
142+
selector.ThrowIfNull("selector");
143+
var d = selector(new MurmurHashMappingDescriptor<T>());
144+
if (d == null)
145+
throw new Exception("Could not get murmur hash mapping");
146+
return d._Mapping;
147+
}
148+
140149
//Reminder if you are adding a new mapping type, may one appear in the future
141150
//Add them to PropertiesDescriptor, CorePropertiesDescriptor (if its a new core type), SingleMappingDescriptor
142151
}

Diff for: src/Nest/Domain/Mapping/Types/Murmur3HashMapping.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Newtonsoft.Json;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
7+
namespace Nest
8+
{
9+
[JsonObject(MemberSerialization.OptIn)]
10+
public class Murmur3HashMapping : MultiFieldMapping, IElasticType, IElasticCoreType
11+
{
12+
public Murmur3HashMapping() : base("murmur3") { }
13+
14+
[JsonIgnore]
15+
public string IndexName { get; set; }
16+
}
17+
}

Diff for: src/Nest/Enums/FieldType.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ public enum FieldType
9393
/// Only set this if you need to force a value type to be mapped to an elasticsearch object type.
9494
/// </summary>
9595
[EnumMember(Value = "object")]
96-
Object
96+
Object,
97+
/// <summary>
98+
/// Murmur hash type, for use with the cardinality aggregation.
99+
/// </summary>
100+
[EnumMember(Value = "murmur3")]
101+
Murmur3Hash
97102
}
98103
}

Diff for: src/Nest/Nest.csproj

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
<Compile Include="Domain\Geo\GeoLocation.cs" />
135135
<Compile Include="Domain\Geo\GeoPrecision.cs" />
136136
<Compile Include="Domain\Hit\ExplainGet.cs" />
137+
<Compile Include="Domain\Mapping\Descriptors\MurmurHashMappingDescriptor.cs" />
137138
<Compile Include="Domain\Mapping\PropertyMapping.cs" />
138139
<Compile Include="Domain\Mapping\Descriptors\FieldDataFilterDescriptor.cs" />
139140
<Compile Include="Domain\Mapping\Descriptors\FieldDataFrequencyFilterDescriptor.cs" />
@@ -162,6 +163,7 @@
162163
<Compile Include="Domain\Mapping\SubMappings\FieldData\FieldDataStringMapping.cs" />
163164
<Compile Include="Domain\Mapping\SubMappings\NormsMapping.cs" />
164165
<Compile Include="Domain\Mapping\SubMappings\MappingTransform.cs" />
166+
<Compile Include="Domain\Mapping\Types\Murmur3HashMapping.cs" />
165167
<Compile Include="Domain\Observers\RestoreObserver.cs" />
166168
<Compile Include="Domain\Observers\SnapshotObserver.cs" />
167169
<Compile Include="Domain\Repository\SnapshotShardFailure.cs" />

Diff for: src/Nest/Resolvers/Writers/TypeMappingWriter.cs

+2
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ private string GetElasticSearchTypeFromFieldType(FieldType? fieldType)
247247
return "nested";
248248
case FieldType.Object:
249249
return "object";
250+
case FieldType.Murmur3Hash:
251+
return "murmur3";
250252
default:
251253
return null;
252254
}

Diff for: src/Tests/Nest.Tests.Unit/Core/Map/FluentMappingFullExampleTests.cs

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public void MapFluentFull()
207207
.Fields(pprops => pprops
208208
.String(ps => ps.Name(p => p.Name).Index(FieldIndexOption.NotAnalyzed))
209209
.String(ps => ps.Name(p => p.Name.Suffix("searchable")).Index(FieldIndexOption.Analyzed))
210+
.Murmur3Hash(ps => ps.Name(p => p.Name.Suffix("hash")))
210211
)
211212
)
212213
.IP(s=>s
@@ -251,6 +252,9 @@ public void MapFluentFull()
251252
)
252253
)
253254
)
255+
.Murmur3Hash(mh => mh
256+
.Name("hash")
257+
)
254258
)
255259
);
256260

Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
{
2-
"elasticsearchprojects": {
3-
"properties": {
4-
"name": {
5-
"type": "multi_field",
6-
"fields": {
7-
"name": {
8-
"type": "string",
9-
"index": "not_analyzed"
10-
},
11-
"searchable": {
12-
"type": "string",
13-
"index": "analyzed"
2+
"elasticsearchprojects": {
3+
"properties": {
4+
"name": {
5+
"type": "multi_field",
6+
"fields": {
7+
"name": {
8+
"type": "string",
9+
"index": "not_analyzed"
10+
},
11+
"searchable": {
12+
"type": "string",
13+
"index": "analyzed"
14+
},
15+
"hash": {
16+
"type": "murmur3"
17+
}
1418
}
1519
}
1620
}
1721
}
18-
}
1922
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"elasticsearchprojects": {
3+
"properties": {
4+
"hash": {
5+
"type": "murmur3"
6+
}
7+
}
8+
}
9+
}

Diff for: src/Tests/Nest.Tests.Unit/Core/Map/Properties/PropertiesTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ public void MultiFieldProperty()
199199
.Fields(pprops => pprops
200200
.String(ps => ps.Name(p => p.Name).Index(FieldIndexOption.NotAnalyzed))
201201
.String(ps => ps.Name(p => p.Name.Suffix("searchable")).Index(FieldIndexOption.Analyzed))
202+
.Murmur3Hash(ps => ps.Name(p => p.Name.Suffix("hash")))
202203
)
203204
)
204205
)
@@ -371,5 +372,18 @@ public void CompletionProperty()
371372
);
372373
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
373374
}
375+
376+
[Test]
377+
public void MurmurHashProperty()
378+
{
379+
var result = this._client.Map<ElasticsearchProject>(m => m
380+
.Properties(props => props
381+
.Murmur3Hash(mh => mh
382+
.Name("hash")
383+
)
384+
)
385+
);
386+
this.JsonEquals(result.ConnectionStatus.Request, MethodInfo.GetCurrentMethod());
387+
}
374388
}
375389
}

Diff for: src/Tests/Nest.Tests.Unit/Nest.Tests.Unit.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,9 @@
621621
<None Include="Core\Map\Properties\NestedObjectProperty.json">
622622
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
623623
</None>
624+
<None Include="Core\Map\Properties\MurmurHashProperty.json">
625+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
626+
</None>
624627
<None Include="Core\Map\Properties\ObjectProperty.json">
625628
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
626629
</None>

0 commit comments

Comments
 (0)