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:
parent
61e2cdc58e
commit
8885e36c90
|
@ -4,7 +4,10 @@ import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.InetAddress;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.codehaus.jackson.map.ObjectMapper;
|
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.action.search.SearchResponse;
|
||||||
import org.elasticsearch.client.transport.TransportClient;
|
import org.elasticsearch.client.transport.TransportClient;
|
||||||
import org.elasticsearch.common.settings.Settings;
|
import org.elasticsearch.common.settings.Settings;
|
||||||
|
import org.elasticsearch.common.text.Text;
|
||||||
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
import org.elasticsearch.common.transport.InetSocketTransportAddress;
|
||||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||||
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
|
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.QueryBuilders;
|
||||||
import org.elasticsearch.index.query.TermsQueryBuilder;
|
import org.elasticsearch.index.query.TermsQueryBuilder;
|
||||||
import org.elasticsearch.search.SearchHit;
|
import org.elasticsearch.search.SearchHit;
|
||||||
|
import org.elasticsearch.search.highlight.HighlightField;
|
||||||
import org.gcube.portal.databook.shared.EnhancedFeed;
|
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.ElasticSearchRunningCluster;
|
||||||
import org.gcube.socialnetworking.social_data_indexing_common.utils.IndexFields;
|
import org.gcube.socialnetworking.social_data_indexing_common.utils.IndexFields;
|
||||||
|
@ -60,11 +65,9 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{
|
||||||
ElasticSearchRunningCluster elasticCluster = new ElasticSearchRunningCluster(scope);
|
ElasticSearchRunningCluster elasticCluster = new ElasticSearchRunningCluster(scope);
|
||||||
|
|
||||||
// save info
|
// save info
|
||||||
clusterName = "ElasticSearchCluster";//elasticCluster.getClusterName();
|
clusterName = elasticCluster.getClusterName();
|
||||||
hostsToContact = new ArrayList<>();
|
hostsToContact = elasticCluster.getHosts();
|
||||||
hostsToContact.add("localhost");//elasticCluster.getHosts();
|
portNumbers = elasticCluster.getPorts();
|
||||||
portNumbers = new ArrayList<>();
|
|
||||||
portNumbers.add(9300);//elasticCluster.getPorts();
|
|
||||||
|
|
||||||
_log.debug("Creating elasticsearch client for hosts = " + hostsToContact + ", port = " + portNumbers + " and "
|
_log.debug("Creating elasticsearch client for hosts = " + hostsToContact + ", port = " + portNumbers + " and "
|
||||||
+ " cluster's name = " + clusterName);
|
+ " cluster's name = " + clusterName);
|
||||||
|
@ -103,7 +106,10 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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<>();
|
List<EnhancedFeed> toReturn = new ArrayList<>();
|
||||||
|
|
||||||
|
@ -118,7 +124,7 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{
|
||||||
IndexFields.EF_COMMENT_FULL_NAME)
|
IndexFields.EF_COMMENT_FULL_NAME)
|
||||||
.type(Type.MOST_FIELDS);
|
.type(Type.MOST_FIELDS);
|
||||||
|
|
||||||
//_log.debug(mq.toString());
|
// _log.debug(mq.toString());
|
||||||
|
|
||||||
// filter on vre
|
// filter on vre
|
||||||
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
BoolQueryBuilder filter = QueryBuilders.boolQuery();
|
||||||
|
@ -136,7 +142,12 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{
|
||||||
|
|
||||||
SearchResponse response = client.prepareSearch(IndexFields.INDEX_NAME)
|
SearchResponse response = client.prepareSearch(IndexFields.INDEX_NAME)
|
||||||
.setQuery(filteredQuery)
|
.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)
|
.setExplain(true)
|
||||||
.execute()
|
.execute()
|
||||||
.actionGet();
|
.actionGet();
|
||||||
|
@ -149,16 +160,36 @@ public class ElasticSearchClientImpl implements ElasticSearchClientInterface{
|
||||||
|
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
// rebuild objects
|
||||||
for (SearchHit hit : results) {
|
for (SearchHit hit : results) {
|
||||||
//JSON from String to Object
|
|
||||||
EnhancedFeed enhFeed;
|
EnhancedFeed enhFeed;
|
||||||
try {
|
try {
|
||||||
|
|
||||||
_log.debug("score " + hit.getScore() + " for " +hit.getSourceAsString());
|
|
||||||
enhFeed = mapper.readValue(hit.getSourceAsString(), EnhancedFeed.class);
|
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);
|
toReturn.add(enhFeed);
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
_log.error(e.toString());
|
_log.error(e.toString());
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,13 +14,14 @@ import org.gcube.portal.databook.shared.EnhancedFeed;
|
||||||
public interface ElasticSearchClientInterface {
|
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 query the query to match
|
||||||
* @param vreIDS specifies the vre(s) to which the returning feeds must belong
|
* @param vreIDS specifies the vre(s) to which the returning feeds must belong
|
||||||
* @param hits the maximum number of hits to return
|
* @param from start hits index
|
||||||
* @return A list if matching enhanced feeds or nothing
|
* @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.
|
* Delete from the index a document with id docID.
|
||||||
|
|
|
@ -2,11 +2,10 @@ package org.gcube.socialnetworking.social_data_search_client;
|
||||||
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
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.After;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -28,29 +27,27 @@ public class Tests
|
||||||
// set security token
|
// set security token
|
||||||
SecurityTokenProvider.instance.set("422d795b-d978-41d5-abac-b1c8be90a632");
|
SecurityTokenProvider.instance.set("422d795b-d978-41d5-abac-b1c8be90a632");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void retrieveElasticSearchInformation() throws Exception{
|
public void retrieveElasticSearchInformation() throws Exception{
|
||||||
|
|
||||||
// ElasticSearchRunningCluster es = new ElasticSearchRunningCluster("gcube");
|
ElasticSearchRunningCluster es = new ElasticSearchRunningCluster("gcube");
|
||||||
// _log.debug("Result is " + es.getClusterName() + " " + es.getHosts() + " " + es.getPorts());
|
_log.debug("Result is " + es.getClusterName() + " " + es.getHosts() + " " + es.getPorts());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void query() throws Exception{
|
public void query() throws Exception{
|
||||||
|
|
||||||
ElasticSearchClientImpl el = new ElasticSearchClientImpl("gcube");
|
ElasticSearchClientImpl el = new ElasticSearchClientImpl("gcube");
|
||||||
Set<String> set = new HashSet<String>();
|
Set<String> set = new HashSet<String>();
|
||||||
set.add("/gcube/devsec/devVRE");
|
set.add("/gcube/devsec/devVRE");
|
||||||
set.add("/gcube/devsec/OpenAireDevVRE");
|
el.searchInEnhancedFeeds("walter white", set, 0, 10);
|
||||||
List<EnhancedFeed> res = el.searchInEnhancedFeeds("#pippo test", set, 10);
|
|
||||||
_log.info("Query result is " + res);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void after(){
|
public void after(){
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue