diff --git a/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientImpl.java b/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientImpl.java index 21e2cd9..930fa14 100644 --- a/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientImpl.java +++ b/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientImpl.java @@ -4,7 +4,10 @@ import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import org.codehaus.jackson.map.ObjectMapper; @@ -12,6 +15,7 @@ import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.text.Text; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MultiMatchQueryBuilder; @@ -19,6 +23,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder.Type; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermsQueryBuilder; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.highlight.HighlightField; import org.gcube.portal.databook.shared.EnhancedFeed; import org.gcube.socialnetworking.social_data_indexing_common.utils.ElasticSearchRunningCluster; import org.gcube.socialnetworking.social_data_indexing_common.utils.IndexFields; @@ -60,11 +65,9 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{ ElasticSearchRunningCluster elasticCluster = new ElasticSearchRunningCluster(scope); // save info - clusterName = "ElasticSearchCluster";//elasticCluster.getClusterName(); - hostsToContact = new ArrayList<>(); - hostsToContact.add("localhost");//elasticCluster.getHosts(); - portNumbers = new ArrayList<>(); - portNumbers.add(9300);//elasticCluster.getPorts(); + clusterName = elasticCluster.getClusterName(); + hostsToContact = elasticCluster.getHosts(); + portNumbers = elasticCluster.getPorts(); _log.debug("Creating elasticsearch client for hosts = " + hostsToContact + ", port = " + portNumbers + " and " + " cluster's name = " + clusterName); @@ -103,7 +106,10 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{ } @Override - public List searchInEnhancedFeeds(String query, Set vreIDS, int hits){ + public List searchInEnhancedFeeds(String query, Set vreIDS, int from, int to){ + + if(from < 0 || to <= 0) + return new ArrayList(); List toReturn = new ArrayList<>(); @@ -118,7 +124,7 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{ IndexFields.EF_COMMENT_FULL_NAME) .type(Type.MOST_FIELDS); - //_log.debug(mq.toString()); + // _log.debug(mq.toString()); // filter on vre BoolQueryBuilder filter = QueryBuilders.boolQuery(); @@ -136,7 +142,12 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{ SearchResponse response = client.prepareSearch(IndexFields.INDEX_NAME) .setQuery(filteredQuery) - .setFrom(0).setSize(hits) + .setFrom(from) + .setSize(to) + .setHighlighterPreTags("") + .setHighlighterPostTags("") + .addHighlightedField(IndexFields.EF_FEED_TEXT) + //.addHighlightedField(IndexFields.EF_COMMENT_TEXT) TODO .setExplain(true) .execute() .actionGet(); @@ -149,16 +160,36 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{ ObjectMapper mapper = new ObjectMapper(); + // rebuild objects for (SearchHit hit : results) { - //JSON from String to Object EnhancedFeed enhFeed; try { - - _log.debug("score " + hit.getScore() + " for " +hit.getSourceAsString()); enhFeed = mapper.readValue(hit.getSourceAsString(), EnhancedFeed.class); - // add to the return set + // highlight returned object + Map fields = hit.getHighlightFields(); + Iterator> iterator = fields.entrySet().iterator(); + + while (iterator.hasNext()) { + Map.Entry entry = iterator.next(); + + switch (entry.getKey()) { + case IndexFields.EF_FEED_TEXT: + Text highlightedText = entry.getValue().fragments()[0]; + enhFeed.getFeed().setDescription(highlightedText.toString()); + break; + + // case IndexFields.EF_COMMENT_TEXT: TODO + // break; + + default: + break; + } + + } + toReturn.add(enhFeed); + } catch (IOException e) { _log.error(e.toString()); } diff --git a/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientInterface.java b/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientInterface.java index 06433b6..4220ca5 100644 --- a/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientInterface.java +++ b/src/main/java/org/gcube/socialnetworking/social_data_search_client/ElasticSearchClientInterface.java @@ -14,13 +14,14 @@ import org.gcube.portal.databook.shared.EnhancedFeed; public interface ElasticSearchClientInterface { /** - * Given a query, the method find matching enhanced feeds into the elasticsearch index. + * Given a query, the method find matching enhanced feeds into the elasticsearch index and return to hits starting from from. * @param query the query to match * @param vreIDS specifies the vre(s) to which the returning feeds must belong - * @param hits the maximum number of hits to return - * @return A list if matching enhanced feeds or nothing + * @param from start hits index + * @param to number of hits to returning starting from from + * @return A list of matching enhanced feeds or nothing */ - public List searchInEnhancedFeeds(String query, Set vreIDS, int hits); + public List searchInEnhancedFeeds(String query, Set vreIDS, int from, int to); /** * Delete from the index a document with id docID. diff --git a/src/test/java/org/gcube/socialnetworking/social_data_search_client/Tests.java b/src/test/java/org/gcube/socialnetworking/social_data_search_client/Tests.java index 123c4e4..8ce45c4 100644 --- a/src/test/java/org/gcube/socialnetworking/social_data_search_client/Tests.java +++ b/src/test/java/org/gcube/socialnetworking/social_data_search_client/Tests.java @@ -2,11 +2,10 @@ package org.gcube.socialnetworking.social_data_search_client; import java.util.HashSet; -import java.util.List; import java.util.Set; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; -import org.gcube.portal.databook.shared.EnhancedFeed; +import org.gcube.socialnetworking.social_data_indexing_common.utils.ElasticSearchRunningCluster; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -28,29 +27,27 @@ public class Tests // set security token SecurityTokenProvider.instance.set("422d795b-d978-41d5-abac-b1c8be90a632"); } - + @Test public void retrieveElasticSearchInformation() throws Exception{ - -// ElasticSearchRunningCluster es = new ElasticSearchRunningCluster("gcube"); -// _log.debug("Result is " + es.getClusterName() + " " + es.getHosts() + " " + es.getPorts()); - + + ElasticSearchRunningCluster es = new ElasticSearchRunningCluster("gcube"); + _log.debug("Result is " + es.getClusterName() + " " + es.getHosts() + " " + es.getPorts()); + } - + @Test public void query() throws Exception{ - + ElasticSearchClientImpl el = new ElasticSearchClientImpl("gcube"); Set set = new HashSet(); set.add("/gcube/devsec/devVRE"); - set.add("/gcube/devsec/OpenAireDevVRE"); - List res = el.searchInEnhancedFeeds("#pippo test", set, 10); - _log.info("Query result is " + res); - + el.searchInEnhancedFeeds("walter white", set, 0, 10); + } - + @After public void after(){ - + } }