1
|
package eu.dnetlib.index.solr;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.util.Collection;
|
5
|
import java.util.Iterator;
|
6
|
|
7
|
import org.apache.commons.lang3.StringUtils;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.apache.solr.client.solrj.impl.CloudSolrClient;
|
11
|
import org.apache.solr.client.solrj.response.UpdateResponse;
|
12
|
import org.apache.solr.common.SolrInputDocument;
|
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
|
|
24
|
/**
|
25
|
* The Class SolrIndexCollection.
|
26
|
*/
|
27
|
public class SolrIndexCollection implements IndexCollection {
|
28
|
|
29
|
/**
|
30
|
* The log.
|
31
|
*/
|
32
|
private static final Log log = LogFactory.getLog(SolrIndexCollection.class); // NOPMD by marko on 11/24/08 5:02 PM
|
33
|
|
34
|
/** The Constant STATUS_INDEX_OK. */
|
35
|
public static final int STATUS_INDEX_OK = 0;
|
36
|
|
37
|
/** The client. */
|
38
|
private CloudSolrClient client;
|
39
|
|
40
|
private boolean shutdown = false;
|
41
|
|
42
|
/**
|
43
|
* The Constructor.
|
44
|
*
|
45
|
* @param newServer
|
46
|
* the client
|
47
|
*/
|
48
|
public SolrIndexCollection(final CloudSolrClient newServer) {
|
49
|
this.client = newServer;
|
50
|
client.connect();
|
51
|
}
|
52
|
|
53
|
/**
|
54
|
* {@inheritDoc}
|
55
|
*
|
56
|
* @see eu.dnetlib.index.IndexCollection#add(eu.dnetlib.index.model.document.IndexDocument)
|
57
|
*/
|
58
|
@Override
|
59
|
public boolean add(final IndexDocument doc) throws IndexServiceException {
|
60
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
61
|
|
62
|
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
|
63
|
try {
|
64
|
final UpdateResponse response = client.add(solrDocument.getSolrDocument());
|
65
|
return response.getStatus() == 0;
|
66
|
} catch (final Exception e) {
|
67
|
throw new IndexServiceException("Unable to add document", e);
|
68
|
}
|
69
|
}
|
70
|
|
71
|
/**
|
72
|
* {@inheritDoc}
|
73
|
*
|
74
|
* @see eu.dnetlib.index.IndexCollection#addAll(java.util.Iterator)
|
75
|
*/
|
76
|
@Override
|
77
|
public boolean addAll(final Iterator<IndexDocument> docs) throws IndexServiceException {
|
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>() {
|
80
|
|
81
|
@Override
|
82
|
public SolrInputDocument apply(final IndexDocument doc) {
|
83
|
final SolrIndexDocument solrDocument = (SolrIndexDocument) doc;
|
84
|
return solrDocument.getSolrDocument();
|
85
|
}
|
86
|
});
|
87
|
|
88
|
try {
|
89
|
final UpdateResponse response = client.add(Lists.newArrayList(solrDocs));
|
90
|
return response.getStatus() == 0;
|
91
|
} catch (final Exception e) {
|
92
|
throw new IndexServiceException("Unable to add document", e);
|
93
|
}
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* {@inheritDoc}
|
98
|
*
|
99
|
* @see eu.dnetlib.index.IndexCollection#addAll(java.util.Collection)
|
100
|
*/
|
101
|
@Override
|
102
|
public boolean addAll(final Collection<IndexDocument> docs) throws IndexServiceException {
|
103
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
104
|
return addAll(docs.iterator());
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* {@inheritDoc}
|
109
|
*
|
110
|
* @see eu.dnetlib.index.IndexCollection#deleteIndex(java.lang.String)
|
111
|
*/
|
112
|
@Override
|
113
|
public boolean deleteIndex(final String dsId) throws IndexServiceException {
|
114
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
115
|
return doDelete(IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* {@inheritDoc}
|
120
|
*
|
121
|
* @see eu.dnetlib.index.IndexCollection#deleteByQuery(java.lang.String, java.lang.String)
|
122
|
*/
|
123
|
@Override
|
124
|
public boolean deleteByQuery(final String query, final String dsId) throws IndexServiceException {
|
125
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
126
|
if (StringUtils.isBlank(dsId)) return doDelete(query);
|
127
|
return doDelete(query + " AND " + IndexFieldUtility.DS_ID + " : \"" + dsId + "\"");
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Do delete.
|
132
|
*
|
133
|
* @param query
|
134
|
* the query
|
135
|
* @return true, if do delete
|
136
|
* @throws IndexServiceException
|
137
|
* the index service exception
|
138
|
*/
|
139
|
protected boolean doDelete(final String query) throws IndexServiceException {
|
140
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
141
|
try {
|
142
|
log.debug("delete by query: " + query);
|
143
|
return client.deleteByQuery(query).getStatus() == STATUS_INDEX_OK;
|
144
|
} catch (final Exception e) {
|
145
|
throw new IndexServiceException("unable to run delete by query: " + query, e);
|
146
|
}
|
147
|
}
|
148
|
|
149
|
/**
|
150
|
* {@inheritDoc}
|
151
|
*
|
152
|
* @see eu.dnetlib.index.IndexCollection#commit()
|
153
|
*/
|
154
|
@Override
|
155
|
public boolean commit() throws IndexServiceException {
|
156
|
if (isShutdown()) throw new IndexServiceException("Please get another SolrIndexCollection: this has been shut down");
|
157
|
try {
|
158
|
log.info("performing commit");
|
159
|
final UpdateResponse rsp = client.commit();
|
160
|
log.info(String.format("commit completed in %s, status %s", rsp.getElapsedTime(), rsp.getStatus()));
|
161
|
return rsp.getStatus() == STATUS_INDEX_OK;
|
162
|
} catch (final Throwable e) {
|
163
|
throw new IndexServiceException("unable to perform index commit", e);
|
164
|
}
|
165
|
}
|
166
|
|
167
|
@Override
|
168
|
public void shutdown() {
|
169
|
try {
|
170
|
client.close();
|
171
|
} catch (IOException e) {
|
172
|
throw new RuntimeException(e);
|
173
|
}
|
174
|
shutdown = true;
|
175
|
}
|
176
|
|
177
|
public boolean isShutdown() {
|
178
|
return shutdown;
|
179
|
}
|
180
|
|
181
|
public void setShutdown(final boolean shutdown) {
|
182
|
this.shutdown = shutdown;
|
183
|
}
|
184
|
|
185
|
}
|