Skip to content

Commit a77c9a4

Browse files
committed
Use lenient to ignore non existing synonyms sets
1 parent 0405fb0 commit a77c9a4

File tree

4 files changed

+177
-4
lines changed

4 files changed

+177
-4
lines changed

modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/SynonymTokenFilterFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public ReaderWithOrigin getRulesReader(SynonymTokenFilterFactory factory, IndexC
7272
);
7373
} else {
7474
reader = new ReaderWithOrigin(
75-
Analysis.getReaderFromIndex(synonymsSet, factory.synonymsManagementAPIService),
75+
Analysis.getReaderFromIndex(synonymsSet, factory.synonymsManagementAPIService, factory.lenient),
7676
"[" + synonymsSet + "] synonyms_set in .synonyms index",
7777
synonymsSet
7878
);

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/synonyms/110_synonyms_invalid.yml

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,158 @@ setup:
313313
indices.stats: { index: test_index }
314314

315315
- length: { indices: 0 }
316+
317+
---
318+
"Load index with non existent synonyms set":
319+
- requires:
320+
test_runner_features: [ capabilities ]
321+
capabilities:
322+
- method: PUT
323+
path: /{index}
324+
capabilities: [ synonyms_set_lenient_on_non_existing ]
325+
reason: "requires synonyms_set_lenient_on_non_existing bug fix"
326+
- do:
327+
indices.create:
328+
index: test_index
329+
body:
330+
settings:
331+
index:
332+
number_of_shards: 1
333+
number_of_replicas: 0
334+
analysis:
335+
filter:
336+
my_synonym_filter:
337+
type: synonym
338+
synonyms_set: set1
339+
updateable: true
340+
analyzer:
341+
my_analyzer:
342+
type: custom
343+
tokenizer: whitespace
344+
filter: [ lowercase, my_synonym_filter ]
345+
mappings:
346+
properties:
347+
my_field:
348+
type: text
349+
search_analyzer: my_analyzer
350+
351+
- match: { acknowledged: true }
352+
- match: { shards_acknowledged: true }
353+
354+
- do:
355+
indices.stats: { index: test_index }
356+
357+
- match: { indices.test_index.health: "green" }
358+
359+
# Synonyms are not applied
360+
- do:
361+
indices.analyze:
362+
index: test_index
363+
body:
364+
analyzer: my_analyzer
365+
text: foo
366+
367+
- length: { tokens: 1 }
368+
- match: { tokens.0.token: foo }
369+
370+
371+
# Create synonyms set and check synonyms are applied
372+
- do:
373+
synonyms.put_synonym:
374+
id: set1
375+
body:
376+
synonyms_set:
377+
synonyms: "foo => bar, baz"
378+
379+
# This is to ensure that all index shards (write and read) are available. In serverless this can take some time.
380+
- do:
381+
cluster.health:
382+
index: .synonyms
383+
wait_for_status: green
384+
385+
386+
- do:
387+
indices.stats: { index: test_index }
388+
389+
- match: { indices.test_index.health: "green" }
390+
391+
# Synonyms are applied
392+
- do:
393+
indices.analyze:
394+
index: test_index
395+
body:
396+
analyzer: my_analyzer
397+
text: foo
398+
399+
- length: { tokens: 2 }
400+
401+
---
402+
"Load index with non existent synonyms set and lenient set to false":
403+
404+
- do:
405+
indices.create:
406+
index: test_index
407+
body:
408+
settings:
409+
index:
410+
number_of_shards: 1
411+
number_of_replicas: 0
412+
analysis:
413+
filter:
414+
my_synonym_filter:
415+
type: synonym
416+
synonyms_set: set1
417+
updateable: true
418+
lenient: false
419+
analyzer:
420+
my_analyzer:
421+
type: custom
422+
tokenizer: whitespace
423+
filter: [ lowercase, my_synonym_filter ]
424+
mappings:
425+
properties:
426+
my_field:
427+
type: text
428+
search_analyzer: my_analyzer
429+
430+
- match: { acknowledged: true }
431+
- match: { shards_acknowledged: false }
432+
433+
- do:
434+
indices.stats: { index: test_index }
435+
436+
- length: { indices: 0 }
437+
438+
# Create synonyms set and check synonyms are applied
439+
- do:
440+
synonyms.put_synonym:
441+
id: set1
442+
body:
443+
synonyms_set:
444+
synonyms: "foo => bar, baz"
445+
446+
# This is to ensure that all index shards (write and read) are available. In serverless this can take some time.
447+
- do:
448+
cluster.health:
449+
index: .synonyms
450+
wait_for_status: green
451+
452+
- do:
453+
cluster.reroute:
454+
retry_failed: true
455+
456+
- do:
457+
cluster.health:
458+
index: test_index
459+
wait_for_status: green
460+
461+
# Synonyms are applied
462+
- do:
463+
indices.analyze:
464+
index: test_index
465+
body:
466+
analyzer: my_analyzer
467+
text: foo
468+
469+
- length: { tokens: 2 }
470+

server/src/main/java/org/elasticsearch/index/analysis/Analysis.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
import org.apache.lucene.analysis.th.ThaiAnalyzer;
4949
import org.apache.lucene.analysis.tr.TurkishAnalyzer;
5050
import org.apache.lucene.analysis.util.CSVUtil;
51+
import org.elasticsearch.ElasticsearchException;
52+
import org.elasticsearch.ResourceNotFoundException;
5153
import org.elasticsearch.action.support.PlainActionFuture;
5254
import org.elasticsearch.common.Strings;
5355
import org.elasticsearch.common.settings.Settings;
@@ -353,10 +355,23 @@ public static Reader getReaderFromFile(Environment env, String filePath, String
353355
}
354356
}
355357

356-
public static Reader getReaderFromIndex(String synonymsSet, SynonymsManagementAPIService synonymsManagementAPIService) {
358+
public static Reader getReaderFromIndex(String synonymsSet, SynonymsManagementAPIService synonymsManagementAPIService, boolean ignoreMissing) {
357359
final PlainActionFuture<PagedResult<SynonymRule>> synonymsLoadingFuture = new PlainActionFuture<>();
358360
synonymsManagementAPIService.getSynonymSetRules(synonymsSet, synonymsLoadingFuture);
359-
PagedResult<SynonymRule> results = synonymsLoadingFuture.actionGet();
361+
362+
PagedResult<SynonymRule> results;
363+
try {
364+
results = synonymsLoadingFuture.actionGet();
365+
} catch (ResourceNotFoundException e) {
366+
if (ignoreMissing == false) {
367+
throw e;
368+
}
369+
logger.info(
370+
"Synonyms set {} not found - synonyms will not be applied to search results on indices that use this synonym set",
371+
synonymsSet
372+
);
373+
results = new PagedResult<>(0, new SynonymRule[0]);
374+
}
360375

361376
SynonymRule[] synonymRules = results.pageResults();
362377
StringBuilder sb = new StringBuilder();

server/src/main/java/org/elasticsearch/rest/action/admin/indices/CreateIndexCapabilities.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ public class CreateIndexCapabilities {
3232

3333
private static final String HUNSPELL_DICT_400 = "hunspell_dict_400";
3434

35+
private static final String SYNONYMS_SET_LENIENT_ON_NON_EXISTING = "synonyms_set_lenient_on_non_existing";
36+
3537
public static final Set<String> CAPABILITIES = Set.of(
3638
LOGSDB_INDEX_MODE_CAPABILITY,
3739
LOOKUP_INDEX_MODE_CAPABILITY,
3840
NESTED_DENSE_VECTOR_SYNTHETIC_TEST,
3941
POORLY_FORMATTED_BAD_REQUEST,
40-
HUNSPELL_DICT_400
42+
HUNSPELL_DICT_400,
43+
SYNONYMS_SET_LENIENT_ON_NON_EXISTING
4144
);
4245
}

0 commit comments

Comments
 (0)