From f909c34e30e3b816a09d3bf8d8f88f743350282d Mon Sep 17 00:00:00 2001 From: haider Date: Fri, 18 Oct 2024 15:26:38 +0100 Subject: [PATCH] - Update exact match logic --- .../controller/api/v1/SolrFieldMapper.java | 10 ++++----- .../controller/api/v1/V1SearchController.java | 21 ++++++++++--------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/SolrFieldMapper.java b/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/SolrFieldMapper.java index 74c6dc10b..fba40164b 100644 --- a/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/SolrFieldMapper.java +++ b/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/SolrFieldMapper.java @@ -18,6 +18,11 @@ public static List mapFieldsList(Collection ols3FieldNames) { String prefix = ""; String suffix = ""; + if (legacyFieldName.indexOf('^') != -1) { + suffix = legacyFieldName.substring(legacyFieldName.indexOf('^')); + legacyFieldName = legacyFieldName.substring(0, legacyFieldName.indexOf('^')); + } + if (legacyFieldName.endsWith("_s")) { prefix = "lowercase_"; legacyFieldName = legacyFieldName.substring(0, legacyFieldName.length() - 2); @@ -26,11 +31,6 @@ public static List mapFieldsList(Collection ols3FieldNames) { legacyFieldName = legacyFieldName.substring(0, legacyFieldName.length() - 2); } - if (legacyFieldName.indexOf('^') != -1) { - suffix = legacyFieldName.substring(legacyFieldName.indexOf('^')); - legacyFieldName = legacyFieldName.substring(0, legacyFieldName.indexOf('^')); - } - if (legacyFieldName.equals("iri")) { newFields.add(prefix + "iri" + suffix); continue; diff --git a/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/V1SearchController.java b/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/V1SearchController.java index 585e6e8e7..c8e846896 100644 --- a/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/V1SearchController.java +++ b/backend/src/main/java/uk/ac/ebi/spot/ols/controller/api/v1/V1SearchController.java @@ -127,17 +127,18 @@ public void search( if (queryFields == null) { // if exact just search the supplied fields for exact matches if (exact) { - String[] fields = {"label_s", "synonym_s", "short_form_s", "obo_id_s", "iri_s", "annotations_trimmed"}; - solrQuery.setQuery( - "((" + - createUnionQuery(query.toLowerCase(), SolrFieldMapper.mapFieldsList(List.of(fields)) - .toArray(new String[0]), true) - + ") AND ("+ IS_DEFINING_ONTOLOGY.getText() + ":\"true\"^100 OR " + - IS_DEFINING_ONTOLOGY.getText() + ":false^0))" - ); - + solrQuery.set("defType", "edismax"); + solrQuery.setQuery(query.toLowerCase()); + // Specify the query fields with boosting + String[] fields = {"label_s^5", "synonym_s^3", "short_form_s^2", "obo_id_s^2", "iri_s", "annotations_trimmed"}; + solrQuery.set("qf", String.join(" ", SolrFieldMapper.mapFieldsList(List.of(fields)))); + // Boost exact phrase matches in label and synonym fields + solrQuery.set("pf", "lowercase_label^10 lowercase_synonym^5"); + // Set minimum match to require all terms in the phrase to match + solrQuery.set("mm", "100%"); + // Add boost query to prioritize defining ontologies + solrQuery.set("bq", IS_DEFINING_ONTOLOGY.getText() + ":\"true\"^100"); } else { - solrQuery.set("defType", "edismax"); solrQuery.setQuery(query);