Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions rest/src/main/groovy/whelk/rest/api/SearchUtils2.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import whelk.search2.ESSettings;
import whelk.search2.Query;
import whelk.search2.QueryParams;
import whelk.search2.VocabMappings;
import whelk.search2.ResourceLookup;

import java.io.IOException;
import java.util.*;
Expand All @@ -18,14 +18,14 @@
import static whelk.util.Jackson.mapper;

public class SearchUtils2 {
private final VocabMappings vocabMappings;
private final ResourceLookup resourceLookup;
private final Whelk whelk;
private final ESSettings esSettings;

SearchUtils2(Whelk whelk) {
this.whelk = whelk;
this.esSettings = new ESSettings(whelk);
this.vocabMappings = VocabMappings.load(whelk);
this.resourceLookup = ResourceLookup.load(whelk);
}

Map<String, Object> doSearch(Map<String, String[]> queryParameters) throws InvalidQueryException, IOException {
Expand All @@ -36,7 +36,7 @@ Map<String, Object> doSearch(Map<String, String[]> queryParameters) throws Inval
QueryParams queryParams = new QueryParams(queryParameters);
AppParams appParams = new AppParams(getAppConfig(queryParameters), whelk.getJsonld());

Query query = Query.init(queryParams, appParams, vocabMappings, esSettings, whelk);
Query query = Query.init(queryParams, appParams, resourceLookup, esSettings, whelk);

return query.collectResults();
}
Expand Down
6 changes: 3 additions & 3 deletions sru/src/main/java/whelk/sru/servlet/SruServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public class SruServlet extends WhelkHttpServlet {

JsonLD2MarcXMLConverter converter;
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
VocabMappings vocabMappings;
ResourceLookup resourceLookup;
ESSettings esSettings;

@Override
protected void init(Whelk whelk) {
converter = new JsonLD2MarcXMLConverter(whelk.getMarcFrameConverter());
vocabMappings = VocabMappings.load(whelk);
resourceLookup = ResourceLookup.load(whelk);
esSettings = new ESSettings(whelk);
}

Expand Down Expand Up @@ -74,7 +74,7 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
paramsAsIfSearch.put("_stats", new String[] { "false" }); // don't need facets
QueryParams qp = new QueryParams(paramsAsIfSearch);
AppParams ap = new AppParams(new HashMap<>(), whelk.getJsonld());
Query query = new Query(qp, ap, vocabMappings, esSettings, whelk);
Query query = new Query(qp, ap, resourceLookup, esSettings, whelk);
results = query.collectResults();
} catch (InvalidQueryException | ParseCancellationException e) {
logger.info("Bad query: \"" + parameters.get("query")[0] + "\" -> " + e.getMessage());
Expand Down
9 changes: 4 additions & 5 deletions sru/src/main/java/whelk/sru/servlet/XSearchServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import whelk.search2.ESSettings;
import whelk.search2.Query;
import whelk.search2.QueryParams;
import whelk.search2.VocabMappings;
import whelk.search2.ResourceLookup;
import whelk.util.DocumentUtil;
import whelk.util.FresnelUtil;
import whelk.util.http.HttpTools;
Expand Down Expand Up @@ -58,7 +58,6 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -145,14 +144,14 @@ public static class Errors {

JsonLD2MarcXMLConverter converter;
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
VocabMappings vocabMappings;
ResourceLookup resourceLookup;
ESSettings esSettings;
Map<Format, Templates> transformers;

@Override
protected void init(Whelk whelk) {
converter = new JsonLD2MarcXMLConverter(whelk.getMarcFrameConverter());
vocabMappings = VocabMappings.load(whelk);
resourceLookup = ResourceLookup.load(whelk);
esSettings = new ESSettings(whelk);

try {
Expand Down Expand Up @@ -226,7 +225,7 @@ public void doGet2(HttpServletRequest req, HttpServletResponse res) throws IOExc

QueryParams qp = new QueryParams(paramsAsIfSearch);
AppParams ap = new AppParams(new HashMap<>(), whelk.getJsonld());
var results = new Query(qp, ap, vocabMappings, esSettings, whelk).collectResults();
var results = new Query(qp, ap, resourceLookup, esSettings, whelk).collectResults();

@SuppressWarnings("unchecked")
List<Map<?,?>> items = (List<Map<?,?>>) results.get("items");
Expand Down
30 changes: 21 additions & 9 deletions whelk-core/src/main/groovy/whelk/search2/Disambiguate.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,34 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static whelk.JsonLd.ID_KEY;
import static whelk.JsonLd.LD_KEYS;
import static whelk.JsonLd.VOCAB_KEY;
import static whelk.JsonLd.looksLikeIri;
import static whelk.search2.Query.NONE_CATEGORY;
import static whelk.search2.Query.WORK_CATEGORY;
import static whelk.search2.QueryUtil.encodeUri;
import static whelk.search2.VocabMappings.expandPrefixed;
import static whelk.search2.ResourceLookup.expandPrefixed;

public class Disambiguate {
private final JsonLd jsonLd;

private final VocabMappings vocabMappings;
private final ResourceLookup resourceLookup;
private final Map<String, FilterAlias> filterAliasMappings;

private final List<String> nsPrecedenceOrder;

public Disambiguate(VocabMappings vocabMappings, Collection<FilterAlias> appFilterAliases, Collection<FilterAlias.QueryDefinedAlias> queryFilterAliases, JsonLd jsonLd) {
this.vocabMappings = vocabMappings;
public Disambiguate(ResourceLookup resourceLookup, Collection<FilterAlias> appFilterAliases, Collection<FilterAlias.QueryDefinedAlias> queryFilterAliases, JsonLd jsonLd) {
this.resourceLookup = resourceLookup;
this.filterAliasMappings = getFilterAliasMappings(appFilterAliases, queryFilterAliases);
this.jsonLd = jsonLd;
this.nsPrecedenceOrder = List.of("rdf", "librissearch", (String) jsonLd.context.get(VOCAB_KEY), "bibdb", "bulk", "marc"); // FIXME
}

// For test only
@PackageScope
public Disambiguate(VocabMappings vocabMappings, Collection<FilterAlias> filterAliasMappings, JsonLd jsonLd) {
this.vocabMappings = vocabMappings;
public Disambiguate(ResourceLookup resourceLookup, Collection<FilterAlias> filterAliasMappings, JsonLd jsonLd) {
this.resourceLookup = resourceLookup;
this.filterAliasMappings = getFilterAliasMappings(filterAliasMappings, List.of());
this.jsonLd = jsonLd;
this.nsPrecedenceOrder = List.of("rdf", "librissearch", (String) jsonLd.context.get(VOCAB_KEY), "bibdb", "bulk", "marc"); // FIXME
Expand Down Expand Up @@ -81,7 +82,7 @@ public Selector restrictByValue(Selector selector, String value) {
}

private boolean isRestrictedByValue(String propertyKey) {
return vocabMappings.propertiesRestrictedByValue().containsKey(propertyKey);
return resourceLookup.vocabMappings().propertiesRestrictedByValue().containsKey(propertyKey);
}

private Property restrictByValue(Property property, String value) {
Expand All @@ -96,7 +97,7 @@ private Property restrictByValue(Property property, String value) {
}

private String tryCoerce(String property, String value) {
var coercingSubPropertyKey = vocabMappings.propertiesRestrictedByValue()
var coercingSubPropertyKey = resourceLookup.vocabMappings().propertiesRestrictedByValue()
.getOrDefault(property, Map.of())
.get(value);
if (coercingSubPropertyKey != null) {
Expand Down Expand Up @@ -126,7 +127,7 @@ private Selector _mapQueryKey(Token token) {

private PathElement mapSingleKey(Token token) {
for (String ns : nsPrecedenceOrder) {
Set<String> mappedProperties = vocabMappings.properties()
Set<String> mappedProperties = resourceLookup.vocabMappings().properties()
.getOrDefault(token.value().toLowerCase(), Map.of())
.getOrDefault(ns, Set.of());
if (mappedProperties.size() == 1) {
Expand Down Expand Up @@ -182,6 +183,7 @@ private Optional<Value> mapValueForProperty(Property property, String value, Tok
}
if (property.isType() || property.isVocabTerm()) {
for (String ns : nsPrecedenceOrder) {
var vocabMappings = resourceLookup.vocabMappings();
var mappings = property.isType() ? vocabMappings.classes() : vocabMappings.enums();
Set<String> mappedClasses = mappings.getOrDefault(value.toLowerCase(), Map.of()).getOrDefault(ns, Set.of());
if (mappedClasses.size() == 1) {
Expand All @@ -202,6 +204,16 @@ private Optional<Value> mapValueForProperty(Property property, String value, Tok
String expanded = expandPrefixed(value);
if (looksLikeIri(expanded)) {
return Optional.of(new Link(encodeUri(expanded), token));
} else if (property.range().size() == 1){
var range = property.range().getFirst();
var mappedResourceDescription = resourceLookup.externalMappings().byType()
.getOrDefault(range, Map.of())
.getOrDefault(value.toLowerCase(), Map.of());
if (!mappedResourceDescription.isEmpty()) {
var link = new Link((String) mappedResourceDescription.get(ID_KEY), token);
link.setChip(mappedResourceDescription);
return Optional.of(link);
}
}
}
/*
Expand Down
5 changes: 2 additions & 3 deletions whelk-core/src/main/groovy/whelk/search2/ObjectQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import whelk.JsonLd;
import whelk.Whelk;
import whelk.component.ElasticSearch;
import whelk.exception.InvalidQueryException;
import whelk.search2.querytree.And;
import whelk.search2.querytree.Condition;
Expand Down Expand Up @@ -40,8 +39,8 @@ public class ObjectQuery extends Query {
protected final Link object;
private final List<Property> curatedPredicates;

public ObjectQuery(QueryParams queryParams, AppParams appParams, VocabMappings vocabMappings, ESSettings esSettings, Whelk whelk) throws InvalidQueryException {
super(queryParams, appParams, vocabMappings, esSettings, whelk);
public ObjectQuery(QueryParams queryParams, AppParams appParams, ResourceLookup resourceLookup, ESSettings esSettings, Whelk whelk) throws InvalidQueryException {
super(queryParams, appParams, resourceLookup, esSettings, whelk);
this.object = loadObject();
this.curatedPredicates = loadCuratedPredicates();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import java.util.stream.Stream;

public class PredicateObjectQuery extends ObjectQuery {
public PredicateObjectQuery(QueryParams queryParams, AppParams appParams, VocabMappings vocabMappings, ESSettings esSettings, Whelk whelk) throws InvalidQueryException {
super(queryParams, appParams, vocabMappings, esSettings, whelk);
public PredicateObjectQuery(QueryParams queryParams, AppParams appParams, ResourceLookup resourceLookup, ESSettings esSettings, Whelk whelk) throws InvalidQueryException {
super(queryParams, appParams, resourceLookup, esSettings, whelk);
}

@Override
Expand Down
20 changes: 10 additions & 10 deletions whelk-core/src/main/groovy/whelk/search2/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.google.common.base.Predicates;
import whelk.JsonLd;
import whelk.Whelk;
import whelk.component.ElasticSearch;
import whelk.exception.InvalidQueryException;
import whelk.search2.querytree.And;
import whelk.search2.querytree.Condition;
Expand All @@ -21,7 +20,6 @@
import whelk.search2.querytree.Resource;
import whelk.search2.querytree.Value;
import whelk.search2.querytree.YearRange;
import whelk.util.DocumentUtil;
import whelk.util.FresnelUtil;

import java.util.ArrayList;
Expand Down Expand Up @@ -93,12 +91,12 @@ public enum Connective {

public Query(QueryParams queryParams,
AppParams appParams,
VocabMappings vocabMappings,
ResourceLookup resourceLookup,
ESSettings esSettings,
Whelk whelk) throws InvalidQueryException {
this.queryParams = queryParams;
this.appParams = appParams;
this.disambiguate = new Disambiguate(vocabMappings, appParams.filterAliases, queryParams.aliased, whelk.getJsonld());
this.disambiguate = new Disambiguate(resourceLookup, appParams.filterAliases, queryParams.aliased, whelk.getJsonld());
this.esSettings = esSettings;
this.whelk = whelk;
this.qTree = new QueryTree(queryParams.q, disambiguate);
Expand All @@ -108,12 +106,12 @@ public Query(QueryParams queryParams,
this.stats = new Stats();
}

public static Query init(QueryParams queryParams, AppParams appParams, VocabMappings vocabMappings, ESSettings esSettings, Whelk whelk) throws InvalidQueryException {
public static Query init(QueryParams queryParams, AppParams appParams, ResourceLookup resourceLookup, ESSettings esSettings, Whelk whelk) throws InvalidQueryException {
return switch (getSearchMode(queryParams)) {
case STANDARD_SEARCH -> new Query(queryParams, appParams, vocabMappings, esSettings, whelk);
case OBJECT_SEARCH -> new ObjectQuery(queryParams, appParams, vocabMappings, esSettings, whelk);
case PREDICATE_OBJECT_SEARCH -> new PredicateObjectQuery(queryParams, appParams, vocabMappings, esSettings, whelk);
case SUGGEST -> new SuggestQuery(queryParams, appParams, vocabMappings, esSettings, whelk);
case STANDARD_SEARCH -> new Query(queryParams, appParams, resourceLookup, esSettings, whelk);
case OBJECT_SEARCH -> new ObjectQuery(queryParams, appParams, resourceLookup, esSettings, whelk);
case PREDICATE_OBJECT_SEARCH -> new PredicateObjectQuery(queryParams, appParams, resourceLookup, esSettings, whelk);
case SUGGEST -> new SuggestQuery(queryParams, appParams, resourceLookup, esSettings, whelk);
};
}

Expand Down Expand Up @@ -580,7 +578,9 @@ private void loadChips() {
}

private void queue(Link link) {
linkMap.computeIfAbsent(link.iri(), k -> new ArrayList<>()).add(link);
if (!link.isChipLoaded()) {
linkMap.computeIfAbsent(link.iri(), k -> new ArrayList<>()).add(link);
}
}

private void queue(Collection<Link> links) {
Expand Down
Loading
Loading