Skip to content

Commit

Permalink
Added the support for multiple org names in the type ahead
Browse files Browse the repository at this point in the history
  • Loading branch information
Camelia-Orcid committed Jul 13, 2024
1 parent d194a26 commit 5861752
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ synchronized public void processOrgsWithIncorrectPopularity() {

@Override
public List<OrgDisambiguated> searchOrgsFromSolr(String searchTerm, int firstResult, int maxResult, boolean fundersOnly) {
List<OrgDisambiguatedSolrDocument> docs = orcidSolrOrgsClient.getOrgs(searchTerm, firstResult, maxResult, fundersOnly);
List<OrgDisambiguatedSolrDocument> docs = orcidSolrOrgsClient.getOrgs(searchTerm, firstResult, maxResult, fundersOnly, true);
List<OrgDisambiguated> ret = new ArrayList<OrgDisambiguated>();
for (OrgDisambiguatedSolrDocument doc : docs) {
OrgDisambiguated org = convertSolrDocument(doc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

Expand Down Expand Up @@ -33,6 +35,8 @@ public class OrcidSolrOrgsClient {
private static final String SOLR_SELF_SERVICE_ORGS_QUERY = "(org-disambiguated-id-from-source:%s)^50.0 (org-disambiguated-name%s)^50.0 (org-disambiguated-name-string:%s)^25.0";

private static final String SOLR_ORG_BY_ROR_ID_QUERY = "org-disambiguated-id-from-source:%s";

private static final String ORG_NAMES_HIGHLIGHT_DELIMITATOR ="::";

public OrgDisambiguatedSolrDocument findById(Long id) {
SolrQuery query = new SolrQuery();
Expand All @@ -50,7 +54,7 @@ public OrgDisambiguatedSolrDocument findById(Long id) {
return null;
}

public List<OrgDisambiguatedSolrDocument> getOrgs(String searchTerm, int firstResult, int maxResult, boolean fundersOnly) {
public List<OrgDisambiguatedSolrDocument> getOrgs(String searchTerm, int firstResult, int maxResult, boolean fundersOnly, boolean withNamesHighlight) {
StringBuilder queryString = new StringBuilder(SOLR_ORGS_QUERY.replace("%s", searchTerm));
if (fundersOnly) {
queryString.append(" AND is-funding-org:true");
Expand All @@ -60,17 +64,52 @@ public List<OrgDisambiguatedSolrDocument> getOrgs(String searchTerm, int firstRe
query.setQuery(queryString.toString());
query.addOrUpdateSort("score", ORDER.desc);
query.addOrUpdateSort("org-disambiguated-popularity", ORDER.desc);

// Set the preserveMulti parameter
query.setParam("preserveMulti", "true");

if(fundersOnly) {
query.addFilterQuery(String.format("(%s:(%s OR %s))", SolrConstants.ORG_DISAMBIGUATED_ID_SOURCE_TYPE, "ROR", "FUNDREF"));
} else {
query.addFilterQuery(String.format("(%s:(%s))", SolrConstants.ORG_DISAMBIGUATED_ID_SOURCE_TYPE, "ROR"));
}

if(withNamesHighlight) {
query.setHighlight(withNamesHighlight);
query.addHighlightField(SolrConstants.ORG_NAMES);
query.setHighlightSnippets(maxResult);
query.setHighlightSimplePost(ORG_NAMES_HIGHLIGHT_DELIMITATOR);
query.setHighlightSimplePre(ORG_NAMES_HIGHLIGHT_DELIMITATOR);
}

LOGGER.debug("SOLR Query: " + query.toQueryString());

try {
QueryResponse queryResponse = solrReadOnlyOrgsClient.query(query);
return queryResponse.getBeans(OrgDisambiguatedSolrDocument.class);
List<OrgDisambiguatedSolrDocument> orgs = queryResponse.getBeans(OrgDisambiguatedSolrDocument.class);
// Get the highlight results
if(withNamesHighlight) {
List<OrgDisambiguatedSolrDocument> orgsNamesHighlighted = new ArrayList<OrgDisambiguatedSolrDocument>();
Map<String, Map<String, List<String>>> highlightMap = queryResponse.getHighlighting();
for(OrgDisambiguatedSolrDocument org : orgs) {
// Print highlighted snippets
if (highlightMap.containsKey(org.getOrgDisambiguatedId())) {
Map<String, List<String>> fieldHighlightMap = highlightMap.get(org.getOrgDisambiguatedId());
if (fieldHighlightMap.containsKey(SolrConstants.ORG_NAMES)) {
List<String> highlights = fieldHighlightMap.get(SolrConstants.ORG_NAMES);
OrgDisambiguatedSolrDocument highlightOrg;
for (String highlight : highlights) {
//strip the highlight delimitator ORG_NAMES_HIGHLIGHT_DELIMITATOR
highlightOrg = new OrgDisambiguatedSolrDocument(org);
highlightOrg.setOrgDisambiguatedName(highlight.replaceAll(ORG_NAMES_HIGHLIGHT_DELIMITATOR, ""));
orgsNamesHighlighted.add(highlightOrg);
}
}
}
}
return orgsNamesHighlighted;
}
return orgs;
} catch (SolrServerException | IOException se) {
String errorMessage = MessageFormat.format("Error when attempting to search for orgs, with search term {0}", new Object[] { searchTerm });
throw new NonTransientDataAccessResourceException(errorMessage, se);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.orcid.pojo.summary;


import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void pesistAndFindByIdTest() throws IOException, SolrServerException {
@Test
public void findFundrefOrgsOnlyTest() throws SolrServerException, IOException {
ArgumentCaptor<SolrQuery> captor = ArgumentCaptor.forClass(SolrQuery.class);
orcidSolrOrgsClient.getOrgs("xxx", 0, 0, true);
orcidSolrOrgsClient.getOrgs("xxx", 0, 0, true, false);
Mockito.verify(solrReadOnlyOrgsClientMock).query(captor.capture());

SolrQuery query = captor.getValue();
Expand All @@ -81,7 +81,7 @@ public void findFundrefOrgsOnlyTest() throws SolrServerException, IOException {
@Test
public void findNonFundrefOrgsOnlyTest() throws SolrServerException, IOException {
ArgumentCaptor<SolrQuery> captor = ArgumentCaptor.forClass(SolrQuery.class);
orcidSolrOrgsClient.getOrgs("xxx", 0, 0, false);
orcidSolrOrgsClient.getOrgs("xxx", 0, 0, false, false);
Mockito.verify(solrReadOnlyOrgsClientMock).query(captor.capture());

SolrQuery query = captor.getValue();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.orcid.utils.solr.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.solr.client.solrj.beans.Field;

Expand Down Expand Up @@ -287,5 +289,25 @@ public boolean equals(Object obj) {
return false;
return true;
}

public OrgDisambiguatedSolrDocument() {};

public OrgDisambiguatedSolrDocument(OrgDisambiguatedSolrDocument originalDoc) {
this.orgDisambiguatedId = String.valueOf(originalDoc.getOrgDisambiguatedId());
this.orgDisambiguatedName = originalDoc.getOrgDisambiguatedName();
this.orgDisambiguatedCity = originalDoc.getOrgDisambiguatedCity();
this.orgDisambiguatedRegion = originalDoc.getOrgDisambiguatedRegion();
this.orgDisambiguatedCountry = originalDoc.getOrgDisambiguatedCountry();
this.orgDisambiguatedIdFromSource = originalDoc.getOrgDisambiguatedIdFromSource();
this.orgDisambiguatedIdSourceType = originalDoc.getOrgDisambiguatedIdSourceType();
this.orgDisambiguatedType = originalDoc.getOrgDisambiguatedType();
this.orgDisambiguatedPopularity =originalDoc.getOrgDisambiguatedPopularity();
this.orgNames = originalDoc.getOrgNames();
this.isFundingOrg =originalDoc.isFundingOrg();
this.orgLocationsJson = originalDoc.getOrgLocationsJson();
this.orgNamesJson = originalDoc.getOrgNamesJson();
this.isOrgChosenByMember = originalDoc.isOrgChosenByMember();
this.orgDisambiguatedStatus = originalDoc.getOrgDisambiguatedStatus();
}

}
5 changes: 4 additions & 1 deletion properties/development.properties
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ org.orcid.core.works.bulk.write.max=100
org.orcid.core.issn.source=APP-POCQZAUC70YOFL9R

# Orgs grouping
org.orcid.core.orgs.query=(org-disambiguated-name:\"%s\") ^100.0 (org-disambiguated-name:%s*) ^10.0
org.orcid.core.orgs.query=(org-names:\"%s\")^100.0 (org-names:\"%s\"~15)^80.0 (org-names:*%s*)^10.0
org.orcid.core.orgsToGroup.query=select a.* from org_disambiguated a full outer join org_disambiguated_external_identifier b on a.id = b.org_disambiguated_id where b.identifier_type = 'FUNDREF' and a.source_type='ROR'

################
Expand Down Expand Up @@ -257,3 +257,6 @@ org.orcid.scheduler.autospam.enabled=true
# Slack
org.orcid.core.autospam.slackChannel=collab-spam-reports
org.orcid.core.autospam.webhookUrl=

#org.orcid.persistence.liquibase.enabled=false
org.orcid.persistence.solr.read.only.url=http://localhost:8983/solr

0 comments on commit 5861752

Please sign in to comment.