Skip to content

Commit 30dab4e

Browse files
authored
Add Search as you type to dynamic templates (#4259)
This commit adds Search as you type property configuration to dynamic templates. Update SingleMappingPropertyTestsBase to use Writable cluster as it creates a new index template. Closes #4249
1 parent 0a53096 commit 30dab4e

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

src/Nest/Mapping/DynamicTemplate/SingleMapping.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,5 +319,9 @@ public IProperty Scalar(Expression<Func<T, FloatRange>> field, Func<FloatRangePr
319319
public IProperty Scalar(Expression<Func<T, IpAddressRange>> field, Func<IpRangePropertyDescriptor<T>, IIpRangeProperty> selector = null) =>
320320
selector.InvokeOrDefault(new IpRangePropertyDescriptor<T>().Name(field));
321321
#pragma warning restore CS3001 // Argument type is not CLS-compliant
322+
323+
/// <inheritdoc cref="ISearchAsYouTypeProperty"/>
324+
public IProperty SearchAsYouType(Func<SearchAsYouTypePropertyDescriptor<T>, ISearchAsYouTypeProperty> selector) =>
325+
selector?.Invoke(new SearchAsYouTypePropertyDescriptor<T>());
322326
}
323327
}

src/Nest/Mapping/Types/Properties.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ public PropertiesDescriptor<T> Object<TChild>(Func<ObjectTypeDescriptor<T, TChil
158158

159159
public PropertiesDescriptor<T> Text(Func<TextPropertyDescriptor<T>, ITextProperty> selector) => SetProperty(selector);
160160

161+
/// <inheritdoc cref="ISearchAsYouTypeProperty"/>
161162
public PropertiesDescriptor<T> SearchAsYouType(Func<SearchAsYouTypePropertyDescriptor<T>, ISearchAsYouTypeProperty> selector) => SetProperty(selector);
162163

163164
public PropertiesDescriptor<T> TokenCount(Func<TokenCountPropertyDescriptor<T>, ITokenCountProperty> selector) => SetProperty(selector);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using System;
2+
using Elastic.Xunit.XunitPlumbing;
3+
using Nest;
4+
using Tests.Core.ManagedElasticsearch.Clusters;
5+
using Tests.Domain;
6+
using Tests.Framework.EndpointTests.TestState;
7+
8+
namespace Tests.Mapping.Types.Core.SearchAsYouType
9+
{
10+
[SkipVersion("<7.2.0", "Implemented in 7.2.0")]
11+
public class SearchAsYouTypeSingleMappingPropertyTests : SingleMappingPropertyTestsBase
12+
{
13+
public SearchAsYouTypeSingleMappingPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
14+
15+
protected override object SingleMappingJson => new
16+
{
17+
max_shingle_size = 4,
18+
type = "search_as_you_type",
19+
analyzer = "standard",
20+
copy_to = new[] { "other_field" },
21+
index = true,
22+
index_options = "offsets",
23+
search_analyzer = "standard",
24+
search_quote_analyzer = "standard",
25+
similarity = "BM25",
26+
store = true,
27+
norms = false,
28+
term_vector = "with_positions_offsets"
29+
};
30+
31+
protected override Func<SingleMappingSelector<object>, IProperty> FluentSingleMapping => f => f
32+
.SearchAsYouType(s => s
33+
.MaxShingleSize(4)
34+
.Analyzer("standard")
35+
.CopyTo(c => c
36+
.Field("other_field")
37+
)
38+
.Index()
39+
.IndexOptions(IndexOptions.Offsets)
40+
.SearchAnalyzer("standard")
41+
.SearchQuoteAnalyzer("standard")
42+
.Similarity("BM25")
43+
.Store()
44+
.Norms(false)
45+
.TermVector(TermVectorOption.WithPositionsOffsets)
46+
);
47+
48+
49+
protected override IProperty InitializerSingleMapping =>
50+
new SearchAsYouTypeProperty
51+
{
52+
MaxShingleSize = 4,
53+
Analyzer = "standard",
54+
CopyTo = "other_field",
55+
Index = true,
56+
IndexOptions = IndexOptions.Offsets,
57+
SearchAnalyzer = "standard",
58+
SearchQuoteAnalyzer = "standard",
59+
Similarity = "BM25",
60+
Store = true,
61+
Norms = false,
62+
TermVector = TermVectorOption.WithPositionsOffsets
63+
};
64+
}
65+
}

src/Tests/Tests/Mapping/Types/SingleMappingPropertyTestsBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace Tests.Mapping.Types
1010
{
1111
public abstract class SingleMappingPropertyTestsBase
12-
: ApiIntegrationTestBase<ReadOnlyCluster, PutIndexTemplateResponse, IPutIndexTemplateRequest, PutIndexTemplateDescriptor,
12+
: ApiIntegrationTestBase<WritableCluster, PutIndexTemplateResponse, IPutIndexTemplateRequest, PutIndexTemplateDescriptor,
1313
PutIndexTemplateRequest>
1414
{
15-
protected SingleMappingPropertyTestsBase(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
15+
protected SingleMappingPropertyTestsBase(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
1616

1717
protected override bool ExpectIsValid => true;
1818

src/Tests/Tests/Mapping/Types/Specialized/Generic/GenericPropertyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class GenericPropertyTests : SingleMappingPropertyTestsBase
99
{
1010
private const string GenericType = "{dynamic_type}";
1111

12-
public GenericPropertyTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
12+
public GenericPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
1313

1414
protected override Func<SingleMappingSelector<object>, IProperty> FluentSingleMapping => m => m
1515
.Generic(g => g

0 commit comments

Comments
 (0)