@@ -184,9 +184,6 @@ func (h *OperatorHandlerImpl) AddSearchAttributes(
184
184
if searchattribute .IsReserved (saName ) {
185
185
return nil , serviceerror .NewInvalidArgument (fmt .Sprintf (errSearchAttributeIsReservedMessage , saName ))
186
186
}
187
- if currentSearchAttributes .IsDefined (saName ) {
188
- return nil , serviceerror .NewAlreadyExist (fmt .Sprintf (errSearchAttributeAlreadyExistsMessage , saName ))
189
- }
190
187
if _ , ok := enumspb .IndexedValueType_name [int32 (saType )]; ! ok {
191
188
return nil , serviceerror .NewInvalidArgument (fmt .Sprintf (errUnknownSearchAttributeTypeMessage , saType ))
192
189
}
@@ -198,24 +195,39 @@ func (h *OperatorHandlerImpl) AddSearchAttributes(
198
195
// `skip-schema-update` is set. This is for backward compatibility using
199
196
// standard visibility.
200
197
if h .visibilityMgr .GetName () == elasticsearch .PersistenceName || indexName == "" {
201
- err = h .addSearchAttributesElasticsearch (ctx , request , indexName )
198
+ err = h .addSearchAttributesElasticsearch (ctx , request , indexName , currentSearchAttributes )
199
+ if err != nil {
200
+ if _ , isWorkflowErr := err .(* serviceerror.SystemWorkflow ); isWorkflowErr {
201
+ scope .Counter (metrics .AddSearchAttributesWorkflowFailuresCount .GetMetricName ()).Record (1 )
202
+ }
203
+ } else {
204
+ scope .Counter (metrics .AddSearchAttributesWorkflowSuccessCount .GetMetricName ()).Record (1 )
205
+ }
202
206
} else {
203
207
err = h .addSearchAttributesSQL (ctx , request , currentSearchAttributes )
204
208
}
205
209
206
210
if err != nil {
207
- scope .Counter (metrics .AddSearchAttributesWorkflowFailuresCount .GetMetricName ()).Record (1 )
208
211
return nil , err
209
212
}
210
- scope .Counter (metrics .AddSearchAttributesWorkflowSuccessCount .GetMetricName ()).Record (1 )
211
213
return & operatorservice.AddSearchAttributesResponse {}, nil
212
214
}
213
215
214
216
func (h * OperatorHandlerImpl ) addSearchAttributesElasticsearch (
215
217
ctx context.Context ,
216
218
request * operatorservice.AddSearchAttributesRequest ,
217
219
indexName string ,
220
+ currentSearchAttributes searchattribute.NameTypeMap ,
218
221
) error {
222
+ // Check if custom search attribute already exists in cluster metadata.
223
+ // This check is not needed in SQL DB because no custom search attributes
224
+ // are pre-allocated, and only aliases are created.
225
+ for saName := range request .GetSearchAttributes () {
226
+ if currentSearchAttributes .IsDefined (saName ) {
227
+ return serviceerror .NewAlreadyExist (fmt .Sprintf (errSearchAttributeAlreadyExistsMessage , saName ))
228
+ }
229
+ }
230
+
219
231
// Execute workflow.
220
232
wfParams := addsearchattributes.WorkflowParams {
221
233
CustomAttributesToAdd : request .GetSearchAttributes (),
0 commit comments