From 0648a72c1f06488c3433b6c3f8b661509ec895c1 Mon Sep 17 00:00:00 2001 From: Zahraa Chreim Date: Thu, 12 Dec 2024 13:52:11 +0200 Subject: [PATCH] 116402: Adjust DSpaceControlledVocabulary#getBestMatch --- .../authority/DSpaceControlledVocabulary.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java b/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java index d82c40a25320..7d69f81d1279 100644 --- a/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java +++ b/dspace-api/src/main/java/org/dspace/content/authority/DSpaceControlledVocabulary.java @@ -206,19 +206,25 @@ public Choices getBestMatch(String text, String locale) { String xpathExpression = ""; String[] textHierarchy = text.split(hierarchyDelimiter, -1); for (int i = 0; i < textHierarchy.length; i++) { - String formattedText = escapeQuotes(textHierarchy[i]); - xpathExpression += String.format(labelTemplate, formattedText); + xpathExpression += String.format(valueTemplate, textHierarchy[i]); } XPath xpath = XPathFactory.newInstance().newXPath(); List choices = new ArrayList(); try { NodeList results = (NodeList) xpath.evaluate(xpathExpression, vocabulary, XPathConstants.NODESET); - choices = getChoicesFromNodeList(results, 0, 1); + if (results.getLength() > 0) { + // If an exact match is found by ID, return it with confidence = CF_UNCERTAIN + choices = getChoicesFromNodeList(results, 0, 1); + return new Choices(choices.toArray(new Choice[choices.size()]), 0, choices.size(), Choices.CF_UNCERTAIN, + false); + } } catch (XPathExpressionException e) { log.warn(e.getMessage(), e); return new Choices(true); } - return new Choices(choices.toArray(new Choice[choices.size()]), 0, choices.size(), Choices.CF_AMBIGUOUS, false); + // If no exact match found, fall back to searching by label using getMatches + log.debug("No exact ID match found, falling back to label search"); + return getMatches(text, 0, 1, locale); // This will return a result based on the label, if any } /**