diff --git a/src/main/java/org/auscope/portal/core/services/ElasticsearchService.java b/src/main/java/org/auscope/portal/core/services/ElasticsearchService.java index 913a2ac83..ae750e19c 100644 --- a/src/main/java/org/auscope/portal/core/services/ElasticsearchService.java +++ b/src/main/java/org/auscope/portal/core/services/ElasticsearchService.java @@ -343,43 +343,58 @@ public CSWRecordSearchResponse searchCSWRecords(final String matchPhraseText, } // Search Criteria and Query - Criteria cswRecordCriteria = new Criteria(); + Criteria cswSearchCriteria = new Criteria(); if (StringUtils.isNotBlank(matchPhraseText)) { try { - Criteria cswFieldCriteria = new Criteria(); for (String field: cswRecordFields) { + Criteria cswFieldCriteria = new Criteria(); + // Wildcards need an expression search, match otherwise if (StringUtils.containsAny(matchPhraseText, " *?")) { - cswFieldCriteria = cswFieldCriteria.or(field).expression(matchPhraseText); + cswFieldCriteria = new Criteria(field).expression(matchPhraseText); } else { - cswFieldCriteria = cswFieldCriteria.or(field).contains(matchPhraseText); + cswFieldCriteria = new Criteria(field).contains(matchPhraseText); } + // Boost known layer fields in results if (field.equals("knownLayerNames") || field.equals("knownLayerDescriptions")) { cswFieldCriteria = cswFieldCriteria.boost(2); } + if (cswSearchCriteria == null) { + cswSearchCriteria = cswFieldCriteria; + } else { + cswSearchCriteria = cswSearchCriteria.or(cswFieldCriteria); + } + cswSearchCriteria = cswSearchCriteria.or(cswFieldCriteria); } - cswRecordCriteria = cswFieldCriteria; } catch(Exception e) { log.error("Error creating search criteria: " + e.getLocalizedMessage()); return new CSWRecordSearchResponse(0, new ArrayList(), new ArrayList()); } } + // Spatial Criteria + Criteria spatialCriteria = createSpatialBoundsCriteria("cswGeographicElements.boundingPolygon", spatialRelation, + westBoundLongitude, eastBoundLongitude, southBoundLatitude, northBoundLatitude); + // OGC services query + Criteria ogcServicesCriteria = null; if (ogcServices != null && ogcServices.size() > 0) { - Criteria ogcServicesCriteria = new Criteria(); + ogcServicesCriteria = new Criteria(); for (String service: ogcServices) { - ogcServicesCriteria = ogcServicesCriteria.or("onlineResources.protocol").contains(service.toLowerCase()); + Criteria serviceCriteria = new Criteria("onlineResources.protocol").contains(service); + ogcServicesCriteria = ogcServicesCriteria.or(serviceCriteria); } - cswRecordCriteria = cswRecordCriteria.and(ogcServicesCriteria); } - // CSWRecords spatial Criteria - Criteria cswRecordSpatialCriteria = createSpatialBoundsCriteria("cswGeographicElements.boundingPolygon", spatialRelation, - westBoundLongitude, eastBoundLongitude, southBoundLatitude, northBoundLatitude); - if (cswRecordSpatialCriteria != null) { - cswRecordCriteria = cswRecordCriteria.and(cswRecordSpatialCriteria); + // Combine Criteria + Criteria cswRecordCriteria = cswSearchCriteria; + if (ogcServicesCriteria != null) { + cswRecordCriteria = Criteria.and().subCriteria(cswSearchCriteria).subCriteria(ogcServicesCriteria); } - + if (spatialCriteria != null) { + cswRecordCriteria = cswRecordCriteria.and(spatialCriteria); + } + + // Search Query cswRecordQuery = new CriteriaQuery(cswRecordCriteria); Pageable pageable = PageRequest.of(0, 1000); if (page != null && pageSize != null) { @@ -396,6 +411,7 @@ public CSWRecordSearchResponse searchCSWRecords(final String matchPhraseText, return new CSWRecordSearchResponse(0, recordResults, new ArrayList()); } + // Add known layer IDs to result set Set knownLayerIds = new HashSet(); for (SearchHit hit: searchHits) { recordResults.add(hit.getContent());