In case of match on the feed's text, the returned matching feed has the highlighted text

git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/social-networking/social-data-search-client@124102 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-02-11 14:53:52 +00:00
parent 61e2cdc58e
commit 8885e36c90
3 changed files with 60 additions and 31 deletions

View File

@ -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<EnhancedFeed> searchInEnhancedFeeds(String query, Set<String> vreIDS, int hits){
public List<EnhancedFeed> searchInEnhancedFeeds(String query, Set<String> vreIDS, int from, int to){
if(from < 0 || to <= 0)
return new ArrayList<EnhancedFeed>();
List<EnhancedFeed> 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("<em><b>")
.setHighlighterPostTags("</b></em>")
.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<String, HighlightField> fields = hit.getHighlightFields();
Iterator<Entry<String, HighlightField>> iterator = fields.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String,HighlightField> 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());
}

View File

@ -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 <b>to</b> hits starting from <b>from</b>.
* @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 <b>from</b>
* @return A list of matching enhanced feeds or nothing
*/
public List<EnhancedFeed> searchInEnhancedFeeds(String query, Set<String> vreIDS, int hits);
public List<EnhancedFeed> searchInEnhancedFeeds(String query, Set<String> vreIDS, int from, int to);
/**
* Delete from the index a document with id docID.

View File

@ -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<String> set = new HashSet<String>();
set.add("/gcube/devsec/devVRE");
set.add("/gcube/devsec/OpenAireDevVRE");
List<EnhancedFeed> res = el.searchInEnhancedFeeds("#pippo test", set, 10);
_log.info("Query result is " + res);
el.searchInEnhancedFeeds("walter white", set, 0, 10);
}
@After
public void after(){
}
}