Revision 57157
Added by Enrico Ottonello over 4 years ago
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 |
} |
Also available in: Unified diff
solr 772 integration