From c15078ffff7399b19560ee76050707abe131fe60 Mon Sep 17 00:00:00 2001 From: Daniele Antuzi Date: Thu, 27 Apr 2023 11:59:11 +0200 Subject: [PATCH] Set word2vec getSynonyms method synchronized. This is a quick mitigation to avoid unexpected behaviour if multiple queries are executed concurrently. This commit wants to address some comments received in the PR #12169 --- .../synonym/word2vec/Word2VecSynonymProvider.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java index 1d1f06fc991f..0f0394280528 100644 --- a/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java +++ b/lucene/analysis/common/src/java/org/apache/lucene/analysis/synonym/word2vec/Word2VecSynonymProvider.java @@ -42,6 +42,7 @@ public class Word2VecSynonymProvider { private static final VectorSimilarityFunction SIMILARITY_FUNCTION = VectorSimilarityFunction.DOT_PRODUCT; private static final VectorEncoding VECTOR_ENCODING = VectorEncoding.FLOAT32; + private static final int NO_LIMIT_ON_VISITED_NODES = Integer.MAX_VALUE; private final Word2VecModel word2VecModel; private final HnswGraph hnswGraph; @@ -64,7 +65,15 @@ public Word2VecSynonymProvider(Word2VecModel model) throws IOException { this.hnswGraph = builder.build(word2VecModel.copy()); } - public List getSynonyms( + /** + * Returns the list of synonyms of a provided term. This method is synchronized because it uses + * the {@link org.apache.lucene.util.hnsw.OnHeapHnswGraph} that is not thread-safe. + * + * @param term term to search to find synonyms + * @param maxSynonymsPerTerm limit of synonyms returned + * @param minAcceptedSimilarity lower similarity threshold to consider another term as synonym + */ + public synchronized List getSynonyms( BytesRef term, int maxSynonymsPerTerm, float minAcceptedSimilarity) throws IOException { if (term == null) { @@ -85,7 +94,7 @@ public List getSynonyms( SIMILARITY_FUNCTION, hnswGraph, null, - word2VecModel.size()); + NO_LIMIT_ON_VISITED_NODES); int size = synonyms.size(); for (int i = 0; i < size; i++) {