Skip to content

Commit 3571b57

Browse files
committed
Add GenericProperty.Indexed to support bool? values (#3225)
This commit adds GenericProperty.Indexed to allow a bool? value to be specified for the field's "index" value, in line with other properties. Deprecate the usage of GenericProperty.Index and point users to users GenericProperty.Indexed. If a user does continue to use GenericProperty.Index however, set a value of true for FieldIndexOption.Analyzed and FieldIndexOption.NotAnalyzed, false for FieldIndexOption.No, and null for null. GenericProperty.Index will be updated to type bool? in 7.x Fixes #3103 (cherry picked from commit 79caff0)
1 parent cb536fb commit 3571b57

File tree

3 files changed

+67
-9
lines changed

3 files changed

+67
-9
lines changed

src/Nest/Mapping/Types/Specialized/Generic/GenericProperty.cs

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ namespace Nest
1111
[JsonObject(MemberSerialization.OptIn)]
1212
public interface IGenericProperty : IDocValuesProperty
1313
{
14-
[JsonProperty("index")]
14+
[JsonIgnore]
15+
[Obsolete("Please use Indexed. Will be fixed in NEST 7.x")]
1516
FieldIndexOption? Index { get; set; }
1617

18+
[JsonProperty("index")]
19+
bool? Indexed { get; set; }
20+
1721
[JsonProperty("term_vector")]
1822
TermVectorOption? TermVector { get; set; }
1923

@@ -56,6 +60,8 @@ public interface IGenericProperty : IDocValuesProperty
5660
[DebuggerDisplay("{DebugDisplay}")]
5761
public class GenericProperty : DocValuesPropertyBase, IGenericProperty
5862
{
63+
private FieldIndexOption? _index;
64+
5965
#pragma warning disable 618
6066
public GenericProperty() : base(null) { }
6167
#pragma warning restore 618
@@ -68,7 +74,31 @@ public GenericProperty() : base(null) { }
6874
public int? IgnoreAbove { get; set; }
6975
public int? PositionIncrementGap { get; set; }
7076
public IStringFielddata Fielddata { get; set; }
71-
public FieldIndexOption? Index { get; set; }
77+
78+
[Obsolete("Please use Indexed. Will be fixed in NEST 7.x")]
79+
public FieldIndexOption? Index
80+
{
81+
get => _index;
82+
set
83+
{
84+
_index = value;
85+
switch (_index)
86+
{
87+
case FieldIndexOption.Analyzed:
88+
case FieldIndexOption.NotAnalyzed:
89+
Indexed = true;
90+
break;
91+
case FieldIndexOption.No:
92+
Indexed = false;
93+
break;
94+
default:
95+
Indexed = null;
96+
break;
97+
}
98+
}
99+
}
100+
101+
public bool? Indexed { get; set; }
72102
public string NullValue { get; set; }
73103
public bool? Norms { get; set; }
74104
public IndexOptions? IndexOptions { get; set; }
@@ -85,7 +115,31 @@ public class GenericPropertyDescriptor<T>
85115
: DocValuesPropertyDescriptorBase<GenericPropertyDescriptor<T>, IGenericProperty, T>, IGenericProperty
86116
where T : class
87117
{
88-
FieldIndexOption? IGenericProperty.Index { get; set; }
118+
private FieldIndexOption? _index;
119+
120+
FieldIndexOption? IGenericProperty.Index
121+
{
122+
get => _index;
123+
set
124+
{
125+
_index = value;
126+
switch (_index)
127+
{
128+
case FieldIndexOption.Analyzed:
129+
case FieldIndexOption.NotAnalyzed:
130+
Self.Indexed = true;
131+
break;
132+
case FieldIndexOption.No:
133+
Self.Indexed = false;
134+
break;
135+
default:
136+
Self.Indexed = null;
137+
break;
138+
}
139+
}
140+
}
141+
142+
bool? IGenericProperty.Indexed { get; set; }
89143
TermVectorOption? IGenericProperty.TermVector { get; set; }
90144
double? IGenericProperty.Boost { get; set; }
91145
string IGenericProperty.NullValue { get; set; }
@@ -105,15 +159,19 @@ public GenericPropertyDescriptor() : base(null) { }
105159

106160
public GenericPropertyDescriptor<T> Type(string type) => Assign(a => a.Type = type);
107161

162+
[Obsolete("Please use the overload that accepts bool?. Will be fixed in NEST 7.x")]
108163
public GenericPropertyDescriptor<T> Index(FieldIndexOption? index = FieldIndexOption.NotAnalyzed) => Assign(a => a.Index = index);
109164

165+
public GenericPropertyDescriptor<T> Index(bool? index = true) => Assign(a => a.Indexed = index);
166+
110167
public GenericPropertyDescriptor<T> Boost(double boost) => Assign(a => a.Boost = boost);
111168

112169
public GenericPropertyDescriptor<T> NullValue(string nullValue) => Assign(a => a.NullValue = nullValue);
113170

114171
/// <remarks>Removed in 6.x</remarks>
115172
public GenericPropertyDescriptor<T> IncludeInAll(bool includeInAll = true) => Assign(a => a.IncludeInAll = includeInAll);
116173

174+
[Obsolete("Deprecated. Will be removed in NEST 7.x")]
117175
public GenericPropertyDescriptor<T> NotAnalyzed() => Index(FieldIndexOption.NotAnalyzed);
118176

119177
public GenericPropertyDescriptor<T> Index(FieldIndexOption index) => Assign(a => a.Index = index);

src/Tests/Indices/IndexSettings/IndexTemplates/PutIndexTemplate/PutIndexTemplateApiTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected override LazyResponses ClientUsage() => Calls(
5252
match_mapping_type = "*",
5353
mapping = new
5454
{
55-
index = "no"
55+
index = false
5656
}
5757
}
5858
}
@@ -78,7 +78,7 @@ protected override LazyResponses ClientUsage() => Calls(
7878
.MatchMappingType("*")
7979
.Mapping(mm => mm
8080
.Generic(g => g
81-
.Index(FieldIndexOption.No)
81+
.Index(false)
8282
)
8383
)
8484
)
@@ -112,7 +112,7 @@ protected override LazyResponses ClientUsage() => Calls(
112112
MatchMappingType = "*",
113113
Mapping = new GenericProperty
114114
{
115-
Index = FieldIndexOption.No
115+
Indexed = false
116116
}
117117
}
118118
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ public class GenericPropertyTests : SingleMappingPropertyTestsBase
1010
private const string GenericType = "{dynamic_type}";
1111
public GenericPropertyTests(WritableCluster cluster, EndpointUsage usage) : base(cluster, usage) { }
1212

13-
protected override object SingleMappingJson { get; } = new {index = "no", type= GenericType};
13+
protected override object SingleMappingJson { get; } = new {index = false, type= GenericType};
1414

1515
protected override Func<SingleMappingDescriptor<object>, IProperty> FluentSingleMapping => m => m
1616
.Generic(g => g
1717
.Type(GenericType)
18-
.Index(FieldIndexOption.No)
18+
.Index(false)
1919
);
2020

2121
protected override IProperty InitializerSingleMapping { get; } = new GenericProperty
2222
{
2323
Type = GenericType,
24-
Index = FieldIndexOption.No
24+
Indexed = false
2525
};
2626
}
2727
}

0 commit comments

Comments
 (0)