Revision 57157
Added by Enrico Ottonello over 4 years ago
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/cql/SolrTypeBasedCqlValueTransformerMapFactory.java | ||
---|---|---|
3 | 3 |
import java.util.Map; |
4 | 4 |
import java.util.function.Function; |
5 | 5 |
|
6 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
|
7 | 6 |
import org.springframework.beans.factory.annotation.Required; |
8 | 7 |
|
8 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
|
9 |
|
|
9 | 10 |
/** |
10 | 11 |
* Factory for the SolrTypeBasedCqlValueTransformerMap class objects |
11 |
* |
|
12 |
*
|
|
12 | 13 |
* @author claudio |
14 |
* |
|
13 | 15 |
*/ |
14 | 16 |
public class SolrTypeBasedCqlValueTransformerMapFactory { |
15 | 17 |
|
... | ... | |
20 | 22 |
|
21 | 23 |
/** |
22 | 24 |
* Method returns a new instance of SolrTypeBasedCqlValueTransformerMap. |
23 |
* |
|
25 |
*
|
|
24 | 26 |
* @param schema |
25 |
* @return |
|
27 |
* @return the SolrTypeBasedCqlValueTransformerMap
|
|
26 | 28 |
*/ |
27 | 29 |
public SolrTypeBasedCqlValueTransformerMap getIt(final Map<String, ValueType> schema) { |
28 | 30 |
return new SolrTypeBasedCqlValueTransformerMap(schema, getTransformerMap()); |
29 | 31 |
} |
30 | 32 |
|
31 |
public Map<String, Function<String, String>> getTransformerMap() { |
|
32 |
return transformerMap; |
|
33 |
} |
|
34 |
|
|
35 | 33 |
@Required |
36 | 34 |
public void setTransformerMap(Map<String, Function<String, String>> transformerMap) { |
37 | 35 |
this.transformerMap = transformerMap; |
38 | 36 |
} |
39 | 37 |
|
38 |
public Map<String, Function<String, String>> getTransformerMap() { |
|
39 |
return transformerMap; |
|
40 |
} |
|
41 |
|
|
40 | 42 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/cql/SimpleDateValueTransformer.java | ||
---|---|---|
5 | 5 |
/** |
6 | 6 |
* Simply and not very roboust normalizer for solr dates. Basically it handles well yyyy-mm-dd and |
7 | 7 |
* yyyy-mm-ddThh:mm:ssZ |
8 |
* |
|
8 |
*
|
|
9 | 9 |
* @author marko |
10 |
* |
|
10 | 11 |
*/ |
11 | 12 |
public class SimpleDateValueTransformer implements Function<String, String> { |
12 | 13 |
|
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/cql/SolrTypeBasedCqlValueTransformerMap.java | ||
---|---|---|
3 | 3 |
import java.util.Map; |
4 | 4 |
import java.util.function.Function; |
5 | 5 |
|
6 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
|
7 |
import eu.dnetlib.cql.CqlValueTransformerMap; |
|
8 | 6 |
import org.apache.commons.logging.Log; |
9 | 7 |
import org.apache.commons.logging.LogFactory; |
10 | 8 |
import org.apache.solr.common.SolrException; |
11 | 9 |
|
10 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
|
11 |
import eu.dnetlib.cql.CqlValueTransformerMap; |
|
12 |
|
|
12 | 13 |
/** |
13 | 14 |
* This class maps the fields in the given index schema with a transformation rule. |
14 |
* |
|
15 |
*
|
|
15 | 16 |
* @author marko |
17 |
* |
|
16 | 18 |
*/ |
17 | 19 |
public class SolrTypeBasedCqlValueTransformerMap implements CqlValueTransformerMap { |
18 | 20 |
|
... | ... | |
33 | 35 |
|
34 | 36 |
/** |
35 | 37 |
* Create value transformer map bound to a specific schema |
36 |
* |
|
37 | 38 |
* @param schema |
39 |
* @param transformerMap |
|
38 | 40 |
*/ |
39 | 41 |
public SolrTypeBasedCqlValueTransformerMap(final Map<String, ValueType> schema, final Map<String, Function<String, String>> transformerMap) { |
40 | 42 |
this.schema = schema; |
... | ... | |
43 | 45 |
|
44 | 46 |
/** |
45 | 47 |
* {@inheritDoc} |
48 |
* |
|
49 |
* @see CqlValueTransformerMap#transformerFor(String) |
|
46 | 50 |
*/ |
47 | 51 |
@Override |
48 | 52 |
public Function<String, String> transformerFor(final String fieldName) { |
... | ... | |
58 | 62 |
} catch (SolrException e) { |
59 | 63 |
log.debug("cannot find field", e); |
60 | 64 |
} |
61 |
return it -> it; |
|
65 |
Function<String,String> identityFunction= (String s)->s; |
|
66 |
return identityFunction; |
|
62 | 67 |
} |
63 | 68 |
|
64 |
} |
|
69 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/model/SolrIndexDocument.java | ||
---|---|---|
4 | 4 |
import java.util.HashMap; |
5 | 5 |
import java.util.Map; |
6 | 6 |
|
7 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
|
8 |
import eu.dnetlib.clients.index.model.document.AbstractIndexDocument; |
|
9 | 7 |
import org.apache.solr.common.SolrInputDocument; |
10 | 8 |
import org.apache.solr.common.SolrInputField; |
11 | 9 |
|
10 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
|
11 |
import eu.dnetlib.clients.index.model.document.AbstractIndexDocument; |
|
12 |
|
|
12 | 13 |
// TODO: Auto-generated Javadoc |
13 | 14 |
|
14 | 15 |
/** |
... | ... | |
19 | 20 |
/** |
20 | 21 |
* Instantiates a new solr index document. |
21 | 22 |
* |
22 |
* @param schema the schema |
|
23 |
* @param dsId the ds id |
|
23 |
* @param schema |
|
24 |
* the schema |
|
25 |
* @param dsId |
|
26 |
* the ds id |
|
24 | 27 |
*/ |
25 | 28 |
public SolrIndexDocument(final Map<String, ValueType> schema, final String dsId) { |
26 | 29 |
super(schema, dsId); |
... | ... | |
29 | 32 |
/** |
30 | 33 |
* Instantiates a new solr index document. |
31 | 34 |
* |
32 |
* @param schema the schema |
|
33 |
* @param dsId the ds id |
|
34 |
* @param solrDocument the solr document |
|
35 |
* @param schema |
|
36 |
* the schema |
|
37 |
* @param dsId |
|
38 |
* the ds id |
|
39 |
* @param solrDocument |
|
40 |
* the solr document |
|
35 | 41 |
*/ |
36 | 42 |
public SolrIndexDocument(final Map<String, ValueType> schema, final String dsId, final SolrInputDocument solrDocument) { |
37 | 43 |
super(schema, dsId); |
... | ... | |
41 | 47 |
/** |
42 | 48 |
* Adds the fields. |
43 | 49 |
* |
44 |
* @param solrDocument the solr document |
|
50 |
* @param solrDocument |
|
51 |
* the solr document |
|
45 | 52 |
*/ |
46 | 53 |
private void addFields(final SolrInputDocument solrDocument) { |
47 | 54 |
for (String name : solrDocument.getFieldNames()) { |
48 | 55 |
Collection<Object> fieldValues = solrDocument.getFieldValues(name); |
49 |
if (fieldValues.size() > 1) |
|
56 |
if (fieldValues.size() > 1) {
|
|
50 | 57 |
addField(name, fieldValues); |
51 |
else if (fieldValues.size() == 1)
|
|
58 |
} else if (fieldValues.size() == 1) {
|
|
52 | 59 |
addField(name, fieldValues.iterator().next()); |
60 |
} |
|
53 | 61 |
} |
54 | 62 |
} |
55 | 63 |
|
56 | 64 |
/** |
57 | 65 |
* Sets the content. |
58 | 66 |
* |
59 |
* @param solrDocument the new content |
|
67 |
* @param solrDocument |
|
68 |
* the new content |
|
60 | 69 |
*/ |
61 | 70 |
public void setContent(final SolrInputDocument solrDocument) { |
62 | 71 |
addFields(solrDocument); |
... | ... | |
69 | 78 |
*/ |
70 | 79 |
public SolrInputDocument getSolrDocument() { |
71 | 80 |
|
72 |
Map<String, SolrInputField> data = new HashMap<String, SolrInputField>();
|
|
81 |
Map<String, SolrInputField> data = new HashMap<>(); |
|
73 | 82 |
for (String key : fields.keySet()) { |
74 | 83 |
SolrInputField solrField = new SolrInputField(key); |
75 | 84 |
for (Object o : fields.get(key)) { |
76 |
solrField.addValue(o, 1.0f);
|
|
85 |
solrField.addValue(o); |
|
77 | 86 |
} |
78 | 87 |
data.put(key, solrField); |
79 | 88 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/SolrIndexServerDAO.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 | 4 |
import java.io.StringReader; |
5 |
import java.util.HashMap; |
|
6 | 5 |
import java.util.Map; |
7 | 6 |
|
7 |
import org.apache.commons.logging.Log; |
|
8 |
import org.apache.commons.logging.LogFactory; |
|
9 |
import org.apache.solr.client.solrj.impl.CloudSolrClient; |
|
10 |
import org.dom4j.Document; |
|
11 |
import org.dom4j.DocumentException; |
|
12 |
import org.dom4j.io.SAXReader; |
|
13 |
import org.springframework.beans.factory.annotation.Autowired; |
|
14 |
import org.springframework.beans.factory.annotation.Required; |
|
15 |
|
|
16 |
import com.google.common.collect.Maps; |
|
17 |
|
|
8 | 18 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
9 | 19 |
import eu.dnetlib.clients.index.query.IndexQueryFactory; |
10 |
import eu.dnetlib.utils.MetadataReference;
|
|
20 |
import eu.dnetlib.cql.CqlValueTransformerMap;
|
|
11 | 21 |
import eu.dnetlib.index.AbstractBackendDescriptor; |
12 | 22 |
import eu.dnetlib.index.IndexCollection; |
13 | 23 |
import eu.dnetlib.index.IndexServerDAO; |
14 |
import eu.dnetlib.cql.CqlValueTransformerMap; |
|
15 | 24 |
import eu.dnetlib.index.feed.DocumentMapperFactory; |
16 | 25 |
import eu.dnetlib.index.query.SolrIndexQueryFactory; |
17 | 26 |
import eu.dnetlib.index.query.SolrIndexQueryResponseFactory; |
... | ... | |
19 | 28 |
import eu.dnetlib.index.solr.feed.SolrDocumentMapperFactory; |
20 | 29 |
import eu.dnetlib.index.utils.IndexConfigFactory; |
21 | 30 |
import eu.dnetlib.index.utils.RemoteSolrAdministrator; |
31 |
import eu.dnetlib.index.utils.ZkServers; |
|
22 | 32 |
import eu.dnetlib.index.utils.ZkUtils; |
23 | 33 |
import eu.dnetlib.rmi.provision.IndexServiceException; |
24 |
import org.apache.commons.logging.Log; |
|
25 |
import org.apache.commons.logging.LogFactory; |
|
26 |
import org.apache.solr.client.solrj.SolrServerException; |
|
27 |
import org.apache.solr.client.solrj.impl.CloudSolrClient; |
|
28 |
import org.dom4j.Document; |
|
29 |
import org.dom4j.DocumentException; |
|
30 |
import org.dom4j.io.SAXReader; |
|
31 |
import org.springframework.beans.factory.annotation.Autowired; |
|
32 |
import org.springframework.beans.factory.annotation.Required; |
|
34 |
import eu.dnetlib.utils.MetadataReference; |
|
33 | 35 |
|
34 | 36 |
/** |
35 | 37 |
* The Class SolrIndexServerDAO. |
... | ... | |
41 | 43 |
*/ |
42 | 44 |
private static final Log log = LogFactory.getLog(SolrIndexServerDAO.class); // NOPMD by marko on 11/24/08 5:02 PM |
43 | 45 |
|
44 |
/** |
|
45 |
* The zk utils. |
|
46 |
*/ |
|
46 |
/** The zk utils. */ |
|
47 | 47 |
@Autowired |
48 | 48 |
private ZkUtils zkUtils; |
49 | 49 |
|
50 |
/** |
|
51 |
* The query response factory. |
|
52 |
*/ |
|
50 |
/** The query response factory. */ |
|
53 | 51 |
@Autowired |
54 | 52 |
private SolrIndexQueryResponseFactory queryResponseFactory; |
55 | 53 |
|
56 | 54 |
@Autowired |
57 | 55 |
private SolrIndexQueryFactory solrIndexQueryFactory; |
58 | 56 |
|
59 |
/** |
|
60 |
* The solr document mapper factory. |
|
61 |
*/ |
|
57 |
/** The solr document mapper factory. */ |
|
62 | 58 |
@Autowired |
63 | 59 |
private SolrDocumentMapperFactory solrDocumentMapperFactory; |
64 | 60 |
|
65 |
/** |
|
66 |
* The solr administrator. |
|
67 |
*/ |
|
61 |
/** The solr administrator. */ |
|
68 | 62 |
private RemoteSolrAdministrator solrAdministrator; |
69 | 63 |
|
70 |
/** |
|
71 |
* The solr type based cql value transformer map. |
|
72 |
*/ |
|
64 |
/** The solr type based cql value transformer map. */ |
|
73 | 65 |
@Autowired |
74 | 66 |
private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory; |
75 | 67 |
|
76 | 68 |
/** |
77 | 69 |
* {@inheritDoc} |
78 |
* <p> |
|
79 |
* String) |
|
70 |
* |
|
71 |
* @see eu.dnetlib.index.IndexServerDAO#createIndexCollection(eu.dnetlib.index.utils.MetadataReference, |
|
72 |
* java.lang.String) |
|
80 | 73 |
*/ |
81 | 74 |
@Override |
82 | 75 |
public void createIndexCollection(final MetadataReference mdref, final String fields) throws IndexServiceException { |
83 |
CloudSolrClient client = null; |
|
84 |
try { |
|
76 |
|
|
77 |
log.debug("createIndexCollection " + mdref.toString() + " " + fields ); |
|
78 |
|
|
79 |
try(CloudSolrClient client = getClient()) { |
|
85 | 80 |
|
86 |
client = getClient(); |
|
87 | 81 |
client.connect(); |
88 |
final Map<String, String> p = getServiceProperties(); |
|
89 | 82 |
|
90 |
if (!solrAdministrator.indexCollectionExists(mdref.toString(), client, p.get("host"), p.get("port"))) {
|
|
91 |
Map<String, String> params = new HashMap<>();
|
|
83 |
if (!solrAdministrator.indexCollectionExists(mdref.toString(), client)) { |
|
84 |
Map<String, String> params = Maps.newHashMap();
|
|
92 | 85 |
|
86 |
final Map<String, String> p = getServiceProperties(); |
|
93 | 87 |
params.put("numShards", p.get("numShards")); |
94 | 88 |
params.put("replicationFactor", p.get("replicationFactor")); |
95 | 89 |
|
96 | 90 |
for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) { |
97 | 91 |
params.put(param_Name.toString(), p.get(param_Name.toString())); |
98 | 92 |
} |
99 |
params.put(IndexConfigFactory.CONFIG_PARAMS.indexDataDir.toString(), mdref.toString()); |
|
100 | 93 |
|
101 |
zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdref.toString(), parse(fields), params, false);
|
|
94 |
zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdref.toString(), parse(fields), params, true);
|
|
102 | 95 |
solrAdministrator.createSolrIndex(p.get("host"), p.get("port"), mdref.toString(), p.get("numShards"), p.get("replicationFactor"), |
103 |
mdref.toString()); |
|
96 |
mdref.toString(), p.get("maxShardsPerNode"));
|
|
104 | 97 |
} |
105 | 98 |
client.getZkStateReader().close(); |
106 | 99 |
|
107 | 100 |
} catch (Exception e) { |
108 | 101 |
log.error("Error on creating IndexCollection", e); |
109 | 102 |
throw new IndexServiceException("Error on creating IndexCollection", e); |
110 |
} finally { |
|
111 |
if (client != null) { |
|
112 |
try { |
|
113 |
client.close(); |
|
114 |
} catch (IOException e) { |
|
115 |
throw new IndexServiceException("Error while closing client", e); |
|
116 |
} |
|
117 |
} |
|
118 | 103 |
} |
119 | 104 |
} |
120 | 105 |
|
121 | 106 |
@Override |
122 | 107 |
public void updateIndexCollection(final MetadataReference mdRef, final Document fields) throws IndexServiceException { |
123 |
CloudSolrClient client = null; |
|
124 |
try { |
|
125 |
client = getClient(); |
|
108 |
try(CloudSolrClient client = getClient()) { |
|
109 |
|
|
126 | 110 |
client.connect(); |
127 |
Map<String, String> params = new HashMap<>();
|
|
111 |
Map<String, String> params = Maps.newHashMap();
|
|
128 | 112 |
|
129 | 113 |
params.put("numShards", getServiceProperties().get("numShards")); |
130 | 114 |
params.put("replicationFactor", getServiceProperties().get("replicationFactor")); |
... | ... | |
132 | 116 |
for (IndexConfigFactory.CONFIG_PARAMS param_Name : IndexConfigFactory.CONFIG_PARAMS.values()) { |
133 | 117 |
params.put(param_Name.toString(), getServiceProperties().get(param_Name.toString())); |
134 | 118 |
} |
135 |
params.put(IndexConfigFactory.CONFIG_PARAMS.indexDataDir.toString(), mdRef.toString()); |
|
119 |
|
|
136 | 120 |
zkUtils.uploadZookeperConfig(client.getZkStateReader().getZkClient(), mdRef.toString(), fields, params, true); |
137 | 121 |
client.getZkStateReader().close(); |
138 |
client.shutdown(); |
|
139 | 122 |
solrAdministrator.reloadCollection(getServiceProperties().get("host"), getServiceProperties().get("port"), mdRef.toString()); |
140 | 123 |
|
141 | 124 |
} catch (Exception e) { |
142 | 125 |
log.error("Error on updating IndexCollection", e); |
143 | 126 |
throw new IndexServiceException("Error on updating IndexCollection", e); |
144 |
} finally { |
|
145 |
if (client != null) { |
|
146 |
client.shutdown(); |
|
147 |
} |
|
148 | 127 |
} |
149 | 128 |
} |
150 | 129 |
|
151 | 130 |
/** |
152 | 131 |
* Parses the fields parameter. |
153 |
* |
|
154 |
* @param fields the fields |
|
132 |
* |
|
133 |
* @param fields |
|
134 |
* the fields |
|
155 | 135 |
* @return the document |
156 |
* @throws IndexServiceException the index service exception |
|
136 |
* @throws IndexServiceException |
|
137 |
* the index service exception |
|
157 | 138 |
*/ |
158 | 139 |
private Document parse(final String fields) throws IndexServiceException { |
159 | 140 |
try { |
... | ... | |
165 | 146 |
|
166 | 147 |
/** |
167 | 148 |
* {@inheritDoc} |
149 |
* |
|
150 |
* @see eu.dnetlib.index.IndexServerDAO#getIndexCollection(eu.dnetlib.index.utils.MetadataReference) |
|
168 | 151 |
*/ |
169 | 152 |
@Override |
170 | 153 |
public IndexCollection getIndexCollection(final MetadataReference mdref) throws IndexServiceException { |
171 |
CloudSolrClient newServer = getClient(mdref); |
|
172 |
try { |
|
173 |
newServer.ping(); |
|
174 |
} catch (SolrServerException e) { |
|
175 |
e.printStackTrace(); |
|
176 |
} catch (IOException e) { |
|
177 |
e.printStackTrace(); |
|
178 |
} |
|
179 |
return new SolrIndexCollection(newServer); |
|
154 |
CloudSolrClient client = getClient(mdref); |
|
155 |
return new SolrIndexCollection(client); |
|
180 | 156 |
} |
181 | 157 |
|
182 | 158 |
/** |
183 | 159 |
* {@inheritDoc} |
160 |
* |
|
161 |
* @see eu.dnetlib.index.IndexServerDAO#getSchema(MetadataReference) |
|
184 | 162 |
*/ |
185 | 163 |
@Override |
186 | 164 |
public Map<String, ValueType> getSchema(final MetadataReference mdRef) throws IndexServiceException { |
187 |
CloudSolrClient server = getClient(mdRef); |
|
188 |
Map<String, ValueType> fields = solrAdministrator.getFieldNamesAndTypes(mdRef.toString(), server); |
|
189 |
try { |
|
190 |
server.close(); |
|
191 |
} catch (IOException e) { |
|
192 |
throw new IndexServiceException("Error on closing client"); |
|
193 |
} |
|
165 |
CloudSolrClient client = getClient(mdRef); |
|
166 |
Map<String, ValueType> fields = solrAdministrator.getFieldNamesAndTypes(mdRef.toString(), client); |
|
167 |
shutdown(mdRef); |
|
194 | 168 |
return fields; |
195 | 169 |
} |
196 | 170 |
|
197 | 171 |
/** |
198 | 172 |
* {@inheritDoc} |
173 |
* |
|
174 |
* @see eu.dnetlib.index.IndexServerDAO#getCqlValueTransformerMap(MetadataReference) |
|
199 | 175 |
*/ |
200 | 176 |
@Override |
201 | 177 |
public CqlValueTransformerMap getCqlValueTransformerMap(final MetadataReference mdRef) throws IndexServiceException { |
... | ... | |
204 | 180 |
|
205 | 181 |
/** |
206 | 182 |
* {@inheritDoc} |
183 |
* |
|
184 |
* @see eu.dnetlib.index.IndexServerDAO#getDocumentMapperFactory() |
|
207 | 185 |
*/ |
208 | 186 |
@Override |
209 | 187 |
public DocumentMapperFactory getDocumentMapperFactory() throws IndexServiceException { |
... | ... | |
212 | 190 |
|
213 | 191 |
/** |
214 | 192 |
* {@inheritDoc} |
193 |
* |
|
194 |
* @see eu.dnetlib.index.IndexServerDAO#shutdown(MetadataReference) |
|
215 | 195 |
*/ |
216 | 196 |
@Override |
217 | 197 |
public void shutdown(final MetadataReference mdRef) throws IndexServiceException { |
218 | 198 |
try { |
219 | 199 |
getClient(mdRef).close(); |
220 | 200 |
} catch (IOException e) { |
221 |
throw new IndexServiceException("Error on closing client", e);
|
|
201 |
throw new IndexServiceException(e); |
|
222 | 202 |
} |
223 | 203 |
|
224 | 204 |
} |
225 | 205 |
|
226 | 206 |
/** |
227 | 207 |
* Gets a server with the default collection set according to the given mdRef. |
228 |
* |
|
229 |
* @param mdRef the md ref |
|
208 |
* |
|
209 |
* @param mdRef |
|
210 |
* the md ref |
|
230 | 211 |
* @return a server instance |
231 |
* @throws IndexServiceException the index service exception |
|
212 |
* @throws IndexServiceException |
|
213 |
* the index service exception |
|
232 | 214 |
*/ |
233 | 215 |
private CloudSolrClient getClient(final MetadataReference mdRef) throws IndexServiceException { |
234 | 216 |
CloudSolrClient client = getClient(); |
... | ... | |
238 | 220 |
|
239 | 221 |
/** |
240 | 222 |
* Gets the server. |
241 |
* |
|
223 |
*
|
|
242 | 224 |
* @return a server instance |
243 | 225 |
*/ |
244 | 226 |
private CloudSolrClient getClient() { |
245 | 227 |
String address = getEndpoint().get(ADDRESS); |
246 | 228 |
log.info("connecting to address: " + address); |
247 |
return new CloudSolrClient(address); |
|
248 | 229 |
|
230 |
final ZkServers zk = ZkServers.newInstance(address); |
|
231 |
return new CloudSolrClient.Builder(zk.getHosts(), zk.getChroot()).build(); |
|
249 | 232 |
} |
250 | 233 |
|
251 | 234 |
/** |
252 | 235 |
* Gets the solr administrator. |
253 |
* |
|
236 |
*
|
|
254 | 237 |
* @return the solrAdministrator |
255 | 238 |
*/ |
256 | 239 |
public RemoteSolrAdministrator getSolrAdministrator() { |
... | ... | |
259 | 242 |
|
260 | 243 |
/** |
261 | 244 |
* Sets the solr administrator. |
262 |
* |
|
263 |
* @param solrAdministrator the solrAdministrator to set |
|
245 |
* |
|
246 |
* @param solrAdministrator |
|
247 |
* the solrAdministrator to set |
|
264 | 248 |
*/ |
265 | 249 |
@Required |
266 | 250 |
public void setSolrAdministrator(final RemoteSolrAdministrator solrAdministrator) { |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/SolrIndexCollection.java | ||
---|---|---|
3 | 3 |
import java.io.IOException; |
4 | 4 |
import java.util.Collection; |
5 | 5 |
import java.util.Iterator; |
6 |
import java.util.List; |
|
7 |
import java.util.stream.Collectors; |
|
8 | 6 |
|
9 |
import eu.dnetlib.clients.index.model.document.IndexDocument; |
|
10 |
import eu.dnetlib.clients.index.utils.IndexFieldUtility; |
|
11 |
import eu.dnetlib.enabling.tools.DnetStreamSupport; |
|
12 |
import eu.dnetlib.index.IndexCollection; |
|
13 |
import eu.dnetlib.index.solr.model.SolrIndexDocument; |
|
14 |
import eu.dnetlib.rmi.provision.IndexServiceException; |
|
15 | 7 |
import org.apache.commons.lang3.StringUtils; |
16 | 8 |
import org.apache.commons.logging.Log; |
17 | 9 |
import org.apache.commons.logging.LogFactory; |
... | ... | |
19 | 11 |
import org.apache.solr.client.solrj.response.UpdateResponse; |
20 | 12 |
import org.apache.solr.common.SolrInputDocument; |
21 | 13 |
|
14 |
import com.google.common.base.Function; |
|
15 |
import com.google.common.collect.Iterators; |
|
16 |
import com.google.common.collect.Lists; |
|
17 |
|
|
18 |
import eu.dnetlib.clients.index.model.document.IndexDocument; |
|
19 |
import eu.dnetlib.clients.index.utils.IndexFieldUtility; |
|
20 |
import eu.dnetlib.index.IndexCollection; |
|
21 |
import eu.dnetlib.index.solr.model.SolrIndexDocument; |
|
22 |
import eu.dnetlib.rmi.provision.IndexServiceException; |
|
23 |
|
|
22 | 24 |
/** |
23 | 25 |
* The Class SolrIndexCollection. |
24 | 26 |
*/ |
25 | 27 |
public class SolrIndexCollection implements IndexCollection { |
26 | 28 |
|
27 | 29 |
/** |
28 |
* The Constant STATUS_INDEX_OK. |
|
29 |
*/ |
|
30 |
public static final int STATUS_INDEX_OK = 0; |
|
31 |
/** |
|
32 | 30 |
* The log. |
33 | 31 |
*/ |
34 | 32 |
private static final Log log = LogFactory.getLog(SolrIndexCollection.class); // NOPMD by marko on 11/24/08 5:02 PM |
35 |
/** |
|
36 |
* The client. |
|
37 |
*/ |
|
33 |
|
|
34 |
/** The Constant STATUS_INDEX_OK. */ |
|
35 |
public static final int STATUS_INDEX_OK = 0; |
|
36 |
|
|
37 |
/** The client. */ |
|
38 | 38 |
private CloudSolrClient client; |
39 | 39 |
|
40 | 40 |
private boolean shutdown = false; |
... | ... | |
42 | 42 |
/** |
43 | 43 |
* The Constructor. |
44 | 44 |
* |
45 |
* @param newClient the client |
|
45 |
* @param newServer |
|
46 |
* the client |
|
46 | 47 |
*/ |
47 |
public SolrIndexCollection(final CloudSolrClient newClient) {
|
|
48 |
this.client = newClient;
|
|
48 |
public SolrIndexCollection(final CloudSolrClient newServer) {
|
|
49 |
this.client = newServer;
|
|
49 | 50 |
client.connect(); |
50 | 51 |
} |
51 | 52 |
|
52 | 53 |
/** |
53 | 54 |
* {@inheritDoc} |
55 |
* |
|
56 |
* @see eu.dnetlib.index.IndexCollection#add(eu.dnetlib.index.model.document.IndexDocument) |
|
54 | 57 |
*/ |
55 | 58 |
@Override |
56 | 59 |
public boolean add(final IndexDocument doc) throws IndexServiceException { |
57 | 60 |
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down"); |
61 |
|
|
58 | 62 |
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc; |
59 | 63 |
try { |
60 | 64 |
final UpdateResponse response = client.add(solrDocument.getSolrDocument()); |
... | ... | |
66 | 70 |
|
67 | 71 |
/** |
68 | 72 |
* {@inheritDoc} |
73 |
* |
|
74 |
* @see eu.dnetlib.index.IndexCollection#addAll(java.util.Iterator) |
|
69 | 75 |
*/ |
70 | 76 |
@Override |
71 | 77 |
public boolean addAll(final Iterator<IndexDocument> docs) throws IndexServiceException { |
72 | 78 |
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down"); |
79 |
final Iterator<SolrInputDocument> solrDocs = Iterators.transform(docs, new Function<IndexDocument, SolrInputDocument>() { |
|
73 | 80 |
|
74 |
final List<SolrInputDocument> solrDocs = DnetStreamSupport.generateStreamFromIterator(docs) |
|
75 |
.map(doc -> ((SolrIndexDocument) doc).getSolrDocument()).collect(Collectors.toList()); |
|
81 |
@Override |
|
82 |
public SolrInputDocument apply(final IndexDocument doc) { |
|
83 |
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc; |
|
84 |
return solrDocument.getSolrDocument(); |
|
85 |
} |
|
86 |
}); |
|
87 |
|
|
76 | 88 |
try { |
77 |
final UpdateResponse response = client.add(solrDocs);
|
|
89 |
final UpdateResponse response = client.add(Lists.newArrayList(solrDocs));
|
|
78 | 90 |
return response.getStatus() == 0; |
79 | 91 |
} catch (final Exception e) { |
80 | 92 |
throw new IndexServiceException("Unable to add document", e); |
... | ... | |
83 | 95 |
|
84 | 96 |
/** |
85 | 97 |
* {@inheritDoc} |
98 |
* |
|
99 |
* @see eu.dnetlib.index.IndexCollection#addAll(java.util.Collection) |
|
86 | 100 |
*/ |
87 | 101 |
@Override |
88 | 102 |
public boolean addAll(final Collection<IndexDocument> docs) throws IndexServiceException { |
... | ... | |
92 | 106 |
|
93 | 107 |
/** |
94 | 108 |
* {@inheritDoc} |
109 |
* |
|
110 |
* @see eu.dnetlib.index.IndexCollection#deleteIndex(java.lang.String) |
|
95 | 111 |
*/ |
96 | 112 |
@Override |
97 | 113 |
public boolean deleteIndex(final String dsId) throws IndexServiceException { |
... | ... | |
101 | 117 |
|
102 | 118 |
/** |
103 | 119 |
* {@inheritDoc} |
120 |
* |
|
121 |
* @see eu.dnetlib.index.IndexCollection#deleteByQuery(java.lang.String, java.lang.String) |
|
104 | 122 |
*/ |
105 | 123 |
@Override |
106 | 124 |
public boolean deleteByQuery(final String query, final String dsId) throws IndexServiceException { |
... | ... | |
112 | 130 |
/** |
113 | 131 |
* Do delete. |
114 | 132 |
* |
115 |
* @param query the query |
|
133 |
* @param query |
|
134 |
* the query |
|
116 | 135 |
* @return true, if do delete |
117 |
* @throws IndexServiceException the index service exception |
|
136 |
* @throws IndexServiceException |
|
137 |
* the index service exception |
|
118 | 138 |
*/ |
119 | 139 |
protected boolean doDelete(final String query) throws IndexServiceException { |
120 | 140 |
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down"); |
... | ... | |
128 | 148 |
|
129 | 149 |
/** |
130 | 150 |
* {@inheritDoc} |
151 |
* |
|
152 |
* @see eu.dnetlib.index.IndexCollection#commit() |
|
131 | 153 |
*/ |
132 | 154 |
@Override |
133 | 155 |
public boolean commit() throws IndexServiceException { |
... | ... | |
147 | 169 |
try { |
148 | 170 |
client.close(); |
149 | 171 |
} catch (IOException e) { |
150 |
log.error("Error on closing client", e);
|
|
172 |
throw new RuntimeException(e);
|
|
151 | 173 |
} |
152 | 174 |
shutdown = true; |
153 | 175 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/feed/StreamingInputDocumentFactory.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.StringReader; |
4 | 4 |
import java.io.StringWriter; |
5 |
import java.util.HashMap; |
|
5 | 6 |
import java.util.Iterator; |
6 |
import java.util.LinkedList; |
|
7 | 7 |
import java.util.List; |
8 |
import javax.xml.stream.*; |
|
8 |
|
|
9 |
import javax.xml.stream.XMLEventFactory; |
|
10 |
import javax.xml.stream.XMLEventReader; |
|
11 |
import javax.xml.stream.XMLEventWriter; |
|
12 |
import javax.xml.stream.XMLInputFactory; |
|
13 |
import javax.xml.stream.XMLOutputFactory; |
|
14 |
import javax.xml.stream.XMLStreamException; |
|
9 | 15 |
import javax.xml.stream.events.Namespace; |
10 | 16 |
import javax.xml.stream.events.StartElement; |
11 | 17 |
import javax.xml.stream.events.XMLEvent; |
12 | 18 |
|
13 |
import eu.dnetlib.index.solr.feed.ResultTransformer.Mode; |
|
14 | 19 |
import org.apache.solr.common.SolrInputDocument; |
15 | 20 |
|
21 |
import com.google.common.collect.Lists; |
|
22 |
|
|
23 |
import eu.dnetlib.index.solr.feed.ResultTransformer.Mode; |
|
24 |
|
|
16 | 25 |
/** |
17 | 26 |
* Optimized version of the document parser, drop in replacement of InputDocumentFactory. |
27 |
* |
|
18 | 28 |
* <p> |
19 |
* <p> |
|
20 | 29 |
* Faster because: |
21 | 30 |
* </p> |
22 | 31 |
* <ul> |
... | ... | |
25 | 34 |
* <li>Quickly serialize the 'result' element directly in a string.</li> |
26 | 35 |
* <li>Uses less memory: less pressure on GC and allows more threads to process this in parallel</li> |
27 | 36 |
* </ul> |
37 |
* |
|
28 | 38 |
* <p> |
29 |
* <p> |
|
30 | 39 |
* This class is fully reentrant and can be invoked in parallel. |
31 | 40 |
* </p> |
32 | 41 |
* |
33 | 42 |
* @author marko |
43 |
* |
|
34 | 44 |
*/ |
35 |
|
|
36 | 45 |
public class StreamingInputDocumentFactory extends InputDocumentFactory { |
37 | 46 |
|
38 | 47 |
protected static final String DEFAULTDNETRESULT = "dnetResult"; |
... | ... | |
43 | 52 |
|
44 | 53 |
protected static final String ROOT_ELEMENT = "indexRecord"; |
45 | 54 |
|
55 |
protected static final int MAX_FIELD_LENGTH = 25000; |
|
56 |
|
|
46 | 57 |
protected ThreadLocal<XMLInputFactory> inputFactory = new ThreadLocal<XMLInputFactory>() { |
47 | 58 |
|
48 | 59 |
@Override |
... | ... | |
67 | 78 |
} |
68 | 79 |
}; |
69 | 80 |
|
70 |
/** |
|
71 |
* {@inheritDoc} |
|
72 |
*/ |
|
73 | 81 |
@Override |
74 | 82 |
public SolrInputDocument parseDocument(final String version, final String inputDocument, final String dsId, final String resultName) |
75 | 83 |
throws XMLStreamException { |
76 | 84 |
return parseDocument(version, inputDocument, dsId, resultName, null); |
77 | 85 |
} |
78 | 86 |
|
79 |
/** |
|
80 |
* {@inheritDoc} |
|
81 |
* <p> |
|
82 |
* String, com.google.common.base.Function) |
|
83 |
*/ |
|
84 | 87 |
@Override |
85 | 88 |
public SolrInputDocument parseDocument(final String version, |
86 | 89 |
final String inputDocument, |
... | ... | |
89 | 92 |
final ResultTransformer resultTransformer) { |
90 | 93 |
|
91 | 94 |
final StringWriter results = new StringWriter(); |
92 |
final List<Namespace> nsList = new LinkedList<>();
|
|
95 |
final List<Namespace> nsList = Lists.newLinkedList();
|
|
93 | 96 |
try { |
94 | 97 |
|
95 | 98 |
XMLEventReader parser = inputFactory.get().createXMLEventReader(new StringReader(inputDocument)); |
96 | 99 |
|
97 |
final SolrInputDocument indexDocument = new SolrInputDocument(); |
|
100 |
final SolrInputDocument indexDocument = new SolrInputDocument(new HashMap<>());
|
|
98 | 101 |
|
99 | 102 |
while (parser.hasNext()) { |
100 | 103 |
final XMLEvent event = parser.nextEvent(); |
... | ... | |
137 | 140 |
} |
138 | 141 |
|
139 | 142 |
private List<Namespace> getNamespaces(final XMLEvent event) { |
140 |
final List<Namespace> res = new LinkedList<>();
|
|
143 |
final List<Namespace> res = Lists.newLinkedList();
|
|
141 | 144 |
@SuppressWarnings("unchecked") |
142 | 145 |
Iterator<Namespace> nsIter = event.asStartElement().getNamespaces(); |
143 | 146 |
while (nsIter.hasNext()) { |
... | ... | |
246 | 249 |
* Helper used to get the string from a text element. |
247 | 250 |
* |
248 | 251 |
* @param text |
249 |
* @return |
|
252 |
* @return the
|
|
250 | 253 |
*/ |
251 | 254 |
protected final String getText(final XMLEvent text) { |
252 | 255 |
if (text.isEndElement()) // log.warn("skipping because isEndOfElement " + text.asEndElement().getName().getLocalPart()); |
253 | 256 |
return ""; |
254 | 257 |
|
255 |
return text.asCharacters().getData(); |
|
258 |
final String data = text.asCharacters().getData(); |
|
259 |
if (data != null && data.length() > MAX_FIELD_LENGTH) { |
|
260 |
return data.substring(0, MAX_FIELD_LENGTH); |
|
261 |
} |
|
262 |
|
|
263 |
return data; |
|
256 | 264 |
} |
257 | 265 |
|
258 | 266 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/feed/ResultTransformer.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.index.solr.feed; |
2 | 2 |
|
3 |
import java.util.function.Function;
|
|
3 |
import com.google.common.base.Function;
|
|
4 | 4 |
|
5 | 5 |
/** |
6 | 6 |
* Created by claudio on 17/11/15. |
7 | 7 |
*/ |
8 | 8 |
public abstract class ResultTransformer implements Function<String, String> { |
9 | 9 |
|
10 |
public enum Mode {compress, empty, xslt, base64} |
|
11 |
|
|
10 | 12 |
protected Mode mode; |
11 | 13 |
|
12 | 14 |
public ResultTransformer(final Mode mode) { |
... | ... | |
21 | 23 |
this.mode = mode; |
22 | 24 |
} |
23 | 25 |
|
24 |
public enum Mode {compress, empty, xslt} |
|
25 |
|
|
26 | 26 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/feed/InputDocumentFactory.java | ||
---|---|---|
3 | 3 |
import java.text.ParseException; |
4 | 4 |
import java.text.SimpleDateFormat; |
5 | 5 |
import java.util.Arrays; |
6 |
import java.util.Date; |
|
6 | 7 |
import java.util.List; |
7 | 8 |
import javax.xml.stream.XMLStreamException; |
8 | 9 |
|
... | ... | |
10 | 11 |
import org.dom4j.DocumentException; |
11 | 12 |
|
12 | 13 |
/** |
14 |
* |
|
13 | 15 |
* @author claudio |
16 |
* |
|
14 | 17 |
*/ |
15 | 18 |
public abstract class InputDocumentFactory { |
16 | 19 |
|
... | ... | |
30 | 33 |
|
31 | 34 |
private final static List<String> dateFormats = Arrays.asList("yyyy-MM-dd'T'hh:mm:ss", "yyyy-MM-dd", "dd-MM-yyyy", "dd/MM/yyyy", "yyyy"); |
32 | 35 |
|
36 |
public abstract SolrInputDocument parseDocument(final String version, |
|
37 |
final String inputDocument, |
|
38 |
final String dsId, |
|
39 |
final String resultName) throws XMLStreamException; |
|
40 |
|
|
41 |
public abstract SolrInputDocument parseDocument(final String version, |
|
42 |
final String inputDocument, |
|
43 |
final String dsId, |
|
44 |
final String resultName, |
|
45 |
final ResultTransformer resultTransformer) throws XMLStreamException; |
|
46 |
|
|
33 | 47 |
/** |
34 | 48 |
* method return a solr-compatible string representation of a date |
35 | 49 |
* |
36 | 50 |
* @param date |
37 |
* @return |
|
51 |
* @return the parsed date
|
|
38 | 52 |
* @throws DocumentException |
39 | 53 |
* @throws ParseException |
40 | 54 |
*/ |
41 | 55 |
public static String getParsedDateField(final String date) { |
56 |
return new SimpleDateFormat(outFormat).format(tryParse(date)); |
|
57 |
} |
|
58 |
|
|
59 |
public static Date tryParse(final String date) { |
|
42 | 60 |
for (String formatString : dateFormats) { |
43 | 61 |
try { |
44 |
return new SimpleDateFormat(outFormat).format(new SimpleDateFormat(formatString).parse(date));
|
|
62 |
return new SimpleDateFormat(formatString).parse(date);
|
|
45 | 63 |
} catch (ParseException e) {} |
46 | 64 |
} |
47 | 65 |
throw new IllegalStateException("unable to parse date: " + date); |
48 | 66 |
} |
49 | 67 |
|
50 |
public abstract SolrInputDocument parseDocument(final String version, |
|
51 |
final String inputDocument, |
|
52 |
final String dsId, |
|
53 |
final String resultName) throws XMLStreamException; |
|
54 |
|
|
55 |
public abstract SolrInputDocument parseDocument(final String version, |
|
56 |
final String inputDocument, |
|
57 |
final String dsId, |
|
58 |
final String resultName, |
|
59 |
final ResultTransformer resultTransformer) throws XMLStreamException; |
|
60 |
|
|
61 | 68 |
public String parseDate(final String date) { |
62 | 69 |
return getParsedDateField(date); |
63 | 70 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/feed/SolrDocumentMapperFactory.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.util.Map; |
4 | 4 |
import java.util.function.Function; |
5 |
|
|
5 | 6 |
import javax.xml.stream.XMLStreamException; |
6 | 7 |
|
8 |
import org.springframework.beans.factory.annotation.Required; |
|
9 |
|
|
7 | 10 |
import eu.dnetlib.clients.index.model.Any.ValueType; |
8 | 11 |
import eu.dnetlib.clients.index.model.document.IndexDocument; |
9 | 12 |
import eu.dnetlib.clients.index.model.document.Status; |
10 |
import eu.dnetlib.utils.MetadataReference; |
|
11 | 13 |
import eu.dnetlib.index.feed.DocumentMapperFactory; |
12 | 14 |
import eu.dnetlib.index.solr.model.SolrIndexDocument; |
13 |
import org.springframework.beans.factory.annotation.Required;
|
|
15 |
import eu.dnetlib.utils.MetadataReference;
|
|
14 | 16 |
|
15 | 17 |
/** |
16 | 18 |
* A factory for creating SolrDocumentMapper objects. |
17 | 19 |
*/ |
18 | 20 |
public class SolrDocumentMapperFactory implements DocumentMapperFactory { |
19 | 21 |
|
20 |
protected static final String DNETRESULT = "result";
|
|
21 |
/**
|
|
22 |
* document factory used for the feed process.
|
|
23 |
*/
|
|
24 |
private InputDocumentFactory documentFactory;
|
|
22 |
protected static final String DNETRESULT = "result";
|
|
23 |
/**
|
|
24 |
* document factory used for the feed process.
|
|
25 |
*/
|
|
26 |
private InputDocumentFactory documentFactory;
|
|
25 | 27 |
|
26 |
private Function<String, IndexDocument> getRecordMapper(final Map<String, ValueType> schema, |
|
27 |
final String dsId, |
|
28 |
final String version) { |
|
29 | 28 |
|
30 |
return doc -> { |
|
31 |
SolrIndexDocument indexDocument = new SolrIndexDocument(schema, dsId); |
|
32 |
try { |
|
33 |
indexDocument.setContent(documentFactory.parseDocument(version, doc, dsId, DNETRESULT)); |
|
34 |
} catch (XMLStreamException e) { |
|
35 |
return indexDocument.setMarked(); |
|
36 |
} |
|
37 |
indexDocument.setStatus(Status.OK); |
|
38 |
return indexDocument; |
|
39 |
}; |
|
40 |
} |
|
29 |
private Function<String, IndexDocument> getRecordMapper(final Map<String, ValueType> schema, |
|
30 |
final String dsId, |
|
31 |
final String version) { |
|
41 | 32 |
|
42 |
@Override
|
|
43 |
public Function<String, IndexDocument> getRecordMapper(final Map<String, ValueType> schema,
|
|
44 |
final MetadataReference mdRef,
|
|
45 |
final String dsId,
|
|
46 |
final String version,
|
|
47 |
final boolean emptyResult) {
|
|
48 |
if (!emptyResult) return getRecordMapper(schema, dsId, version);
|
|
49 |
else {
|
|
50 |
return doc -> {
|
|
51 |
SolrIndexDocument indexDocument = new SolrIndexDocument(schema, dsId);
|
|
52 |
try {
|
|
33 |
return doc -> {
|
|
34 |
SolrIndexDocument indexDocument = new SolrIndexDocument(schema, dsId);
|
|
35 |
try {
|
|
36 |
indexDocument.setContent(documentFactory.parseDocument(version, doc, dsId, DNETRESULT));
|
|
37 |
} catch (XMLStreamException e) {
|
|
38 |
return indexDocument.setMarked();
|
|
39 |
}
|
|
40 |
indexDocument.setStatus(Status.OK);
|
|
41 |
return indexDocument;
|
|
42 |
};
|
|
43 |
}
|
|
53 | 44 |
|
54 |
ResultTransformer transformer = new ResultTransformer(ResultTransformer.Mode.empty) { |
|
55 |
@Override |
|
56 |
public String apply(String input) { |
|
57 |
return input; |
|
58 |
} |
|
59 |
}; |
|
60 |
indexDocument.setContent(documentFactory.parseDocument(version, doc, dsId, DNETRESULT, transformer)); |
|
61 |
} catch (XMLStreamException e) { |
|
62 |
return indexDocument.setMarked(); |
|
63 |
} |
|
64 |
indexDocument.setStatus(Status.OK); |
|
65 |
return indexDocument; |
|
66 |
}; |
|
67 |
} |
|
68 |
} |
|
45 |
@Override |
|
46 |
public Function<String, IndexDocument> getRecordMapper(final Map<String, ValueType> schema, final MetadataReference mdRef, final String dsId, final String version, final boolean emptyResult) { |
|
47 |
if (emptyResult == false) return getRecordMapper(schema, dsId, version); |
|
48 |
else { |
|
49 |
return doc -> { |
|
50 |
SolrIndexDocument indexDocument = new SolrIndexDocument(schema, dsId); |
|
51 |
try { |
|
69 | 52 |
|
70 |
/** |
|
71 |
* Gets the document factory. |
|
72 |
* |
|
73 |
* @return the documentFactory |
|
74 |
*/ |
|
75 |
public InputDocumentFactory getDocumentFactory() { |
|
76 |
return documentFactory; |
|
77 |
} |
|
53 |
ResultTransformer transformer = new ResultTransformer(ResultTransformer.Mode.empty) { |
|
54 |
@Override |
|
55 |
public String apply(String input) { |
|
56 |
return input; |
|
57 |
} |
|
58 |
}; |
|
59 |
indexDocument.setContent(documentFactory.parseDocument(version, doc, dsId, DNETRESULT, transformer)); |
|
60 |
} catch (XMLStreamException e) { |
|
61 |
return indexDocument.setMarked(); |
|
62 |
} |
|
63 |
indexDocument.setStatus(Status.OK); |
|
64 |
return indexDocument; |
|
65 |
}; |
|
66 |
} |
|
67 |
} |
|
78 | 68 |
|
79 |
/** |
|
80 |
* Sets the document factory. |
|
81 |
* |
|
82 |
* @param documentFactory the documentFactory to set |
|
83 |
*/ |
|
84 |
@Required |
|
85 |
public void setDocumentFactory(final InputDocumentFactory documentFactory) { |
|
86 |
this.documentFactory = documentFactory; |
|
87 |
} |
|
69 |
/** |
|
70 |
* Gets the document factory. |
|
71 |
* |
|
72 |
* @return the documentFactory |
|
73 |
*/ |
|
74 |
public InputDocumentFactory getDocumentFactory() { |
|
75 |
return documentFactory; |
|
76 |
} |
|
88 | 77 |
|
78 |
/** |
|
79 |
* Sets the document factory. |
|
80 |
* |
|
81 |
* @param documentFactory the documentFactory to set |
|
82 |
*/ |
|
83 |
@Required |
|
84 |
public void setDocumentFactory(final InputDocumentFactory documentFactory) { |
|
85 |
this.documentFactory = documentFactory; |
|
86 |
} |
|
87 |
|
|
89 | 88 |
} |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/browsing/BrowsingRow.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.index.solr.browsing; |
2 | 2 |
|
3 | 3 |
import java.util.List; |
4 |
|
|
4 | 5 |
import javax.xml.bind.annotation.XmlAccessType; |
5 | 6 |
import javax.xml.bind.annotation.XmlAccessorType; |
6 | 7 |
import javax.xml.bind.annotation.XmlElement; |
... | ... | |
8 | 9 |
|
9 | 10 |
import org.springframework.beans.factory.annotation.Required; |
10 | 11 |
|
12 |
|
|
11 | 13 |
/** |
14 |
* |
|
12 | 15 |
* serialization of the browsing result. |
13 |
* <p>
|
|
16 |
* |
|
14 | 17 |
* <row> <groupresult field="facetFieldName1"> <value>facetFieldValue</value> <count>1</count> </groupresult> |
15 |
* <p>
|
|
18 |
* |
|
16 | 19 |
* <groupresult field="facetFieldName2"> <value>facetFieldValue</value> <count>1</count> </groupresult> |
17 |
* <p>
|
|
20 |
* |
|
18 | 21 |
* </row> |
19 | 22 |
* |
20 | 23 |
* @author claudio |
24 |
* |
|
21 | 25 |
*/ |
22 | 26 |
@XmlRootElement(namespace = "", name = "row") |
23 | 27 |
@XmlAccessorType(XmlAccessType.FIELD) |
... | ... | |
26 | 30 |
@XmlElement(name = "groupresult", required = true) |
27 | 31 |
private List<GroupResult> groupresult; |
28 | 32 |
|
29 |
public BrowsingRow() { |
|
30 |
} |
|
33 |
public BrowsingRow() {} |
|
31 | 34 |
|
32 | 35 |
public BrowsingRow(final List<GroupResult> groupresult) { |
33 | 36 |
this.groupresult = groupresult; |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/browsing/GroupResult.java | ||
---|---|---|
16 | 16 |
* </pre> |
17 | 17 |
* |
18 | 18 |
* @author claudio |
19 |
* |
|
19 | 20 |
*/ |
20 | 21 |
@XmlRootElement(namespace = "", name = "groupresult") |
21 | 22 |
public class GroupResult { |
... | ... | |
26 | 27 |
|
27 | 28 |
private int count; |
28 | 29 |
|
29 |
public GroupResult() { |
|
30 |
} |
|
30 |
public GroupResult() {} |
|
31 | 31 |
|
32 | 32 |
/** |
33 | 33 |
* Builds a groupResult. |
34 | 34 |
* |
35 |
* @param fieldName |
|
36 |
* @param fieldValue |
|
35 | 37 |
* @param count |
36 | 38 |
*/ |
37 | 39 |
public GroupResult(final String name, final String value, final int count) { |
... | ... | |
53 | 55 |
return name; |
54 | 56 |
} |
55 | 57 |
|
58 |
public String getValue() { |
|
59 |
return value; |
|
60 |
} |
|
61 |
|
|
62 |
public int getCount() { |
|
63 |
return count; |
|
64 |
} |
|
65 |
|
|
56 | 66 |
@Required |
57 | 67 |
public void setName(final String name) { |
58 | 68 |
this.name = name; |
59 | 69 |
} |
60 | 70 |
|
61 |
public String getValue() { |
|
62 |
return value; |
|
63 |
} |
|
64 |
|
|
65 | 71 |
@Required |
66 | 72 |
public void setValue(final String value) { |
67 | 73 |
this.value = value; |
68 | 74 |
} |
69 | 75 |
|
70 |
public int getCount() { |
|
71 |
return count; |
|
72 |
} |
|
73 |
|
|
74 | 76 |
@Required |
75 | 77 |
public void setCount(final int count) { |
76 | 78 |
this.count = count; |
modules/dnet-data-provision-services/branches/saxonHE-SOLR-772/src/main/java/eu/dnetlib/index/solr/client/SolrIndexClient.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.index.solr.client; |
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 |
import java.text.SimpleDateFormat; |
|
4 | 5 |
import java.util.ArrayList; |
6 |
import java.util.Date; |
|
5 | 7 |
import java.util.HashMap; |
6 | 8 |
import java.util.List; |
7 | 9 |
import java.util.Map; |
10 |
import java.util.function.Function; |
|
8 | 11 |
|
12 |
import org.apache.commons.lang3.StringUtils; |
|
13 |
import org.apache.commons.logging.Log; |
|
14 |
import org.apache.commons.logging.LogFactory; |
|
15 |
import org.apache.solr.client.solrj.SolrClient; |
|
16 |
import org.apache.solr.client.solrj.SolrQuery; |
|
17 |
import org.apache.solr.client.solrj.SolrServerException; |
|
18 |
import org.apache.solr.client.solrj.impl.CloudSolrClient; |
|
19 |
import org.apache.solr.client.solrj.request.LukeRequest; |
|
20 |
import org.apache.solr.client.solrj.response.LukeResponse; |
|
21 |
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo; |
|
22 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
23 |
import org.apache.solr.client.solrj.response.UpdateResponse; |
|
24 |
import org.apache.solr.common.SolrInputDocument; |
|
25 |
|
|
9 | 26 |
import com.google.common.collect.BiMap; |
10 | 27 |
import com.google.common.collect.Maps; |
11 |
import eu.dnetlib.clients.index.client.AbstractIndexClient; |
|
28 |
|
|
12 | 29 |
import eu.dnetlib.clients.index.client.IndexClient; |
13 | 30 |
import eu.dnetlib.clients.index.client.IndexClientException; |
14 | 31 |
import eu.dnetlib.clients.index.client.response.BrowseEntry; |
... | ... | |
19 | 36 |
import eu.dnetlib.clients.index.query.QueryLanguage; |
20 | 37 |
import eu.dnetlib.clients.index.query.QueryResponseFactory; |
21 | 38 |
import eu.dnetlib.clients.index.query.QueryResponseParser; |
22 |
import eu.dnetlib.utils.MetadataReference; |
|
23 | 39 |
import eu.dnetlib.cql.CqlValueTransformerMap; |
24 | 40 |
import eu.dnetlib.index.query.SolrIndexQuery; |
25 | 41 |
import eu.dnetlib.index.query.SolrIndexQueryFactory; |
26 | 42 |
import eu.dnetlib.index.query.SolrIndexQueryResponse; |
27 | 43 |
import eu.dnetlib.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory; |
44 |
import eu.dnetlib.index.solr.feed.StreamingInputDocumentFactory; |
|
45 |
import eu.dnetlib.index.utils.ZkServers; |
|
46 |
import eu.dnetlib.miscutils.datetime.HumanTime; |
|
28 | 47 |
import eu.dnetlib.rmi.provision.BrowsingRow; |
29 | 48 |
import eu.dnetlib.rmi.provision.GroupResult; |
30 |
import org.apache.commons.logging.Log; |
|
31 |
import org.apache.commons.logging.LogFactory; |
|
32 |
import org.apache.solr.client.solrj.SolrServerException; |
|
33 |
import org.apache.solr.client.solrj.impl.CloudSolrClient; |
|
34 |
import org.apache.solr.client.solrj.request.LukeRequest; |
|
35 |
import org.apache.solr.client.solrj.response.LukeResponse; |
|
36 |
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo; |
|
37 |
import org.apache.solr.client.solrj.response.LukeResponse.FieldTypeInfo; |
|
38 |
import org.apache.solr.client.solrj.response.QueryResponse; |
|
49 |
import eu.dnetlib.rmi.provision.IndexServiceException; |
|
50 |
import eu.dnetlib.utils.MetadataReference; |
|
39 | 51 |
|
40 | 52 |
/** |
41 | 53 |
* The Class SolrIndexClient. |
42 | 54 |
*/ |
43 |
public class SolrIndexClient extends AbstractIndexClient implements IndexClient {
|
|
55 |
public class SolrIndexClient implements IndexClient { |
|
44 | 56 |
|
45 | 57 |
private static final Log log = LogFactory.getLog(SolrIndexClient.class); |
46 |
/** |
|
47 |
* The server. |
|
48 |
*/ |
|
58 |
|
|
59 |
private static final String INDEX_RECORD_RESULT_FIELD = "dnetResult"; |
|
60 |
|
|
61 |
private static String ZK_ADDRESS = "address"; |
|
62 |
|
|
63 |
/** The format. */ |
|
64 |
private String format; |
|
65 |
|
|
66 |
/** The layout. */ |
|
67 |
private String layout; |
|
68 |
|
|
69 |
/** The interpretation. */ |
|
70 |
private String interpretation; |
|
71 |
|
|
72 |
protected Map<String, String> serviceProperties; |
|
73 |
|
|
74 |
/** The client. */ |
|
49 | 75 |
private CloudSolrClient client; |
76 |
|
|
50 | 77 |
private SolrIndexQueryFactory solrIndexQueryFactory; |
51 |
/** |
|
52 |
* The query response factory. |
|
53 |
*/ |
|
78 |
|
|
79 |
/** The query response factory. */ |
|
54 | 80 |
private QueryResponseFactory<QueryResponse> queryResponseFactory; |
81 |
|
|
55 | 82 |
private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory; |
56 | 83 |
|
57 | 84 |
/** |
58 | 85 |
* The Constructor. |
59 | 86 |
* |
60 |
* @param format the format |
|
61 |
* @param layout the layout |
|
62 |
* @param interpretation the interpretation |
|
63 |
* @param serviceProperties the service properties |
|
87 |
* @param format |
|
88 |
* the format |
|
89 |
* @param layout |
|
90 |
* the layout |
|
91 |
* @param interpretation |
|
92 |
* the interpretation |
|
93 |
* @param serviceProperties |
|
94 |
* the service properties |
|
64 | 95 |
* @param tMapFactory |
65 | 96 |
*/ |
66 | 97 |
public SolrIndexClient(final String format, final String layout, final String interpretation, final Map<String, String> serviceProperties, |
67 | 98 |
final SolrIndexQueryFactory indexQueryFactory, final QueryResponseFactory<QueryResponse> queryResponseFactory, |
68 | 99 |
final SolrTypeBasedCqlValueTransformerMapFactory tMapFactory) { |
69 |
super(format, layout, interpretation, serviceProperties); |
|
70 |
|
|
71 |
log.debug(String.format("Created a new instance of the index of type %s-%s-%s", format, layout, interpretation)); |
|
100 |
this.format = format; |
|
101 |
this.layout = layout; |
|
102 |
this.interpretation = interpretation; |
|
103 |
this.serviceProperties = serviceProperties; |
|
72 | 104 |
this.solrIndexQueryFactory = indexQueryFactory; |
73 | 105 |
this.queryResponseFactory = queryResponseFactory; |
74 | 106 |
this.tMapFactory = tMapFactory; |
75 | 107 |
|
108 |
log.debug(String.format("Created a new instance of the index of type %s-%s-%s", format, layout, interpretation)); |
|
76 | 109 |
} |
77 | 110 |
|
78 |
public static Map<String, ValueType> getStringValueTypeMap(final Map<String, FieldInfo> fieldInfos, final Map<String, FieldTypeInfo> fieldTypeInfos) { |
|
79 |
final Map<String, ValueType> result = Maps.newHashMap(); |
|
80 |
for (FieldInfo fieldInfo : fieldInfos.values()) { |
|
81 |
FieldTypeInfo fieldTypeInfo = fieldTypeInfos.get(fieldInfo.getType()); |
|
82 |
final String fieldName = fieldTypeInfo.getName().toLowerCase(); |
|
83 |
final ValueType fieldType = resolveSolrTypeClassName(fieldName); |
|
84 |
result.put(fieldInfo.getName(), fieldType); |
|
85 |
} |
|
86 |
return result; |
|
87 |
} |
|
88 |
|
|
89 |
private static ValueType resolveSolrTypeClassName(final String solrTypeName) { |
|
90 |
if (solrTypeName.contains("LongField")) { |
|
91 |
return ValueType.LONG; |
|
92 |
} else if (solrTypeName.contains("IntField")) { |
|
93 |
return ValueType.LONG; |
|
94 |
} else if (solrTypeName.contains("short")) { |
|
95 |
return ValueType.LONG; |
|
96 |
} else if (solrTypeName.contains("float")) { |
|
97 |
return ValueType.DOUBLE; |
|
98 |
} else if (solrTypeName.contains("double")) { |
|
99 |
return ValueType.DOUBLE; |
|
100 |
} else if (solrTypeName.contains("date")) { |
|
101 |
return ValueType.DATETIME; |
|
102 |
} else { |
|
103 |
return ValueType.STRING; |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 | 111 |
/** |
108 | 112 |
* Do delete. |
109 | 113 |
* |
110 |
* @param query the CQL query |
|
114 |
* @param query |
|
115 |
* the CQL query |
|
111 | 116 |
* @return true, if do delete |
112 |
* @throws IndexClientException the index service exception |
|
117 |
* @throws IndexServiceException |
|
118 |
* the index service exception |
|
113 | 119 |
*/ |
114 | 120 |
@Override |
115 | 121 |
public long delete(final String query) throws IndexClientException { |
... | ... | |
120 | 126 |
String tquery = translatedQuery.getQuery(); |
121 | 127 |
translatedQuery.setQueryLimit(0); |
122 | 128 |
|
123 |
SolrIndexQueryResponse rsp = new SolrIndexQueryResponse(client.query(translatedQuery));
|
|
129 |
SolrIndexQueryResponse rsp = new SolrIndexQueryResponse(getClient().query(translatedQuery));
|
|
124 | 130 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(rsp, mdRef); |
125 | 131 |
long total = responseParser.getNumFound(); |
126 |
client.deleteByQuery(tquery);
|
|
127 |
client.commit();
|
|
132 |
getClient().deleteByQuery(tquery);
|
|
133 |
getClient().commit();
|
|
128 | 134 |
return total; |
129 | 135 |
} catch (Exception e) { |
130 | 136 |
throw new IndexClientException("unable to run delete by query: " + query, e); |
... | ... | |
135 | 141 |
* {@inheritDoc} |
136 | 142 |
* |
137 | 143 |
* @throws IndexClientException |
144 |
* |
|
145 |
* @see IndexClient#browse(String, List, int) |
|
138 | 146 |
*/ |
139 | 147 |
@Override |
140 | 148 |
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max) throws IndexClientException { |
... | ... | |
147 | 155 |
* {@inheritDoc} |
148 | 156 |
* |
149 | 157 |
* @throws IndexClientException |
158 |
* |
|
159 |
* @see IndexClient#browse(String, List, int, List) |
|
150 | 160 |
*/ |
151 | 161 |
@Override |
152 | 162 |
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max, final List<String> filterQuery) |
... | ... | |
164 | 174 |
|
165 | 175 |
} |
166 | 176 |
|
167 |
public SolrIndexQuery buildBrowseQuery(final String query, final List<String> browseFields, final int max, final MetadataReference mdRef)
|
|
177 |
private SolrIndexQuery buildBrowseQuery(final String query, final List<String> browseFields, final int max, final MetadataReference mdRef)
|
|
168 | 178 |
throws IndexClientException { |
169 | 179 |
log.debug("Browse request for the index collection for query:" + query); |
170 | 180 |
|
... | ... | |
189 | 199 |
final MetadataReference mdRef, |
190 | 200 |
final List<String> browseFields) throws IndexClientException { |
191 | 201 |
try { |
192 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(client.query(query));
|
|
202 |
SolrIndexQueryResponse response = new SolrIndexQueryResponse(getClient().query(query));
|
|
193 | 203 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(response, mdRef); |
194 | 204 |
List<BrowsingRow> results = responseParser.getBrowsingResults(); |
195 | 205 |
List<BrowseEntry> out = convertBrowseEntry(browseFields, results, responseParser.getAliases()); |
196 | 206 |
return out; |
197 |
} catch (Throwable e) {
|
|
207 |
} catch (SolrServerException | IOException e) {
|
|
198 | 208 |
throw new IndexClientException("Error on executing a query " + originalQuery, e); |
199 | 209 |
} |
200 | 210 |
} |
... | ... | |
203 | 213 |
* Creates the connection. |
204 | 214 |
* |
205 | 215 |
* @return the string |
206 |
* @throws IndexClientException the index client exception |
|
216 |
* @throws IndexClientException |
|
217 |
* the index client exception |
|
207 | 218 |
*/ |
208 | 219 |
private String getUrl() throws IndexClientException { |
209 | 220 |
String address = serviceProperties.get(ZK_ADDRESS); |
210 |
if (address == null) { throw new IndexClientException("Unable to load a solr client, missing zk address"); } |
|
221 |
if (StringUtils.isBlank(address)) { |
|
222 |
throw new IndexClientException("Unable to load a solr client, missing zk address"); |
|
223 |
} |
|
211 | 224 |
return address; |
212 | 225 |
} |
213 | 226 |
|
214 | 227 |
/** |
215 |
* Gets the server.
|
|
228 |
* Gets the client.
|
|
216 | 229 |
* |
217 |
* @return the server |
|
218 |
* @throws IndexClientException the index client exception |
|
230 |
* @return the client |
|
231 |
* @throws IndexClientException |
|
232 |
* the index client exception |
|
219 | 233 |
*/ |
220 |
public CloudSolrClient getClient() throws IndexClientException {
|
|
234 |
public SolrClient getClient() throws IndexClientException { |
|
221 | 235 |
if (this.client == null) { |
222 | 236 |
String url = getUrl(); |
223 | 237 |
log.debug("create new Client " + url); |
224 |
client = new CloudSolrClient(url); |
|
238 |
|
|
239 |
final ZkServers zk = ZkServers.newInstance(url); |
|
240 |
client = new CloudSolrClient.Builder(zk.getHosts(), zk.getChroot()).build(); |
|
241 |
|
|
242 |
client.connect(); |
|
225 | 243 |
client.setDefaultCollection(String.format("%s-%s-%s", getFormat(), getLayout(), getInterpretation())); |
226 | 244 |
try { |
227 | 245 |
client.ping(); |
... | ... | |
233 | 251 |
} |
234 | 252 |
|
235 | 253 |
/** |
236 |
* Sets the server.
|
|
254 |
* Sets the client.
|
|
237 | 255 |
* |
238 |
* @param client the Client |
|
256 |
* @param client |
|
257 |
* the client |
Also available in: Unified diff
solr 772 integration