From ea7de2814ca5b87fe35d0a074e7b2c28da07dfaa Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Fri, 18 Sep 2020 09:25:05 +0200 Subject: [PATCH] management of topic * from UI --- .../controllers/SubscriptionsController.java | 2 +- .../openaire/OpenaireBrokerController.java | 23 ++++++++++++++----- .../src/main/resources/config/schema.sql | 7 ++---- .../static/openaire/datasources.html | 2 +- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/controllers/SubscriptionsController.java b/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/controllers/SubscriptionsController.java index 2eaaeace..b6c80fcd 100644 --- a/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/controllers/SubscriptionsController.java +++ b/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/controllers/SubscriptionsController.java @@ -43,7 +43,7 @@ public class SubscriptionsController extends AbstractLbsController { @Autowired private NotificationRepository notificationRepo; - public static final Predicate verifyTopic = Pattern.compile("^[a-zA-Z0-9._-]+(\\/[a-zA-Z0-9._-]+)+$").asPredicate(); + public static final Predicate verifyTopic = Pattern.compile("^([a-zA-Z0-9._-]+(\\/[a-zA-Z0-9._-]+)+)|\\*$").asPredicate(); public static final Predicate verifyEmail = email -> { try { new InternetAddress(email).validate(); diff --git a/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/openaire/OpenaireBrokerController.java b/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/openaire/OpenaireBrokerController.java index 38e22108..e007d036 100644 --- a/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/openaire/OpenaireBrokerController.java +++ b/apps/dhp-broker-application/src/main/java/eu/dnetlib/broker/openaire/OpenaireBrokerController.java @@ -11,12 +11,15 @@ import java.util.stream.Collectors; import java.util.stream.StreamSupport; import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.NestedQueryBuilder; import org.elasticsearch.index.query.Operator; +import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -164,10 +167,14 @@ public class OpenaireBrokerController extends AbstractLbsController { @GetMapping("/events/{nPage}/{size}") public EventsPage showEvents(@RequestParam final String ds, @RequestParam final String topic, @PathVariable final int nPage, @PathVariable final int size) { + final NestedQueryBuilder nestedQuery = QueryBuilders.nestedQuery("map", QueryBuilders.matchQuery("map.targetDatasourceName", ds), ScoreMode.None); + + final QueryBuilder q = StringUtils.isNotBlank(topic) && !topic.equals("*") ? QueryBuilders.boolQuery() + .must(QueryBuilders.matchQuery("topic", topic).operator(Operator.AND)) + .must(nestedQuery) : nestedQuery; + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(QueryBuilders.boolQuery() - .must(QueryBuilders.matchQuery("topic", topic).operator(Operator.AND)) - .must(QueryBuilders.nestedQuery("map", QueryBuilders.matchQuery("map.targetDatasourceName", ds), ScoreMode.None))) + .withQuery(q) .withSearchType(SearchType.DEFAULT) .withFields("payload") .withPageable(PageRequest.of(nPage, size)) @@ -196,10 +203,14 @@ public class OpenaireBrokerController extends AbstractLbsController { ElasticSearchQueryUtils.addMapConditionForTrust(mapQuery, "map.trust", qObj.getTrust()); ElasticSearchQueryUtils.addMapConditionForDates(mapQuery, "map.targetDateofacceptance", qObj.getDates()); + final NestedQueryBuilder nestedQuery = QueryBuilders.nestedQuery("map", mapQuery, ScoreMode.None); + + final QueryBuilder q = StringUtils.isNotBlank(qObj.getTopic()) && !qObj.getTopic().equals("*") ? QueryBuilders.boolQuery() + .must(QueryBuilders.matchQuery("topic", qObj.getTopic()).operator(Operator.AND)) + .must(nestedQuery) : nestedQuery; + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(QueryBuilders.boolQuery() - .must(QueryBuilders.matchQuery("topic", qObj.getTopic()).operator(Operator.AND)) - .must(QueryBuilders.nestedQuery("map", mapQuery, ScoreMode.None))) + .withQuery(q) .withSearchType(SearchType.DEFAULT) .withFields("payload") .withPageable(PageRequest.of(nPage, size)) diff --git a/apps/dhp-broker-application/src/main/resources/config/schema.sql b/apps/dhp-broker-application/src/main/resources/config/schema.sql index 7472374b..bfff483e 100644 --- a/apps/dhp-broker-application/src/main/resources/config/schema.sql +++ b/apps/dhp-broker-application/src/main/resources/config/schema.sql @@ -27,11 +27,8 @@ CREATE TABLE oa_datasource_stats ( PRIMARY KEY (id, topic) ); --- curl "http://localhost:8080/api/topic-types/add" -d "name=ENRICH&expression=ENRICH%2F%3Ccond%3E%2F%3Cfield%3E&producerId=OpenAIRE&mapKeys=targetDatasourceName" INSERT INTO public.topic_types (id, name, expression, map_keys, producer_id, regex) VALUES ('tt-a739fa2b-fde0-4eb2-bcee-6e7f277347db', 'ENRICH', 'ENRICH//', 'targetDatasourceName', 'OpenAIRE', '^ENRICH\/[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+$'); - --- curl "http://localhost:8080/api/topic-types/add" -d "name=ENRICH_WITH_SUBFIELD&expression=ENRICH%2F%3Ccond%3E%2F%3Cfield%3E%2F%3Csubfield%3E&producerId=OpenAIRE&mapKeys=targetDatasourceName" INSERT INTO public.topic_types (id, name, expression, map_keys, producer_id, regex) VALUES ('tt-93be0404-e7fb-43bb-9a0a-d317f418ed6d', 'ENRICH_WITH_SUBFIELD', 'ENRICH///', 'targetDatasourceName', 'OpenAIRE', '^ENRICH\/[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+\/[a-zA-Z0-9._-]+$'); - --- curl "http://localhost:8080/api/topic-types/add" -d "name=ADD&expression=ADD%2F%3Cfield%3E&producerId=OpenAIRE&mapKeys=targetDatasourceName" INSERT INTO public.topic_types (id, name, expression, map_keys, producer_id, regex) VALUES ('tt-80978da9-1859-47aa-9897-0a0c372365a1', 'ADD', 'ADD/', 'targetDatasourceName', 'OpenAIRE', '^ADD\/[a-zA-Z0-9._-]+$'); +INSERT INTO public.topic_types (id, name, expression, map_keys, producer_id, regex) VALUES ('tt-75e4b98f-17b8-4c2a-b3c6-d0ad7dfb4d51', '*', '*', 'targetDatasourceName', 'OpenAIRE', '^\*$'); + diff --git a/apps/dhp-broker-application/src/main/resources/static/openaire/datasources.html b/apps/dhp-broker-application/src/main/resources/static/openaire/datasources.html index 22ea9430..cdf1ee00 100644 --- a/apps/dhp-broker-application/src/main/resources/static/openaire/datasources.html +++ b/apps/dhp-broker-application/src/main/resources/static/openaire/datasources.html @@ -38,7 +38,7 @@ {{t.size}} - Total + Total {{calculateTotal(topics)}}