Skip to content

Commit 90ff011

Browse files
github-actions[bot]Mpdreamzfate1009
authored
[Backport 7.10] Allow custom tokenizers to be serialized fully (#5068)
Co-authored-by: fate1009 <gzx@0553tao.com> Co-authored-by: Martijn Laarman <Mpdreamz@gmail.com> Co-authored-by: fate1009 <gzx@0553tao.com>
1 parent a1559f3 commit 90ff011

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

src/Nest/Analysis/Tokenizers/TokenizerFormatter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ public void Serialize(ref JsonWriter writer, ITokenizer value, IJsonFormatterRes
119119
Serialize<INoriTokenizer>(ref writer, value, formatterResolver);
120120
break;
121121
default:
122-
var formatter = DynamicObjectResolver.ExcludeNullCamelCase.GetFormatter<ITokenizer>();
122+
// serialize user defined tokenizer
123+
var formatter = formatterResolver.GetFormatter<object>();
123124
formatter.Serialize(ref writer, value, formatterResolver);
124125
break;
125126
}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 System.Collections.Generic;
7+
using System.Runtime.Serialization;
8+
using Elastic.Elasticsearch.Xunit.XunitPlumbing;
9+
using FluentAssertions;
10+
using Nest;
11+
using Tests.Core.Client;
12+
using static Tests.Core.Serialization.SerializationTestHelper;
13+
14+
namespace Tests.Reproduce
15+
{
16+
public class GithubPR5039
17+
{
18+
public class MyCustomTokenizer : ITokenizer
19+
{
20+
public string Type => "my_custom_tok";
21+
public string Version { get; set; }
22+
23+
public string Y { get; set; }
24+
}
25+
26+
[U]
27+
public void CustomTokenizer()
28+
{
29+
var tokenizer = Object(new MyCustomTokenizer() { Version = "x", Y = "z" })
30+
.RoundTrips(new { type = "my_custom_tok", version = "x", y = "z" });
31+
tokenizer.Type.Should().Be("my_custom_tok");
32+
tokenizer.Version.Should().Be("x");
33+
tokenizer.Y.Should().Be("z");
34+
}
35+
36+
public class DynamicSynonymTokenFilter : ITokenFilter
37+
{
38+
public bool? Expand { get; set; }
39+
public SynonymFormat? Format { get; set; }
40+
public bool? Lenient { get; set; }
41+
public IEnumerable<string> Synonyms { get; set; }
42+
43+
[DataMember(Name = "synonyms_path")]
44+
public string SynonymsPath { get; set; }
45+
46+
public string Tokenizer { get; set; }
47+
public bool? Updateable { get; set; }
48+
public string Type { get; } = "dynamic_synonym";
49+
public string Version { get; set; }
50+
public int? Interval { get; set; }
51+
}
52+
53+
[U]
54+
public void CustomTokenFilter()
55+
{
56+
var tokenizer = Object(new DynamicSynonymTokenFilter() { Version = "x", SynonymsPath = "/root/access" })
57+
.RoundTrips(new { type = "dynamic_synonym", version = "x", synonyms_path = "/root/access" });
58+
tokenizer.Type.Should().Be("dynamic_synonym");
59+
tokenizer.Version.Should().Be("x");
60+
tokenizer.SynonymsPath.Should().Be("/root/access");
61+
}
62+
63+
[U]
64+
public void CreateIndex()
65+
{
66+
var client = TestClient.DefaultInMemoryClient;
67+
68+
var response = client.Indices.Create("my-index", i => i
69+
.Settings(s => s
70+
.Analysis(a => a
71+
.TokenFilters(t => t
72+
.UserDefined("mytf",
73+
new DynamicSynonymTokenFilter
74+
{
75+
SynonymsPath = "https://my-synonym-server-url-that-not-is-relevant",
76+
Updateable = true,
77+
Lenient = true,
78+
Interval = 60
79+
})
80+
)
81+
.Tokenizers(t => t
82+
.UserDefined("myt", new MyCustomTokenizer { Y = "yy" })
83+
)
84+
)
85+
)
86+
);
87+
88+
Expect(new
89+
{
90+
settings = new
91+
{
92+
analysis = new
93+
{
94+
filter = new
95+
{
96+
mytf = new
97+
{
98+
lenient = true,
99+
synonyms_path = "https://my-synonym-server-url-that-not-is-relevant",
100+
updateable = true,
101+
type = "dynamic_synonym",
102+
interval = 60
103+
}
104+
},
105+
tokenizer = new { myt = new { type = "my_custom_tok", y = "yy" } }
106+
}
107+
}
108+
})
109+
.NoRoundTrip()
110+
.FromRequest(response);
111+
}
112+
}
113+
}

0 commit comments

Comments
 (0)