Skip to content

Commit 51a78ca

Browse files
renatohepugh
andauthored
ensure original token is not set to type:SYNONYM in SGF (#4960)
Co-authored-by: Eric Pugh <epugh@opensourceconnections.com>
1 parent 7892348 commit 51a78ca

3 files changed

Lines changed: 49 additions & 6 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# See https://github.com/apache/solr/blob/main/dev-docs/changelog.adoc
2+
title: ManagedSynonymGraphFilterFactory no longer tags the original token as type `SYNONYM` for equivalence-group mappings (e.g. `[terma]->[terma, termb, termc]`), using Lucene's `includeOrig` flag so only the injected synonyms get that type.
3+
type: fixed
4+
authors:
5+
- name: Renato Haeberli
6+
nick: renatoh
7+
links:
8+
- name: PR#4960
9+
url: https://github.com/apache/solr/pull/4960

‎solr/core/src/java/org/apache/solr/rest/schema/analysis/ManagedSynonymGraphFilterFactory.java‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,24 @@ public void parse(Reader in) throws IOException, ParseException {
365365
boolean ignoreCase = synonymManager.getIgnoreCase();
366366
for (CasePreservedSynonymMappings cpsm : synonymManager.synonymMappings.values()) {
367367
for (Map.Entry<String, Set<String>> entry : cpsm.mappings.entrySet()) {
368+
// Use includeOrig=true so the original token keeps its upstream type and only the
369+
// injected synonyms are tagged as SYNONYM
370+
boolean mapsToItself = entry.getValue().contains(entry.getKey());
371+
CharsRef casedTerm =
372+
analyze(
373+
synonymManager.applyCaseSetting(ignoreCase, entry.getKey()),
374+
new CharsRefBuilder());
368375
for (String mapping : entry.getValue()) {
369-
// apply the case setting to match the behavior of the SynonymMap builder
370-
CharsRef casedTerm =
371-
analyze(
372-
synonymManager.applyCaseSetting(ignoreCase, entry.getKey()),
373-
new CharsRefBuilder());
374376
CharsRef casedMapping =
375377
analyze(
376378
synonymManager.applyCaseSetting(ignoreCase, mapping), new CharsRefBuilder());
377-
add(casedTerm, casedMapping, false);
379+
if (mapsToItself) {
380+
if (!casedTerm.equals(casedMapping)) {
381+
add(casedTerm, casedMapping, true);
382+
}
383+
} else {
384+
add(casedTerm, casedMapping, false);
385+
}
378386
}
379387
}
380388
}

‎solr/core/src/test/org/apache/solr/rest/schema/analysis/TestManagedSynonymGraphFilterFactory.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,32 @@ public void testManagedSynonyms() throws Exception {
247247
assertJDelete(endpoint + "/cheerful/joyful", "/error/code==404");
248248
}
249249

250+
@Test
251+
public void testUpstreamTokenTypeIsPreserved() throws Exception {
252+
String endpoint = "/schema/analysis/synonyms/englishgraph";
253+
254+
List<String> equivGroup = Arrays.asList("funny", "entertaining", "whimsical");
255+
assertJPut(endpoint, toJSONString(equivGroup), "/responseHeader/status==0");
256+
257+
assertJQ(endpoint + "/funny", "/funny==['entertaining','funny','whimsical']");
258+
259+
restTestHarness.reload();
260+
261+
String analysisUrl = "/analysis/field?analysis.fieldtype=managed_graph_en&analysis.query=funny";
262+
String synFilter = "//arr[contains(@name,'SynonymGraphFilter')]";
263+
assertQ(
264+
analysisUrl,
265+
"count("
266+
+ synFilter
267+
+ "/lst[str[@name='text'][.='funny']]/str[@name='type'][.='<ALPHANUM>']) = 1",
268+
"count("
269+
+ synFilter
270+
+ "/lst[str[@name='text'][.='entertaining']]/str[@name='type'][.='SYNONYM']) = 1",
271+
"count("
272+
+ synFilter
273+
+ "/lst[str[@name='text'][.='whimsical']]/str[@name='type'][.='SYNONYM']) = 1");
274+
}
275+
250276
/** Can we add and remove stopwords with umlauts */
251277
@Test
252278
public void testCanHandleDecodingAndEncodingForSynonyms() throws Exception {

0 commit comments

Comments
 (0)