Skip to content

Commit 05d6957

Browse files
[Backport master] Add match_only_text field (#5805)
* Add match_only_text field (#5801) * Fix namespace Co-authored-by: Steve Gordon <sgordon@hotmail.co.uk>
1 parent ca1ee1e commit 05d6957

File tree

15 files changed

+142
-5
lines changed

15 files changed

+142
-5
lines changed

src/Nest/Mapping/DynamicTemplate/SingleMapping.cs

+4
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,10 @@ public IProperty Generic(Func<GenericPropertyDescriptor<T>, IGenericProperty> se
157157
public IProperty DenseVector(Func<DenseVectorPropertyDescriptor<T>, IDenseVectorProperty> selector) =>
158158
selector?.Invoke(new DenseVectorPropertyDescriptor<T>());
159159

160+
/// <inheritdoc />
161+
public IProperty MatchOnlyText(Func<MatchOnlyTextPropertyDescriptor<T>, IMatchOnlyTextProperty> selector) =>
162+
selector?.Invoke(new MatchOnlyTextPropertyDescriptor<T>());
163+
160164
#pragma warning disable CS3001 // Argument type is not CLS-compliant
161165
public IProperty Scalar(Expression<Func<T, int>> field, Func<NumberPropertyDescriptor<T>, INumberProperty> selector = null) =>
162166
selector.InvokeOrDefault(new NumberPropertyDescriptor<T>().Name(field).Type(NumberType.Integer));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
namespace Nest
6+
{
7+
public class MatchOnlyTextAttribute : ElasticsearchCorePropertyAttributeBase, IMatchOnlyTextProperty {
8+
public MatchOnlyTextAttribute() : base(FieldType.MatchOnlyText) { }
9+
protected MatchOnlyTextAttribute(FieldType fieldType) : base(fieldType) { }
10+
private IMatchOnlyTextProperty Self => this;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System.Diagnostics;
6+
using Nest.Utf8Json;
7+
8+
namespace Nest
9+
{
10+
/// <summary>
11+
/// A variant of text that trades scoring and efficiency of positional queries for space efficiency.
12+
/// This field effectively stores data the same way as a text field that only indexes documents
13+
/// (index_options: docs) and disables norms (norms: false).
14+
/// <para />
15+
/// MatchOnlyText fields are not used for sorting and seldom used for aggregations.
16+
/// </summary>
17+
[InterfaceDataContract]
18+
public interface IMatchOnlyTextProperty : ICoreProperty
19+
{
20+
}
21+
22+
/// <inheritdoc cref="IMatchOnlyTextProperty"/>
23+
[DebuggerDisplay("{DebugDisplay}")]
24+
public class MatchOnlyTextProperty : CorePropertyBase, IMatchOnlyTextProperty
25+
{
26+
public MatchOnlyTextProperty() : base(FieldType.MatchOnlyText) { }
27+
}
28+
29+
/// <inheritdoc cref="IMatchOnlyTextProperty"/>
30+
public class MatchOnlyTextPropertyDescriptor<T>
31+
: CorePropertyDescriptorBase<MatchOnlyTextPropertyDescriptor<T>, IMatchOnlyTextProperty, T>, IMatchOnlyTextProperty
32+
where T : class
33+
{
34+
public MatchOnlyTextPropertyDescriptor() : base(FieldType.MatchOnlyText) { }
35+
}
36+
}

src/Nest/Mapping/Types/FieldType.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ public enum FieldType
170170
/// A dense_vector field stores dense vectors of float values.
171171
/// </summary>
172172
[EnumMember(Value = "dense_vector")]
173-
DenseVector
173+
DenseVector,
174+
175+
/// <summary>
176+
/// A variant of text that trades scoring and efficiency of positional queries for space efficiency.
177+
/// </summary>
178+
[EnumMember(Value = "match_only_text")]
179+
MatchOnlyText,
174180
}
175181
}

src/Nest/Mapping/Types/Properties.cs

+6
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ TReturnType Nested<TChild>(Func<NestedPropertyDescriptor<T, TChild>, INestedProp
157157

158158
/// <inheritdoc cref="IDenseVectorProperty" />
159159
TReturnType DenseVector(Func<DenseVectorPropertyDescriptor<T>, IDenseVectorProperty> selector);
160+
161+
/// <inheritdoc cref="IMatchOnlyTextProperty" />
162+
TReturnType MatchOnlyText(Func<MatchOnlyTextPropertyDescriptor<T>, IMatchOnlyTextProperty> selector);
160163
}
161164

162165
public partial class PropertiesDescriptor<T> where T : class
@@ -229,6 +232,9 @@ public PropertiesDescriptor<T> ConstantKeyword(Func<ConstantKeywordPropertyDescr
229232
/// <inheritdoc cref="ILongRangeProperty" />
230233
public PropertiesDescriptor<T> LongRange(Func<LongRangePropertyDescriptor<T>, ILongRangeProperty> selector) => SetProperty(selector);
231234

235+
/// <inheritdoc cref="IMatchOnlyTextProperty" />
236+
public PropertiesDescriptor<T> MatchOnlyText(Func<MatchOnlyTextPropertyDescriptor<T>, IMatchOnlyTextProperty> selector) => SetProperty(selector);
237+
232238
/// <inheritdoc cref="IMurmur3HashProperty" />
233239
public PropertiesDescriptor<T> Murmur3Hash(Func<Murmur3HashPropertyDescriptor<T>, IMurmur3HashProperty> selector) => SetProperty(selector);
234240

src/Nest/Mapping/Types/PropertyFormatter.cs

+4
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public IProperty Deserialize(ref JsonReader reader, IJsonFormatterResolver forma
9797
case FieldType.Wildcard: return Deserialize<WildcardProperty>(ref segmentReader, formatterResolver);
9898
case FieldType.Version: return Deserialize<VersionProperty>(ref segmentReader, formatterResolver);
9999
case FieldType.DenseVector: return Deserialize<DenseVectorProperty>(ref segmentReader, formatterResolver);
100+
case FieldType.MatchOnlyText: return Deserialize<MatchOnlyTextProperty>(ref segmentReader, formatterResolver);
100101
case FieldType.None:
101102
// no "type" field in the property mapping, or FieldType enum could not be parsed from typeString
102103
return Deserialize<ObjectProperty>(ref segmentReader, formatterResolver);
@@ -223,6 +224,9 @@ public void Serialize(ref JsonWriter writer, IProperty value, IJsonFormatterReso
223224
case IDenseVectorProperty denseVectorProperty:
224225
Serialize(ref writer, denseVectorProperty, formatterResolver);
225226
break;
227+
case IMatchOnlyTextProperty matchOnlyTextProperty:
228+
Serialize(ref writer, matchOnlyTextProperty, formatterResolver);
229+
break;
226230
default:
227231
var formatter = formatterResolver.GetFormatter<object>();
228232
formatter.Serialize(ref writer, value, formatterResolver);

src/Nest/Mapping/Visitor/IMappingVisitor.cs

+4
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public interface IMappingVisitor
7575
void Visit(IVersionProperty property);
7676

7777
void Visit(IDenseVectorProperty property);
78+
79+
void Visit(IMatchOnlyTextProperty property);
7880
}
7981

8082
public class NoopMappingVisitor : IMappingVisitor
@@ -148,5 +150,7 @@ public virtual void Visit(IConstantKeywordProperty property) { }
148150
public virtual void Visit(IVersionProperty property) { }
149151

150152
public virtual void Visit(IDenseVectorProperty property) { }
153+
154+
public virtual void Visit(IMatchOnlyTextProperty property) { }
151155
}
152156
}

src/Nest/Mapping/Visitor/IPropertyVisitor.cs

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public interface IPropertyVisitor
8080

8181
void Visit(IDenseVectorProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute);
8282

83+
void Visit(IMatchOnlyTextProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute);
84+
8385
IProperty Visit(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute);
8486

8587
bool SkipProperty(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute);

src/Nest/Mapping/Visitor/MappingWalker.cs

+6
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ public void Accept(IProperties properties)
285285
_visitor.Visit(t);
286286
});
287287
break;
288+
case FieldType.MatchOnlyText:
289+
Visit<IMatchOnlyTextProperty>(field, t =>
290+
{
291+
_visitor.Visit(t);
292+
});
293+
break;
288294
case FieldType.None:
289295
continue;
290296
}

src/Nest/Mapping/Visitor/NoopPropertyVisitor.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public virtual void Visit(IVersionProperty type, PropertyInfo propertyInfo, Elas
8080

8181
public virtual void Visit(IDenseVectorProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute) { }
8282

83+
public virtual void Visit(IMatchOnlyTextProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute) { }
84+
8385
public virtual IProperty Visit(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute) => null;
8486

8587
public virtual void Visit(IProperty type, PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
@@ -185,8 +187,11 @@ public virtual void Visit(IProperty type, PropertyInfo propertyInfo, Elasticsear
185187
case IVersionProperty version:
186188
Visit(version, propertyInfo, attribute);
187189
break;
188-
case IDenseVectorProperty version:
189-
Visit(version, propertyInfo, attribute);
190+
case IDenseVectorProperty denseVector:
191+
Visit(denseVector, propertyInfo, attribute);
192+
break;
193+
case IMatchOnlyTextProperty matchOnlyText:
194+
Visit(matchOnlyText, propertyInfo, attribute);
190195
break;
191196
}
192197
}

src/Nest/Search/FieldCapabilities/FieldCapabilitiesResponse.cs

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public class FieldTypes : IsADictionaryBase<string, FieldCapabilities>
5656
public FieldCapabilities Keyword => BackingDictionary.TryGetValue("keyword", out var f) ? f : null;
5757
public FieldCapabilities Long => BackingDictionary.TryGetValue("long", out var f) ? f : null;
5858
public FieldCapabilities LongRange => BackingDictionary.TryGetValue("long_range", out var f) ? f : null;
59+
public FieldCapabilities MatchOnlyText => BackingDictionary.TryGetValue("match_only_text", out var f) ? f : null;
5960
public FieldCapabilities Murmur3 => BackingDictionary.TryGetValue("murmur3", out var f) ? f : null;
6061
public FieldCapabilities Parent => BackingDictionary.TryGetValue("_parent", out var f) ? f : null;
6162
public FieldCapabilities ParentJoin => BackingDictionary.TryGetValue("_parent_join", out var f) ? f : null;

tests/Tests/Indices/MappingManagement/GetMapping/GetMappingApiTest.cs

+2
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ internal class TestVisitor : IMappingVisitor
229229

230230
public void Visit(IDenseVectorProperty property) => Increment("dense_vector");
231231

232+
public void Visit(IMatchOnlyTextProperty property) => Increment("match_only_text");
233+
232234
private void Increment(string key)
233235
{
234236
if (!Counts.ContainsKey(key)) Counts.Add(key, 0);

tests/Tests/Mapping/Types/Core/DenseVector/DenseVectorPropertyTests.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
namespace Tests.Mapping.Types.Core.DenseVector
1313
{
1414
[SkipVersion("<7.6.0", "Dense Vector property GA in 7.6.0")]
15-
public class DenseVectorTests : PropertyTestsBase
15+
public class DenseVectorPropertyTests : PropertyTestsBase
1616
{
17-
public DenseVectorTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
17+
public DenseVectorPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
1818

1919
protected override object ExpectJson => new { properties = new { name = new { type = "dense_vector", dims = 2 } } };
2020

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
6+
using Nest;
7+
8+
namespace Tests.Mapping.Types.Core.MatchOnlyText
9+
{
10+
public class MatchOnlyTextTest
11+
{
12+
[MatchOnlyText]
13+
public string MatchOnlyText { get; set; }
14+
}
15+
16+
[SkipVersion("<7.14.0", "Match only text property added in 7.14.0")]
17+
public class MatchOnlyTextAttributeTests : AttributeTestsBase<MatchOnlyTextTest>
18+
{
19+
protected override object ExpectJson => new { properties = new { matchOnlyText = new { type = "match_only_text" } } };
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Licensed to Elasticsearch B.V under one or more agreements.
2+
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3+
// See the LICENSE file in the project root for more information
4+
5+
using System;
6+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
7+
using Nest;
8+
using Tests.Core.ManagedElasticsearch.Clusters;
9+
using Tests.Domain;
10+
using Tests.Framework.EndpointTests.TestState;
11+
12+
namespace Tests.Mapping.Types.Core.MatchOnlyText
13+
{
14+
[SkipVersion("<7.14.0", "Match only text property added in 7.14.0")]
15+
public class MatchOnlyTextPropertyTests : PropertyTestsBase
16+
{
17+
public MatchOnlyTextPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
18+
19+
protected override object ExpectJson => new { properties = new { name = new { type = "match_only_text" } } };
20+
21+
protected override Func<PropertiesDescriptor<Project>, IPromise<IProperties>> FluentProperties => f => f
22+
.MatchOnlyText(s => s
23+
.Name(p => p.Name)
24+
);
25+
26+
protected override IProperties InitializerProperties => new Properties { { "name", new MatchOnlyTextProperty() } };
27+
}
28+
}

0 commit comments

Comments
 (0)