Revision 45244
Added by Claudio Atzori almost 8 years ago
modules/dnet-index-solr-client/trunk/deploy.info | ||
---|---|---|
1 |
{"type_source": "SVN", "goal": "package -U -T 4C source:jar", "url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/dnet-index-solr-client/trunk/", "deploy_repository": "dnet4-snapshots", "version": "4", "mail": "sandro.labruzzo@isti.cnr.it,michele.artini@isti.cnr.it, claudio.atzori@isti.cnr.it, alessia.bardi@isti.cnr.it", "deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", "name": "dnet-index-solr-client"} |
modules/dnet-index-solr-client/trunk/src/test/java/eu/dnetlib/functionality/index/query/SolrIndexQueryFactoryTest.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.query; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
|
|
5 |
import org.junit.Assert; |
|
6 |
import org.junit.Before; |
|
7 |
import org.junit.Test; |
|
8 |
|
|
9 |
import com.google.common.collect.BiMap; |
|
10 |
import com.google.common.collect.HashBiMap; |
|
11 |
import com.google.common.collect.Lists; |
|
12 |
|
|
13 |
import eu.dnetlib.functionality.index.client.IndexClientException; |
|
14 |
|
|
15 |
public class SolrIndexQueryFactoryTest { |
|
16 |
|
|
17 |
IndexQueryFactory factory; |
|
18 |
BiMap<String, String> browsingAliases = HashBiMap.create(); |
|
19 |
|
|
20 |
@Before |
|
21 |
public void setUp() throws Exception { |
|
22 |
factory = new SolrIndexQueryFactory(); |
|
23 |
browsingAliases.put("field1", "field1ForBrowsing"); |
|
24 |
browsingAliases.put("field2", "field2ForBrowsing"); |
|
25 |
} |
|
26 |
|
|
27 |
@Test |
|
28 |
public void testGetBrowsableFields() throws IndexClientException { |
|
29 |
List<String> browsables = factory.getBrowsableFields(Lists.newArrayList("field1", "field3", "field2"), browsingAliases); |
|
30 |
Assert.assertEquals(Lists.newArrayList("field1ForBrowsing", "field3", "field2ForBrowsing"), browsables); |
|
31 |
} |
|
32 |
|
|
33 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/solr/cql/SolrTypeBasedCqlValueTransformerMapFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.solr.cql; |
|
2 |
|
|
3 |
import java.util.Map; |
|
4 |
|
|
5 |
import org.springframework.beans.factory.annotation.Required; |
|
6 |
|
|
7 |
import eu.dnetlib.functionality.index.model.Any.ValueType; |
|
8 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
9 |
|
|
10 |
/** |
|
11 |
* Factory for the SolrTypeBasedCqlValueTransformerMap class objects |
|
12 |
* |
|
13 |
* @author claudio |
|
14 |
* |
|
15 |
*/ |
|
16 |
public class SolrTypeBasedCqlValueTransformerMapFactory { |
|
17 |
|
|
18 |
/** |
|
19 |
* Map of functions, injected via spring. |
|
20 |
*/ |
|
21 |
private Map<String, UnaryFunction<String, String>> transformerMap; |
|
22 |
|
|
23 |
/** |
|
24 |
* Method returns a new instance of SolrTypeBasedCqlValueTransformerMap. |
|
25 |
* |
|
26 |
* @param schema |
|
27 |
* @return |
|
28 |
*/ |
|
29 |
public SolrTypeBasedCqlValueTransformerMap getIt(final Map<String, ValueType> schema) { |
|
30 |
return new SolrTypeBasedCqlValueTransformerMap(schema, getTransformerMap()); |
|
31 |
} |
|
32 |
|
|
33 |
@Required |
|
34 |
public void setTransformerMap(Map<String, UnaryFunction<String, String>> transformerMap) { |
|
35 |
this.transformerMap = transformerMap; |
|
36 |
} |
|
37 |
|
|
38 |
public Map<String, UnaryFunction<String, String>> getTransformerMap() { |
|
39 |
return transformerMap; |
|
40 |
} |
|
41 |
|
|
42 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/solr/cql/SimpleDateValueTransformer.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.solr.cql; |
|
2 |
|
|
3 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
4 |
|
|
5 |
/** |
|
6 |
* Simply and not very roboust normalizer for solr dates. Basically it handles well yyyy-mm-dd and |
|
7 |
* yyyy-mm-ddThh:mm:ssZ |
|
8 |
* |
|
9 |
* @author marko |
|
10 |
* |
|
11 |
*/ |
|
12 |
public class SimpleDateValueTransformer implements UnaryFunction<String, String> { |
|
13 |
@Override |
|
14 |
public String evaluate(final String value) { |
|
15 |
if (!value.endsWith("Z")) |
|
16 |
return value + "T00:00:00Z"; |
|
17 |
return value; |
|
18 |
} |
|
19 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/solr/cql/SolrTypeBasedCqlValueTransformerMap.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.solr.cql; |
|
2 |
|
|
3 |
import java.util.Map; |
|
4 |
|
|
5 |
import eu.dnetlib.functionality.cql.CqlValueTransformerMap; |
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
8 |
import org.apache.solr.common.SolrException; |
|
9 |
|
|
10 |
import eu.dnetlib.functionality.index.model.Any.ValueType; |
|
11 |
import eu.dnetlib.miscutils.functional.IdentityFunction; |
|
12 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
13 |
|
|
14 |
/** |
|
15 |
* This class maps the fields in the given index schema with a transformation rule. |
|
16 |
* |
|
17 |
* @author marko |
|
18 |
* |
|
19 |
*/ |
|
20 |
public class SolrTypeBasedCqlValueTransformerMap implements CqlValueTransformerMap { |
|
21 |
|
|
22 |
/** |
|
23 |
* logger. |
|
24 |
*/ |
|
25 |
private static final Log log = LogFactory.getLog(SolrTypeBasedCqlValueTransformerMap.class); // NOPMD by marko on 11/24/08 5:02 PM |
|
26 |
|
|
27 |
/** |
|
28 |
* Index schema. |
|
29 |
*/ |
|
30 |
private final Map<String, ValueType> schema; |
|
31 |
|
|
32 |
/** |
|
33 |
* Map of functions. |
|
34 |
*/ |
|
35 |
private final Map<String, UnaryFunction<String, String>> transformerMap; |
|
36 |
|
|
37 |
/** |
|
38 |
* Create value transformer map bound to a specific schema |
|
39 |
* @param schema |
|
40 |
* @param transformerMap |
|
41 |
*/ |
|
42 |
public SolrTypeBasedCqlValueTransformerMap(final Map<String, ValueType> schema, final Map<String, UnaryFunction<String, String>> transformerMap) { |
|
43 |
this.schema = schema; |
|
44 |
this.transformerMap = transformerMap; |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* {@inheritDoc} |
|
49 |
* |
|
50 |
* @see eu.dnetlib.functionality.cql.CqlValueTransformerMap#transformerFor(java.lang.String) |
|
51 |
*/ |
|
52 |
@Override |
|
53 |
public UnaryFunction<String, String> transformerFor(final String fieldName) { |
|
54 |
try { |
|
55 |
final ValueType field = schema.get(fieldName); |
|
56 |
|
|
57 |
if (field != null) { |
|
58 |
UnaryFunction<String, String> res = transformerMap.get(field.name()); |
|
59 |
if (res != null) { |
|
60 |
return res; |
|
61 |
} |
|
62 |
} |
|
63 |
} catch (SolrException e) { |
|
64 |
log.debug("cannot find field", e); |
|
65 |
} |
|
66 |
return new IdentityFunction<String>(); |
|
67 |
} |
|
68 |
|
|
69 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/solr/utils/HighlightUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.solr.utils; |
|
2 |
|
|
3 |
import org.apache.oro.text.perl.Perl5Util; |
|
4 |
|
|
5 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
6 |
|
|
7 |
public class HighlightUtils implements UnaryFunction<String, String> { |
|
8 |
|
|
9 |
public final static String DEFAULT_HL_PRE = "[hl]"; |
|
10 |
|
|
11 |
public final static String DEFAULT_HL_POST = "[/hl]"; |
|
12 |
|
|
13 |
private static String CLEAN_HEADER = "s#\\[/?hl\\]##gm"; |
|
14 |
private static String CLEAN_REGEX_OPEN = "<([^>]*)\\[hl\\]([^>]*)>"; |
|
15 |
private static String CLEAN_REGEX_CLOSE = "<([^>]*)\\[\\/hl\\]([^>]*)>"; |
|
16 |
|
|
17 |
// private static String CLEAN_REGEX_OPEN = "s#<([^>]*)\\[hl\\]([^>]*)>#<$1$2>#gm"; |
|
18 |
// private static String CLEAN_REGEX_CLOSE = "s#<([^>]*)\\[\\/hl\\]([^>]*)>#<$1$2>#gm"; |
|
19 |
|
|
20 |
private Perl5Util p5util = new Perl5Util(); |
|
21 |
|
|
22 |
@Override |
|
23 |
public String evaluate(final String doc) { |
|
24 |
String[] chunk = doc.split("</header>"); |
|
25 |
String string = cleanHeader(chunk[0]) + "</header>" + cleanBody(chunk[1]); |
|
26 |
return string; |
|
27 |
} |
|
28 |
|
|
29 |
private String cleanHeader(final String header) { |
|
30 |
return p5util.substitute(CLEAN_HEADER, header); |
|
31 |
} |
|
32 |
|
|
33 |
// TODO: implement a faster way to do this |
|
34 |
private String cleanBody(final String body) { |
|
35 |
String res = body.replaceAll(CLEAN_REGEX_OPEN, "<$1$2>").replaceAll(CLEAN_REGEX_CLOSE, "<$1$2>"); |
|
36 |
|
|
37 |
if (res.equals(body)) return res; |
|
38 |
|
|
39 |
return cleanBody(res); |
|
40 |
} |
|
41 |
|
|
42 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/model/util/AnySolrUtil.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.model.util; |
|
2 |
|
|
3 |
import java.util.Iterator; |
|
4 |
import java.util.Map.Entry; |
|
5 |
|
|
6 |
import org.apache.solr.common.SolrDocumentList; |
|
7 |
import org.apache.solr.common.util.NamedList; |
|
8 |
|
|
9 |
import eu.dnetlib.functionality.index.model.AnyMap; |
|
10 |
import eu.dnetlib.functionality.index.model.DataFactory; |
|
11 |
import eu.dnetlib.functionality.index.model.InvalidValueTypeException; |
|
12 |
import eu.dnetlib.functionality.index.model.Value; |
|
13 |
import eu.dnetlib.functionality.index.model.impl.DefaultDataFactoryImpl; |
|
14 |
|
|
15 |
/** |
|
16 |
* The Class AnySolrUtil. |
|
17 |
*/ |
|
18 |
public class AnySolrUtil extends AnyUtil { |
|
19 |
|
|
20 |
/** |
|
21 |
* Convert named list to any map. |
|
22 |
* |
|
23 |
* @param list |
|
24 |
* the list |
|
25 |
* @param map |
|
26 |
* the map |
|
27 |
* @return the any map |
|
28 |
*/ |
|
29 |
@SuppressWarnings("unchecked") |
|
30 |
public static AnyMap convertNamedListToAnyMap(final NamedList<Object> list, final AnyMap map) { |
|
31 |
final Iterator<Entry<String, Object>> it = list.iterator(); |
|
32 |
while (it.hasNext()) { |
|
33 |
Entry<String, Object> entry = it.next(); |
|
34 |
final String key = entry.getKey(); |
|
35 |
final Object obj = entry.getValue(); |
|
36 |
if (obj instanceof NamedList<?>) { |
|
37 |
final AnyMap subMap = map.getMap(key, true); |
|
38 |
convertNamedListToAnyMap((NamedList<Object>) obj, subMap); |
|
39 |
} else if (obj instanceof SolrDocumentList) { |
|
40 |
SolrDocumentList docList = (SolrDocumentList) obj; |
|
41 |
AnyMap response = DataFactory.DEFAULT.createAnyMap(); |
|
42 |
response.put("numFound", docList.getNumFound()); |
|
43 |
response.put("start", docList.getStart()); |
|
44 |
response.put("maxScore", docList.getMaxScore()); |
|
45 |
response.put("docs", objectToAny(obj)); |
|
46 |
map.put("response", response); |
|
47 |
} else { |
|
48 |
try { |
|
49 |
final Value value = DataFactory.DEFAULT.autoConvertValue(obj); |
|
50 |
map.put(key, value); |
|
51 |
} catch (InvalidValueTypeException exception) { |
|
52 |
; // skip |
|
53 |
} |
|
54 |
} |
|
55 |
} |
|
56 |
return map; |
|
57 |
} |
|
58 |
|
|
59 |
/** |
|
60 |
* Convert named list to any map. |
|
61 |
* |
|
62 |
* @param list |
|
63 |
* the list |
|
64 |
* @return the any map |
|
65 |
*/ |
|
66 |
public static AnyMap convertNamedListToAnyMap(final NamedList<Object> list) { |
|
67 |
return convertNamedListToAnyMap(list, DefaultDataFactoryImpl.INSTANCE.createAnyMap()); |
|
68 |
} |
|
69 |
|
|
70 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/model/util/SolrIndexDocument.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.model.util; |
|
2 |
|
|
3 |
import java.util.Collection; |
|
4 |
import java.util.HashMap; |
|
5 |
import java.util.Map; |
|
6 |
|
|
7 |
import org.apache.solr.client.solrj.response.SpellCheckResponse.Collation; |
|
8 |
import org.apache.solr.common.SolrInputDocument; |
|
9 |
import org.apache.solr.common.SolrInputField; |
|
10 |
|
|
11 |
import eu.dnetlib.functionality.index.model.Any.ValueType; |
|
12 |
import eu.dnetlib.functionality.index.model.document.AbstractIndexDocument; |
|
13 |
|
|
14 |
// TODO: Auto-generated Javadoc |
|
15 |
/** |
|
16 |
* The Class SolrIndexDocument an implementation of the index document for SOLR. |
|
17 |
*/ |
|
18 |
public class SolrIndexDocument extends AbstractIndexDocument { |
|
19 |
|
|
20 |
/** |
|
21 |
* Instantiates a new solr index document. |
|
22 |
* |
|
23 |
* @param schema |
|
24 |
* the schema |
|
25 |
* @param dsId |
|
26 |
* the ds id |
|
27 |
*/ |
|
28 |
public SolrIndexDocument(final Map<String, ValueType> schema, final String dsId) { |
|
29 |
super(schema, dsId); |
|
30 |
} |
|
31 |
|
|
32 |
/** |
|
33 |
* Instantiates a new solr index document. |
|
34 |
* |
|
35 |
* @param schema |
|
36 |
* the schema |
|
37 |
* @param dsId |
|
38 |
* the ds id |
|
39 |
* @param solrDocument |
|
40 |
* the solr document |
|
41 |
*/ |
|
42 |
public SolrIndexDocument(final Map<String, ValueType> schema, final String dsId, final SolrInputDocument solrDocument) { |
|
43 |
super(schema, dsId); |
|
44 |
addFields(solrDocument); |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* Adds the fields. |
|
49 |
* |
|
50 |
* @param solrDocument |
|
51 |
* the solr document |
|
52 |
*/ |
|
53 |
private void addFields(final SolrInputDocument solrDocument) { |
|
54 |
for (String name : solrDocument.getFieldNames()) { |
|
55 |
Collection<Object> fieldValues = solrDocument.getFieldValues(name); |
|
56 |
if(fieldValues.size()>1) |
|
57 |
addField(name, fieldValues); |
|
58 |
else if (fieldValues.size()==1) |
|
59 |
addField(name, fieldValues.iterator().next()); |
|
60 |
} |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* Sets the content. |
|
65 |
* |
|
66 |
* @param solrDocument |
|
67 |
* the new content |
|
68 |
*/ |
|
69 |
public void setContent(final SolrInputDocument solrDocument) { |
|
70 |
addFields(solrDocument); |
|
71 |
} |
|
72 |
|
|
73 |
/** |
|
74 |
* Gets the solr document. |
|
75 |
* |
|
76 |
* @return the solr document |
|
77 |
*/ |
|
78 |
public SolrInputDocument getSolrDocument() { |
|
79 |
|
|
80 |
Map<String, SolrInputField> data = new HashMap<String, SolrInputField>(); |
|
81 |
for (String key : fields.keySet()) { |
|
82 |
SolrInputField solrField = new SolrInputField(key); |
|
83 |
for (Object o : fields.get(key)) { |
|
84 |
solrField.addValue(o, 1.0f); |
|
85 |
} |
|
86 |
data.put(key, solrField); |
|
87 |
} |
|
88 |
return new SolrInputDocument(data); |
|
89 |
} |
|
90 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/query/SolrIndexQueryResponseFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.query; |
|
2 |
|
|
3 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
4 |
|
|
5 |
import eu.dnetlib.functionality.index.client.IndexClientException; |
|
6 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
7 |
|
|
8 |
/** |
|
9 |
* The Class SolrIndexQueryResponseFactory. |
|
10 |
*/ |
|
11 |
public class SolrIndexQueryResponseFactory extends QueryResponseFactory<QueryResponse> { |
|
12 |
|
|
13 |
/** |
|
14 |
* {@inheritDoc} |
|
15 |
* |
|
16 |
* @throws IndexClientException |
|
17 |
* |
|
18 |
* @see eu.dnetlib.functionality.index.query.QueryResponseFactory#getQueryResponseParser(eu.dnetlib.functionality.index.query.IndexQueryResponse, |
|
19 |
* eu.dnetlib.functionality.index.utils.MetadataReference) |
|
20 |
*/ |
|
21 |
@Override |
|
22 |
public QueryResponseParser getQueryResponseParser(final IndexQueryResponse<QueryResponse> queryRsp, final MetadataReference mdRef) |
|
23 |
throws IndexClientException { |
|
24 |
|
|
25 |
QueryResponse response = queryRsp.getContextualQueryResponse(); |
|
26 |
return new SolrResponseParser(highlightUtils, browseAliases.get(mdRef), returnEmptyFields, includeRanking, response); |
|
27 |
} |
|
28 |
|
|
29 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/query/SolrIndexQueryFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.query; |
|
2 |
|
|
3 |
import java.util.Arrays; |
|
4 |
|
|
5 |
import eu.dnetlib.functionality.cql.lucene.TranslatedQuery; |
|
6 |
import org.apache.commons.logging.Log; |
|
7 |
import org.apache.commons.logging.LogFactory; |
|
8 |
|
|
9 |
import eu.dnetlib.functionality.index.client.AbstractIndexClient; |
|
10 |
import eu.dnetlib.functionality.index.query.Pruner.Result; |
|
11 |
import eu.dnetlib.functionality.index.solr.utils.HighlightUtils; |
|
12 |
import eu.dnetlib.functionality.index.utils.IndexFieldUtility; |
|
13 |
|
|
14 |
/** |
|
15 |
* A factory for creating SolrIndexQuery objects. |
|
16 |
*/ |
|
17 |
public class SolrIndexQueryFactory extends IndexQueryFactory { |
|
18 |
|
|
19 |
/** |
|
20 |
* logger. |
|
21 |
*/ |
|
22 |
private static final Log log = LogFactory.getLog(SolrIndexQueryFactory.class); |
|
23 |
|
|
24 |
/** The Property name SERVICE_HIGHLIGHT_ENABLE. */ |
|
25 |
private static final String SERVICE_HIGHLIGHT_ENABLE = "service.index.solr.highlight.enable"; |
|
26 |
|
|
27 |
/* |
|
28 |
* (non-Javadoc) |
|
29 |
* |
|
30 |
* @see eu.dnetlib.functionality.index.query.IndexQueryFactory#newInstance(eu.dnetlib.functionality.index.cql.TranslatedQuery, |
|
31 |
* eu.dnetlib.functionality.index.query.Pruner.Result, eu.dnetlib.functionality.index.query.QueryLanguage) |
|
32 |
*/ |
|
33 |
@Override |
|
34 |
protected IndexQuery newInstance(final TranslatedQuery cql, final Result res, final QueryLanguage lang) { |
|
35 |
|
|
36 |
switch (lang) { |
|
37 |
case CQL: |
|
38 |
return new SolrIndexQuery(cql, res.getOptionMap()); |
|
39 |
case SOLR: |
|
40 |
return new SolrIndexQuery(res.getNode().toCQL(), res.getOptionMap()); |
|
41 |
default: |
|
42 |
throw new IllegalArgumentException("invalid query language: " + lang); |
|
43 |
} |
|
44 |
} |
|
45 |
|
|
46 |
/* |
|
47 |
* (non-Javadoc) |
|
48 |
* |
|
49 |
* @see eu.dnetlib.functionality.index.query.IndexQueryFactory#setQueryOptions(eu.dnetlib.functionality.index.query.IndexQuery, |
|
50 |
* eu.dnetlib.functionality.index.IndexServerDAO) |
|
51 |
*/ |
|
52 |
@Override |
|
53 |
protected IndexQuery setQueryOptions(final IndexQuery indexQuery, final AbstractIndexClient client) { |
|
54 |
|
|
55 |
final SolrIndexQuery solrQuery = (SolrIndexQuery) indexQuery; |
|
56 |
|
|
57 |
boolean isHighlightEnabled = Boolean.parseBoolean(client.getServiceProperties().get(SERVICE_HIGHLIGHT_ENABLE)); |
|
58 |
if (solrQuery.getHighlight() & isHighlightEnabled) { |
|
59 |
solrQuery.setHighlightFragsize(0).setHighlightSnippets(1).setHighlightSimplePre(HighlightUtils.DEFAULT_HL_PRE) |
|
60 |
.setHighlightSimplePost(HighlightUtils.DEFAULT_HL_POST).addHighlightField(IndexFieldUtility.RESULT) |
|
61 |
.addField(IndexFieldUtility.INDEX_RECORD_ID); |
|
62 |
} |
|
63 |
|
|
64 |
solrQuery.addField(IndexFieldUtility.RESULT); |
|
65 |
if (solrQuery.getFacetFields() != null) { |
|
66 |
log.debug("getFacetFields() " + Arrays.asList(solrQuery.getFacetFields())); |
|
67 |
solrQuery.setFacetMinCount(1); |
|
68 |
} |
|
69 |
|
|
70 |
return solrQuery; |
|
71 |
} |
|
72 |
|
|
73 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/query/SolrResponseParser.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.query; |
|
2 |
|
|
3 |
import static eu.dnetlib.miscutils.collections.MappedCollection.listMap; |
|
4 |
|
|
5 |
import java.util.Collection; |
|
6 |
import java.util.List; |
|
7 |
import java.util.Map; |
|
8 |
|
|
9 |
import org.apache.commons.logging.Log; |
|
10 |
import org.apache.commons.logging.LogFactory; |
|
11 |
import org.apache.solr.client.solrj.response.FacetField; |
|
12 |
import org.apache.solr.client.solrj.response.FacetField.Count; |
|
13 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
14 |
import org.apache.solr.common.SolrDocument; |
|
15 |
import org.apache.solr.common.SolrDocumentList; |
|
16 |
|
|
17 |
import com.google.common.base.Predicate; |
|
18 |
import com.google.common.collect.BiMap; |
|
19 |
import com.google.common.collect.Iterables; |
|
20 |
import com.google.common.collect.Lists; |
|
21 |
|
|
22 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
|
23 |
import eu.dnetlib.data.provision.index.rmi.GroupResult; |
|
24 |
import eu.dnetlib.functionality.index.utils.IndexFieldUtility; |
|
25 |
import eu.dnetlib.miscutils.functional.UnaryFunction; |
|
26 |
|
|
27 |
/** |
|
28 |
* The Class SolrResponseParser. |
|
29 |
*/ |
|
30 |
public class SolrResponseParser extends QueryResponseParser { |
|
31 |
|
|
32 |
/** |
|
33 |
* logger. |
|
34 |
*/ |
|
35 |
private static final Log log = LogFactory.getLog(SolrResponseParser.class); |
|
36 |
|
|
37 |
/** |
|
38 |
* Lower level response. |
|
39 |
*/ |
|
40 |
private QueryResponse queryRsp = null; |
|
41 |
|
|
42 |
/** The wrapper rank. */ |
|
43 |
protected final UnaryFunction<String, SolrDocument> wrapperRank = new UnaryFunction<String, SolrDocument>() { |
|
44 |
|
|
45 |
@Override |
|
46 |
public String evaluate(final SolrDocument doc) { |
|
47 |
return addRanking(getSingleField(doc, IndexFieldUtility.RESULT), getSingleField(doc, IndexFieldUtility.SCORE_FIELD)); |
|
48 |
} |
|
49 |
}; |
|
50 |
|
|
51 |
/** The wrapper no rank. */ |
|
52 |
protected final UnaryFunction<String, SolrDocument> wrapperNoRank = new UnaryFunction<String, SolrDocument>() { |
|
53 |
|
|
54 |
@Override |
|
55 |
public String evaluate(final SolrDocument doc) { |
|
56 |
return wrap(getSingleField(doc, IndexFieldUtility.RESULT)); |
|
57 |
} |
|
58 |
}; |
|
59 |
|
|
60 |
/** |
|
61 |
* The Constructor. |
|
62 |
* |
|
63 |
* @param highlightUtils |
|
64 |
* the highlight utils |
|
65 |
* @param aliases |
|
66 |
* the aliases |
|
67 |
* @param returnEmptyFields |
|
68 |
* the return empty fields |
|
69 |
* @param includeRanking |
|
70 |
* the include ranking |
|
71 |
* @param response |
|
72 |
* the response |
|
73 |
*/ |
|
74 |
public SolrResponseParser(final UnaryFunction<String, String> highlightUtils, final BiMap<String, String> aliases, final boolean returnEmptyFields, |
|
75 |
final boolean includeRanking, final QueryResponse response) { |
|
76 |
super(highlightUtils, aliases, returnEmptyFields, includeRanking); |
|
77 |
this.queryRsp = response; |
|
78 |
} |
|
79 |
|
|
80 |
/** |
|
81 |
* {@inheritDoc} |
|
82 |
* |
|
83 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getNumFound() |
|
84 |
*/ |
|
85 |
@Override |
|
86 |
public long getNumFound() { |
|
87 |
|
|
88 |
return this.queryRsp.getResults().getNumFound(); |
|
89 |
} |
|
90 |
|
|
91 |
/** |
|
92 |
* {@inheritDoc} |
|
93 |
* |
|
94 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getQueryTime() |
|
95 |
*/ |
|
96 |
@Override |
|
97 |
public int getQueryTime() { |
|
98 |
return queryRsp.getQTime(); |
|
99 |
} |
|
100 |
|
|
101 |
/** |
|
102 |
* {@inheritDoc} |
|
103 |
* |
|
104 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getElapsedTime() |
|
105 |
*/ |
|
106 |
@Override |
|
107 |
public long getElapsedTime() { |
|
108 |
return queryRsp.getElapsedTime(); |
|
109 |
} |
|
110 |
|
|
111 |
/** |
|
112 |
* {@inheritDoc} |
|
113 |
* |
|
114 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getStatus() |
|
115 |
*/ |
|
116 |
@Override |
|
117 |
public String getStatus() { |
|
118 |
return String.valueOf(queryRsp.getStatus()); |
|
119 |
} |
|
120 |
|
|
121 |
/** |
|
122 |
* {@inheritDoc} |
|
123 |
* |
|
124 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getCurrentSize() |
|
125 |
*/ |
|
126 |
@Override |
|
127 |
public int getCurrentSize() { |
|
128 |
return queryRsp.getResults().size(); |
|
129 |
} |
|
130 |
|
|
131 |
/** |
|
132 |
* Gets the query response. |
|
133 |
* |
|
134 |
* @return the query response |
|
135 |
*/ |
|
136 |
public QueryResponse getQueryResponse() { |
|
137 |
return queryRsp; |
|
138 |
} |
|
139 |
|
|
140 |
/** |
|
141 |
* {@inheritDoc} |
|
142 |
* |
|
143 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getResults() |
|
144 |
*/ |
|
145 |
@Override |
|
146 |
public List<String> getResults() { |
|
147 |
return asRankedList(queryRsp.getResults()); |
|
148 |
} |
|
149 |
|
|
150 |
/** |
|
151 |
* {@inheritDoc} |
|
152 |
* |
|
153 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getNumberOfBrowsingResults() |
|
154 |
*/ |
|
155 |
@Override |
|
156 |
public Long getNumberOfBrowsingResults() { |
|
157 |
List<FacetField> ffList = queryRsp.getFacetFields(); |
|
158 |
Long maxCount = 0L; |
|
159 |
|
|
160 |
if (ffList != null) { |
|
161 |
for (FacetField ff : ffList) { |
|
162 |
if (ff != null) { |
|
163 |
Long countFacets = countFacets(ff.getValues()); |
|
164 |
if (countFacets > maxCount) { |
|
165 |
maxCount = countFacets; |
|
166 |
} |
|
167 |
} |
|
168 |
} |
|
169 |
} |
|
170 |
return maxCount; |
|
171 |
} |
|
172 |
|
|
173 |
/** |
|
174 |
* {@inheritDoc} |
|
175 |
* |
|
176 |
* @see eu.dnetlib.functionality.index.query.QueryResponseParser#getBrowsingResults() |
|
177 |
*/ |
|
178 |
@Override |
|
179 |
public List<BrowsingRow> getBrowsingResults() { |
|
180 |
List<BrowsingRow> bresList = Lists.newArrayList(); |
|
181 |
List<GroupResult> facets = Lists.newArrayList(); |
|
182 |
|
|
183 |
final List<FacetField> ffList = queryRsp.getFacetFields(); |
|
184 |
|
|
185 |
Long numberOfBrowsingResults = getNumberOfBrowsingResults(); |
|
186 |
for (int i = 0; (ffList != null) && (i < numberOfBrowsingResults); i++) { |
|
187 |
for (FacetField ff : ffList) { |
|
188 |
|
|
189 |
String name = null; |
|
190 |
if (aliases != null) { |
|
191 |
name = aliases.inverse().get(ff.getName()); |
|
192 |
} |
|
193 |
|
|
194 |
// fix #1456 |
|
195 |
if (name == null) { |
|
196 |
name = ff.getName(); |
|
197 |
} |
|
198 |
|
|
199 |
final Count facet = getFacet(ff, i); |
|
200 |
|
|
201 |
if ((facet != null) && (facet.getCount() > 0)) { |
|
202 |
|
|
203 |
final String value = facet.getName(); |
|
204 |
final int count = (int) facet.getCount(); |
|
205 |
|
|
206 |
if (returnEmptyFields || !value.isEmpty()) { |
|
207 |
facets.add(new GroupResult(name, value, count)); |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 |
if (facets.size() > 0) { |
|
213 |
bresList.add(new BrowsingRow(Lists.newArrayList(facets))); |
|
214 |
facets.clear(); |
|
215 |
} |
|
216 |
} |
|
217 |
if (log.isDebugEnabled()) { |
|
218 |
log.debug("BrowsingResult size: " + bresList.size()); |
|
219 |
} |
|
220 |
return bresList; |
|
221 |
} |
|
222 |
|
|
223 |
// /////////////// helpers |
|
224 |
|
|
225 |
/** |
|
226 |
* Gets the facet. |
|
227 |
* |
|
228 |
* @param ff |
|
229 |
* the ff |
|
230 |
* @param pos |
|
231 |
* the pos |
|
232 |
* @return the facet |
|
233 |
*/ |
|
234 |
private Count getFacet(final FacetField ff, final int pos) { |
|
235 |
|
|
236 |
if ((ff.getValues() == null) || (pos >= ff.getValues().size())) return null; |
|
237 |
return ff.getValues().get(pos); |
|
238 |
} |
|
239 |
|
|
240 |
/** |
|
241 |
* Given SolrDocumentList, return a List of Strings, representing it. |
|
242 |
* |
|
243 |
* @param documentList |
|
244 |
* the document list |
|
245 |
* @return the list< string> |
|
246 |
*/ |
|
247 |
private List<String> asRankedList(final SolrDocumentList documentList) { |
|
248 |
|
|
249 |
UnaryFunction<String, SolrDocument> wrapper = includeRanking ? wrapperRank : wrapperNoRank; |
|
250 |
|
|
251 |
if (queryRsp.getHighlighting() != null) return listMap(listMap(documentList, new UnaryFunction<String, SolrDocument>() { |
|
252 |
|
|
253 |
@Override |
|
254 |
public String evaluate(final SolrDocument doc) { |
|
255 |
|
|
256 |
String score = getSingleField(doc, IndexFieldUtility.SCORE_FIELD); |
|
257 |
|
|
258 |
String hl = getHighlighting(getSingleField(doc, IndexFieldUtility.INDEX_RECORD_ID)); |
|
259 |
String res = hl != null ? hl : getSingleField(doc, IndexFieldUtility.RESULT); |
|
260 |
|
|
261 |
return includeRanking ? addRanking(res, score) : wrap(res); |
|
262 |
} |
|
263 |
}), highlightUtils); |
|
264 |
|
|
265 |
return listMap(documentList, wrapper); |
|
266 |
} |
|
267 |
|
|
268 |
/** |
|
269 |
* Converts a String document to |
|
270 |
* |
|
271 |
* <record rank="score"> [document] </record>. |
|
272 |
* |
|
273 |
* @param doc |
|
274 |
* the doc |
|
275 |
* @param score |
|
276 |
* the score |
|
277 |
* @return the string |
|
278 |
*/ |
|
279 |
private String addRanking(final String doc, final String score) { |
|
280 |
return new String("<record rank=\"" + score + "\">" + doc + "</record>"); |
|
281 |
} |
|
282 |
|
|
283 |
/** |
|
284 |
* Wraps the given document as <record> [document] </record>. |
|
285 |
* |
|
286 |
* @param doc |
|
287 |
* the doc |
|
288 |
* @return the string |
|
289 |
*/ |
|
290 |
private String wrap(final String doc) { |
|
291 |
return new String("<record>" + doc + "</record>"); |
|
292 |
} |
|
293 |
|
|
294 |
/** |
|
295 |
* Gets the single field. |
|
296 |
* |
|
297 |
* @param doc |
|
298 |
* the doc |
|
299 |
* @param fieldName |
|
300 |
* the field name |
|
301 |
* @return the single field |
|
302 |
*/ |
|
303 |
@SuppressWarnings("unchecked") |
|
304 |
protected String getSingleField(final SolrDocument doc, final String fieldName) { |
|
305 |
Object value = doc.getFieldValue(fieldName); |
|
306 |
if (value instanceof Collection) return Iterables.getOnlyElement((Iterable<String>) value); |
|
307 |
return String.valueOf(value); |
|
308 |
} |
|
309 |
|
|
310 |
/** |
|
311 |
* Gets the highlighting. |
|
312 |
* |
|
313 |
* @param docId |
|
314 |
* the doc id |
|
315 |
* @return the highlighting |
|
316 |
*/ |
|
317 |
private String getHighlighting(final String docId) { |
|
318 |
final Map<String, List<String>> highlight = queryRsp.getHighlighting().get(docId); |
|
319 |
|
|
320 |
String result = new String(); |
|
321 |
if ((highlight != null) && (highlight.get(IndexFieldUtility.RESULT) != null)) { |
|
322 |
for (String s : highlight.get(IndexFieldUtility.RESULT)) { |
|
323 |
result = result.concat(s); |
|
324 |
} |
|
325 |
return result; |
|
326 |
} |
|
327 |
return null; |
|
328 |
} |
|
329 |
|
|
330 |
/** |
|
331 |
* helper method. |
|
332 |
* |
|
333 |
* @param facets |
|
334 |
* the list of facets to analyze |
|
335 |
* @return the number of non-empty facets in the list whose count is greater than zero |
|
336 |
*/ |
|
337 |
private Long countFacets(final List<Count> facets) { |
|
338 |
|
|
339 |
if (facets == null) return 0L; |
|
340 |
|
|
341 |
return (long) Iterables.size(Iterables.filter(facets, new Predicate<Count>() { |
|
342 |
|
|
343 |
@Override |
|
344 |
public boolean apply(final Count c) { |
|
345 |
return (c != null) && (c.getName() != null) && !c.getName().isEmpty() && (c.getCount() > 0); |
|
346 |
} |
|
347 |
})); |
|
348 |
} |
|
349 |
|
|
350 |
@Override |
|
351 |
public long getStart() { |
|
352 |
// TODO Auto-generated method stub |
|
353 |
return queryRsp.getResults().getStart(); |
|
354 |
} |
|
355 |
|
|
356 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/query/SolrIndexQueryResponse.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.query; |
|
2 |
|
|
3 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
4 |
|
|
5 |
/** |
|
6 |
* The Class SolrIndexQueryResponse. |
|
7 |
*/ |
|
8 |
public class SolrIndexQueryResponse implements IndexQueryResponse<QueryResponse> { |
|
9 |
|
|
10 |
private QueryResponse solrQueryResponse; |
|
11 |
|
|
12 |
public SolrIndexQueryResponse(final QueryResponse solrQueryResponse) { |
|
13 |
this.solrQueryResponse = solrQueryResponse; |
|
14 |
} |
|
15 |
|
|
16 |
@Override |
|
17 |
public QueryResponse getContextualQueryResponse() { |
|
18 |
return solrQueryResponse; |
|
19 |
} |
|
20 |
|
|
21 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/query/SolrIndexQuery.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.query; |
|
2 |
|
|
3 |
import java.util.List; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
import eu.dnetlib.functionality.cql.lucene.QueryOptions; |
|
7 |
import eu.dnetlib.functionality.cql.lucene.TranslatedQuery; |
|
8 |
import eu.dnetlib.functionality.index.utils.IndexFieldUtility; |
|
9 |
import org.apache.commons.logging.Log; |
|
10 |
import org.apache.commons.logging.LogFactory; |
|
11 |
import org.apache.solr.client.solrj.SolrQuery; |
|
12 |
import org.apache.solr.common.params.ModifiableSolrParams; |
|
13 |
import org.apache.solr.common.params.SolrParams; |
|
14 |
|
|
15 |
/** |
|
16 |
* The Class SolrIndexQuery. |
|
17 |
* |
|
18 |
* @author claudio, sandro |
|
19 |
*/ |
|
20 |
public class SolrIndexQuery extends SolrQuery implements IndexQuery { |
|
21 |
|
|
22 |
/** The Constant serialVersionUID. */ |
|
23 |
private static final long serialVersionUID = 1L; |
|
24 |
|
|
25 |
/** |
|
26 |
* logger. |
|
27 |
*/ |
|
28 |
private static final Log log = LogFactory.getLog(SolrIndexQuery.class); |
|
29 |
|
|
30 |
/** |
|
31 |
* Instantiates a new solr index query. |
|
32 |
* |
|
33 |
* @param query |
|
34 |
* the query |
|
35 |
* @param options |
|
36 |
* the options |
|
37 |
*/ |
|
38 |
public SolrIndexQuery(final TranslatedQuery query, final Map<String, List<String>> options) { |
|
39 |
this(query.asLucene(), options); |
|
40 |
|
|
41 |
setCqlParams(query.getOptions()); |
|
42 |
|
|
43 |
log.debug("internal solr query: " + this.toString()); |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* Instantiates a new solr index query. |
|
48 |
* |
|
49 |
* @param query |
|
50 |
* the query |
|
51 |
* @param options |
|
52 |
* the options |
|
53 |
*/ |
|
54 |
public SolrIndexQuery(final String query, final Map<String, List<String>> options) { |
|
55 |
this(query); |
|
56 |
|
|
57 |
// TODO verify that the input options belongs to solr |
|
58 |
super.add(getQueryParams(options)); |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* Instantiates a new solr index query. |
|
63 |
* |
|
64 |
* @param query |
|
65 |
* the query |
|
66 |
*/ |
|
67 |
public SolrIndexQuery(final String query) { |
|
68 |
super(query); |
|
69 |
} |
|
70 |
|
|
71 |
@Override |
|
72 |
public IndexQuery setQueryOffset(final int offset) { |
|
73 |
super.setStart(offset); |
|
74 |
return this; |
|
75 |
} |
|
76 |
|
|
77 |
@Override |
|
78 |
public IndexQuery setQueryLimit(final int limit) { |
|
79 |
super.setRows(limit); |
|
80 |
return this; |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public IndexQuery setQueryFacetLimit(final int limit) { |
|
85 |
super.setFacetLimit(limit); |
|
86 |
return this; |
|
87 |
} |
|
88 |
|
|
89 |
@Override |
|
90 |
public IndexQuery setQueryFacetOffset(final int offset) { |
|
91 |
super.set("facet.offset", offset); |
|
92 |
return this; |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public IndexQuery setDSIdFilters(final List<String> dsIds) { |
|
97 |
if (!isAll(dsIds)) { |
|
98 |
for (String id : dsIds) { |
|
99 |
addFilterQuery(IndexFieldUtility.DS_ID + ":\"" + id + "\""); |
|
100 |
} |
|
101 |
} |
|
102 |
return this; |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Checks if is all. |
|
107 |
* |
|
108 |
* @param dsIds |
|
109 |
* the ds id |
|
110 |
* @return true, if is all |
|
111 |
*/ |
|
112 |
protected boolean isAll(final List<String> dsIds) { |
|
113 |
return (dsIds != null) && (!dsIds.isEmpty()) && (dsIds.size() == 1) && dsIds.get(0).equalsIgnoreCase(IndexFieldUtility.INDEX_DSID_ALL); |
|
114 |
} |
|
115 |
|
|
116 |
/** |
|
117 |
* Convert our option map to a solr option parameter map. |
|
118 |
* |
|
119 |
* @param options |
|
120 |
* input paramter map. |
|
121 |
* @return solr option parameter map. |
|
122 |
*/ |
|
123 |
private SolrParams getQueryParams(final Map<String, List<String>> options) { |
|
124 |
ModifiableSolrParams params = new ModifiableSolrParams(); |
|
125 |
String[] typeTag = new String[] {}; |
|
126 |
|
|
127 |
for (Map.Entry<String, List<String>> entry : options.entrySet()) { |
|
128 |
params.add(entry.getKey(), entry.getValue().toArray(typeTag)); |
|
129 |
} |
|
130 |
return params; |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* Sets the query options. |
|
135 |
* |
|
136 |
* @param options |
|
137 |
* the options. |
|
138 |
*/ |
|
139 |
private void setCqlParams(final QueryOptions options) { |
|
140 |
if (options != null) { |
|
141 |
if (options.getSort() != null) { |
|
142 |
super.addSort(options.getSort().getField(), SolrQuery.ORDER.valueOf(options.getSort().getMode().name())); |
|
143 |
} |
|
144 |
} |
|
145 |
} |
|
146 |
|
|
147 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/client/clients/solr/SolrIndexClient.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.client.clients.solr; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.util.ArrayList; |
|
5 |
import java.util.HashMap; |
|
6 |
import java.util.List; |
|
7 |
import java.util.Map; |
|
8 |
|
|
9 |
import eu.dnetlib.functionality.cql.CqlValueTransformerMap; |
|
10 |
import org.apache.commons.logging.Log; |
|
11 |
import org.apache.commons.logging.LogFactory; |
|
12 |
import org.apache.solr.client.solrj.SolrServer; |
|
13 |
import org.apache.solr.client.solrj.SolrServerException; |
|
14 |
import org.apache.solr.client.solrj.impl.CloudSolrServer; |
|
15 |
import org.apache.solr.client.solrj.request.LukeRequest; |
|
16 |
import org.apache.solr.client.solrj.response.LukeResponse; |
|
17 |
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo; |
|
18 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
19 |
|
|
20 |
import com.google.common.collect.BiMap; |
|
21 |
import com.google.common.collect.Maps; |
|
22 |
|
|
23 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
|
24 |
import eu.dnetlib.data.provision.index.rmi.GroupResult; |
|
25 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
26 |
import eu.dnetlib.functionality.index.client.AbstractIndexClient; |
|
27 |
import eu.dnetlib.functionality.index.client.IndexClient; |
|
28 |
import eu.dnetlib.functionality.index.client.IndexClientException; |
|
29 |
import eu.dnetlib.functionality.index.client.response.BrowseEntry; |
|
30 |
import eu.dnetlib.functionality.index.client.response.BrowseValueEntry; |
|
31 |
import eu.dnetlib.functionality.index.client.response.LookupResponse; |
|
32 |
|
|
33 |
import eu.dnetlib.functionality.index.model.Any.ValueType; |
|
34 |
import eu.dnetlib.functionality.index.query.IndexQueryFactory; |
|
35 |
import eu.dnetlib.functionality.index.query.QueryLanguage; |
|
36 |
import eu.dnetlib.functionality.index.query.QueryResponseFactory; |
|
37 |
import eu.dnetlib.functionality.index.query.QueryResponseParser; |
|
38 |
import eu.dnetlib.functionality.index.query.SolrIndexQuery; |
|
39 |
import eu.dnetlib.functionality.index.query.SolrIndexQueryFactory; |
|
40 |
import eu.dnetlib.functionality.index.query.SolrIndexQueryResponse; |
|
41 |
import eu.dnetlib.functionality.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory; |
|
42 |
import eu.dnetlib.functionality.index.utils.MetadataReference; |
|
43 |
|
|
44 |
/** |
|
45 |
* The Class SolrIndexClient. |
|
46 |
*/ |
|
47 |
public class SolrIndexClient extends AbstractIndexClient implements IndexClient { |
|
48 |
|
|
49 |
/** The server. */ |
|
50 |
private CloudSolrServer server; |
|
51 |
|
|
52 |
private SolrIndexQueryFactory solrIndexQueryFactory; |
|
53 |
|
|
54 |
/** The query response factory. */ |
|
55 |
private QueryResponseFactory<QueryResponse> queryResponseFactory; |
|
56 |
|
|
57 |
private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory; |
|
58 |
|
|
59 |
private static final Log log = LogFactory.getLog(SolrIndexClient.class); |
|
60 |
|
|
61 |
/** |
|
62 |
* The Constructor. |
|
63 |
* |
|
64 |
* @param format |
|
65 |
* the format |
|
66 |
* @param layout |
|
67 |
* the layout |
|
68 |
* @param interpretation |
|
69 |
* the interpretation |
|
70 |
* @param serviceProperties |
|
71 |
* the service properties |
|
72 |
* @param tMapFactory |
|
73 |
*/ |
|
74 |
public SolrIndexClient(final String format, final String layout, final String interpretation, final Map<String, String> serviceProperties, |
|
75 |
final SolrIndexQueryFactory indexQueryFactory, final QueryResponseFactory<QueryResponse> queryResponseFactory, |
|
76 |
final SolrTypeBasedCqlValueTransformerMapFactory tMapFactory) { |
|
77 |
super(format, layout, interpretation, serviceProperties); |
|
78 |
|
|
79 |
log.debug(String.format("Created a new instance of the index of type %s-%s-%s", format, layout, interpretation)); |
|
80 |
this.solrIndexQueryFactory = indexQueryFactory; |
|
81 |
this.queryResponseFactory = queryResponseFactory; |
|
82 |
this.tMapFactory = tMapFactory; |
|
83 |
|
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Do delete. |
|
88 |
* |
|
89 |
* @param query |
|
90 |
* the CQL query |
|
91 |
* @return true, if do delete |
|
92 |
* @throws IndexServiceException |
|
93 |
* the index service exception |
|
94 |
*/ |
|
95 |
@Override |
|
96 |
public long delete(final String query) throws IndexClientException { |
|
97 |
try { |
|
98 |
log.debug("delete by query: " + query); |
|
99 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
|
100 |
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef); |
|
101 |
String tquery = translatedQuery.getQuery(); |
|
102 |
translatedQuery.setQueryLimit(0); |
|
103 |
|
|
104 |
SolrIndexQueryResponse rsp = new SolrIndexQueryResponse(server.query(translatedQuery)); |
|
105 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(rsp, mdRef); |
|
106 |
long total = responseParser.getNumFound(); |
|
107 |
server.deleteByQuery(tquery); |
|
108 |
server.commit(); |
|
109 |
return total; |
|
110 |
} catch (Exception e) { |
|
111 |
throw new IndexClientException("unable to run delete by query: " + query, e); |
|
112 |
} |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* {@inheritDoc} |
|
117 |
* |
|
118 |
* @throws IndexClientException |
|
119 |
* |
|
120 |
* @see eu.dnetlib.functionality.index.client.IndexClient#browse(String, List, int) |
|
121 |
*/ |
|
122 |
@Override |
|
123 |
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max) throws IndexClientException { |
|
124 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
|
125 |
SolrIndexQuery translatedQuery = buildBrowseQuery(query, browseFields, max, mdRef); |
|
126 |
return executeBrowseQuery(query, translatedQuery, mdRef, browseFields); |
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* {@inheritDoc} |
|
131 |
* |
|
132 |
* @throws IndexClientException |
|
133 |
* |
|
134 |
* @see eu.dnetlib.functionality.index.client.IndexClient#browse(String, List, int, List) |
|
135 |
*/ |
|
136 |
@Override |
|
137 |
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max, final List<String> filterQuery) |
|
138 |
throws IndexClientException { |
|
139 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
|
140 |
SolrIndexQuery translatedQuery = buildBrowseQuery(query, browseFields, max, mdRef); |
|
141 |
if (filterQuery != null) { |
|
142 |
log.debug("Filter Query:"); |
|
143 |
for (String fq : filterQuery) { |
|
144 |
translatedQuery.addFilterQuery(fq); |
|
145 |
log.debug("- " + fq); |
|
146 |
} |
|
147 |
} |
|
148 |
return executeBrowseQuery(query, translatedQuery, mdRef, browseFields); |
|
149 |
|
|
150 |
} |
|
151 |
|
|
152 |
public SolrIndexQuery buildBrowseQuery(final String query, final List<String> browseFields, final int max, final MetadataReference mdRef) |
|
153 |
throws IndexClientException { |
|
154 |
log.debug("Browse request for the index collection for query:" + query); |
|
155 |
|
|
156 |
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef); |
|
157 |
translatedQuery.setFacet(true); |
|
158 |
if (browseFields != null) { |
|
159 |
List<String> browsableFields = solrIndexQueryFactory.getBrowsableFields(browseFields, mdRef); |
|
160 |
log.debug("Browsing fields:"); |
|
161 |
for (String field : browsableFields) { |
|
162 |
translatedQuery.addFacetField(field); |
|
163 |
log.debug("- " + field); |
|
164 |
|
|
165 |
} |
|
166 |
translatedQuery.setFacetLimit(max); |
|
167 |
log.debug("max number of browsing field :" + max); |
|
168 |
} |
|
169 |
return translatedQuery; |
|
170 |
} |
|
171 |
|
|
172 |
private List<BrowseEntry> executeBrowseQuery(final String originalQuery, |
|
173 |
final SolrIndexQuery query, |
|
174 |
final MetadataReference mdRef, |
|
175 |
final List<String> browseFields) throws IndexClientException { |
|
176 |
try { |
|
177 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(server.query(query)); |
|
178 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(response, mdRef); |
|
179 |
List<BrowsingRow> results = responseParser.getBrowsingResults(); |
|
180 |
List<BrowseEntry> out = convertBrowseEntry(browseFields, results, responseParser.getAliases()); |
|
181 |
return out; |
|
182 |
} catch (SolrServerException e) { |
|
183 |
throw new IndexClientException("Error on executing a query " + originalQuery, e); |
|
184 |
} |
|
185 |
} |
|
186 |
|
|
187 |
/** |
|
188 |
* Creates the connection. |
|
189 |
* |
|
190 |
* @return the string |
|
191 |
* @throws IndexClientException |
|
192 |
* the index client exception |
|
193 |
*/ |
|
194 |
private String getUrl() throws IndexClientException { |
|
195 |
String address = serviceProperties.get(ZK_ADDRESS); |
|
196 |
if (address == null) { throw new IndexClientException("Unable to load a solr client, missing zk address"); } |
|
197 |
return address; |
|
198 |
} |
|
199 |
|
|
200 |
/** |
|
201 |
* Gets the server. |
|
202 |
* |
|
203 |
* @return the server |
|
204 |
* @throws IndexClientException |
|
205 |
* the index client exception |
|
206 |
*/ |
|
207 |
public SolrServer getServer() throws IndexClientException { |
|
208 |
if (this.server == null) { |
|
209 |
String url = getUrl(); |
|
210 |
log.debug("create new Client " + url); |
|
211 |
server = new CloudSolrServer(url); |
|
212 |
server.setDefaultCollection(String.format("%s-%s-%s", getFormat(), getLayout(), getInterpretation())); |
|
213 |
try { |
|
214 |
server.ping(); |
|
215 |
} catch (Exception e) { |
|
216 |
throw new IndexClientException("oops something went wrong", e); |
|
217 |
} |
|
218 |
} |
|
219 |
return server; |
|
220 |
} |
|
221 |
|
|
222 |
/** |
|
223 |
* Sets the server. |
|
224 |
* |
|
225 |
* @param server |
|
226 |
* the server |
|
227 |
*/ |
|
228 |
public void setServer(final CloudSolrServer server) { |
|
229 |
this.server = server; |
|
230 |
} |
|
231 |
|
|
232 |
@Override |
|
233 |
public LookupResponse lookup(final String query, final List<String> filterQuery, final int from, final int to) throws IndexClientException { |
|
234 |
log.debug("lookup request for the index collection for query:" + query); |
|
235 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
|
236 |
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef); |
|
237 |
translatedQuery.setQueryOffset(from); |
|
238 |
translatedQuery.setQueryLimit(to - from + 1); |
|
239 |
if (filterQuery != null) { |
|
240 |
for (String fq : filterQuery) { |
|
241 |
translatedQuery.addFilterQuery(fq); |
|
242 |
} |
|
243 |
} |
|
244 |
|
|
245 |
try { |
|
246 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(server.query(translatedQuery)); |
|
247 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(response, mdRef); |
|
248 |
|
|
249 |
return new LookupResponse(responseParser); |
|
250 |
} catch (SolrServerException e) { |
|
251 |
throw new IndexClientException("Error on executing a query " + query, e); |
|
252 |
} |
|
253 |
|
|
254 |
} |
|
255 |
|
|
256 |
@Override |
|
257 |
public CqlValueTransformerMap getCqlValueTransformerMap(final MetadataReference mdRef) throws IndexClientException { |
|
258 |
try { |
|
259 |
return this.tMapFactory.getIt(readFieldNamesAndTypes(mdRef.toString())); |
|
260 |
} catch (Exception e) { |
|
261 |
throw new IndexClientException(e); |
|
262 |
|
|
263 |
} |
|
264 |
} |
|
265 |
|
|
266 |
@Override |
|
267 |
public IndexQueryFactory getIndexQueryFactory() { |
|
268 |
return solrIndexQueryFactory; |
|
269 |
} |
|
270 |
|
|
271 |
@Override |
|
272 |
public void stop() throws IndexClientException { |
|
273 |
try { |
|
274 |
log.debug("shutdown client: " + serviceProperties.get(ZK_ADDRESS)); |
|
275 |
server.shutdown(); |
|
276 |
} catch (Throwable e) { |
|
277 |
throw new IndexClientException(e); |
|
278 |
} |
|
279 |
} |
|
280 |
|
|
281 |
private List<BrowseEntry> convertBrowseEntry(final List<String> browseFields, final List<BrowsingRow> results, final BiMap<String, String> aliases) { |
|
282 |
|
|
283 |
Map<String, BrowseEntry> mapResult = new HashMap<String, BrowseEntry>(); |
|
284 |
for (BrowsingRow row : results) { |
|
285 |
for (GroupResult groupResult : row.getGroupResult()) { |
|
286 |
String name = groupResult.getName(); |
|
287 |
List<BrowseValueEntry> valuesEntry; |
|
288 |
BrowseEntry entry; |
|
289 |
if (mapResult.containsKey(name)) { |
|
290 |
entry = mapResult.get(name); |
|
291 |
valuesEntry = entry.getValues(); |
|
292 |
if (valuesEntry == null) { |
|
293 |
valuesEntry = new ArrayList<BrowseValueEntry>(); |
|
294 |
entry.setValues(valuesEntry); |
|
295 |
} |
|
296 |
|
|
297 |
} else { |
|
298 |
entry = new BrowseEntry(); |
|
299 |
entry.setField(name); |
|
300 |
entry.setLabel(name); |
|
301 |
valuesEntry = new ArrayList<BrowseValueEntry>(); |
|
302 |
entry.setValues(valuesEntry); |
|
303 |
mapResult.put(name, entry); |
|
304 |
} |
|
305 |
String value = groupResult.getValue(); |
|
306 |
int count = groupResult.getCount(); |
|
307 |
BrowseValueEntry entryValue = new BrowseValueEntry(value, count); |
|
308 |
valuesEntry.add(entryValue); |
|
309 |
} |
|
310 |
} |
|
311 |
List<BrowseEntry> out = new ArrayList<BrowseEntry>(); |
|
312 |
for (String b : browseFields) { |
|
313 |
String inverse = null; |
|
314 |
if (aliases != null) { |
|
315 |
inverse = aliases.get(b) != null ? aliases.get(b) : aliases.inverse().get(b); |
|
316 |
} |
|
317 |
if (mapResult.containsKey(b)) { |
|
318 |
out.add(mapResult.get(b)); |
|
319 |
} else if (mapResult.containsKey(inverse) == true) { |
|
320 |
BrowseEntry data = mapResult.get(inverse); |
|
321 |
data.setField(b); |
|
322 |
out.add(data); |
|
323 |
} |
|
324 |
} |
|
325 |
return out; |
|
326 |
} |
|
327 |
|
|
328 |
private Map<String, ValueType> readFieldNamesAndTypes(final String coreName) throws SolrServerException, IOException, IndexClientException { |
|
329 |
// final SolrServer server = cloudServer.getSolrServer(coreName); |
|
330 |
final LukeRequest request = new LukeRequest(); |
|
331 |
request.setShowSchema(true); |
|
332 |
|
|
333 |
// cloudServer.setDefaultCollection(coreName); |
|
334 |
request.setNumTerms(0); |
|
335 |
final LukeResponse response = request.process(getServer()); |
|
336 |
final Map<String, FieldInfo> fieldInfos = response.getFieldInfo(); |
|
337 |
final Map<String, LukeResponse.FieldTypeInfo> fieldTypeInfos = response.getFieldTypeInfo(); |
|
338 |
final Map<String, ValueType> result = Maps.newHashMap(); |
|
339 |
for (FieldInfo fieldInfo : fieldInfos.values()) { |
|
340 |
LukeResponse.FieldTypeInfo fieldTypeInfo = fieldTypeInfos.get(fieldInfo.getType()); |
|
341 |
final String fieldName = fieldTypeInfo.getName().toLowerCase(); |
|
342 |
final ValueType fieldType = resolveSolrTypeClassName(fieldName); |
|
343 |
result.put(fieldInfo.getName(), fieldType); |
|
344 |
} |
|
345 |
return result; |
|
346 |
} |
|
347 |
|
|
348 |
private ValueType resolveSolrTypeClassName(final String solrTypeName) { |
|
349 |
if (solrTypeName.contains("LongField")) { |
|
350 |
return ValueType.LONG; |
|
351 |
} else if (solrTypeName.contains("IntField")) { |
|
352 |
return ValueType.LONG; |
|
353 |
} else if (solrTypeName.contains("short")) { |
|
354 |
return ValueType.LONG; |
|
355 |
} else if (solrTypeName.contains("float")) { |
|
356 |
return ValueType.DOUBLE; |
|
357 |
} else if (solrTypeName.contains("double")) { |
|
358 |
return ValueType.DOUBLE; |
|
359 |
} else if (solrTypeName.contains("date")) { |
|
360 |
return ValueType.DATETIME; |
|
361 |
} else { |
|
362 |
return ValueType.STRING; |
|
363 |
} |
|
364 |
} |
|
365 |
|
|
366 |
} |
modules/dnet-index-solr-client/trunk/src/main/java/eu/dnetlib/functionality/index/client/clients/solr/SolrIndexClientFactory.java | ||
---|---|---|
1 |
package eu.dnetlib.functionality.index.client.clients.solr; |
|
2 |
|
|
3 |
import java.lang.reflect.Type; |
|
4 |
import java.util.Map; |
|
5 |
|
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
import org.springframework.beans.factory.annotation.Required; |
|
8 |
|
|
9 |
import com.google.common.reflect.TypeToken; |
|
10 |
import com.google.gson.Gson; |
|
11 |
|
|
12 |
import eu.dnetlib.functionality.index.client.AbstractIndexClientFactory; |
|
13 |
import eu.dnetlib.functionality.index.client.IndexClient; |
|
14 |
import eu.dnetlib.functionality.index.client.IndexClientException; |
|
15 |
import eu.dnetlib.functionality.index.client.IndexClientFactory; |
|
16 |
import eu.dnetlib.functionality.index.query.SolrIndexQueryFactory; |
|
17 |
import eu.dnetlib.functionality.index.query.SolrIndexQueryResponseFactory; |
|
18 |
import eu.dnetlib.functionality.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory; |
|
19 |
|
|
20 |
/** |
|
21 |
* The Class SolrIndexClientFactory. |
|
22 |
*/ |
|
23 |
public class SolrIndexClientFactory extends AbstractIndexClientFactory implements IndexClientFactory { |
|
24 |
|
|
25 |
/** The id. */ |
|
26 |
private static String ID = "id"; |
|
27 |
|
|
28 |
/** The configuration. */ |
|
29 |
private String configuration; |
|
30 |
|
|
31 |
/** The type token. */ |
|
32 |
@SuppressWarnings("serial") |
|
33 |
protected Type typeToken = new TypeToken<Map<String, String>>() {}.getType(); |
|
34 |
|
|
35 |
/** The service properties. */ |
|
36 |
private Map<String, String> serviceProperties; |
|
37 |
|
|
38 |
@Autowired |
|
39 |
private SolrIndexQueryFactory solrIndexQueryFactory; |
|
40 |
|
|
41 |
@Autowired |
|
42 |
private SolrIndexQueryResponseFactory solrIndexQueryResponseFactory; |
|
43 |
|
|
44 |
@Autowired |
|
45 |
private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory; |
|
46 |
|
|
47 |
/** |
|
48 |
* Inits the class. |
|
49 |
* |
|
50 |
* @throws IndexClientException |
|
51 |
* the index client exception |
|
52 |
*/ |
|
53 |
@Override |
|
54 |
public void init() throws IndexClientException { |
|
55 |
try { |
|
56 |
serviceProperties = new Gson().fromJson(getConfiguration(), typeToken); |
|
57 |
} catch (Throwable e) { |
|
58 |
throw new IndexClientException("unable to parse configuration: " + getConfiguration(), e); |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
/** |
|
63 |
* {@inheritDoc} |
|
64 |
* |
|
65 |
* @see eu.dnetlib.functionality.index.client.IndexClientFactory#getBackendId() |
|
66 |
*/ |
Also available in: Unified diff
codebase used to migrate to java8 the production system