PublisherSearchRepositorySolrj.java
2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package org.legrog.entities;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.enterprise.inject.Default;
import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Implementation of PublisherSearchRepository using SolrJ
*/
@Default
public class PublisherSearchRepositorySolrj implements PublisherSearchRepository {
Logger logger = LoggerFactory.getLogger(getClass());
SolrClient solrClient;
protected static String collectionName = "publishers";
@Inject
PublisherSearchRepositorySolrj(SolrClient solrClient) {
this.solrClient = solrClient;
}
//no args constructor to make it proxyable
PublisherSearchRepositorySolrj() {
}
@Override
public IndexedPublisher save(IndexedPublisher indexedPublisher) throws IndexingException {
try {
UpdateResponse updateResponse = solrClient.addBean(collectionName, indexedPublisher, 1);
logger.trace("validatePublisherVersion SolrJ UpdateResponse {}", updateResponse);
} catch (IOException ioe) {
throw new IndexingException(ioe);
} catch (SolrServerException sse) {
logger.error("SolrServerException {}", sse);
throw new IndexingException(sse.getRootCause());
}
return indexedPublisher;
}
@Override
public List<IndexedPublisher> search(String string) throws SearchingException {
SolrQuery solrQuery = new SolrQuery(string);
QueryResponse queryResponse;
try {
queryResponse = solrClient.query(collectionName, solrQuery);
} catch (IOException ioe) {
throw new SearchingException(ioe);
} catch (SolrServerException sse) {
logger.error("SolrServerException {}", sse);
throw new SearchingException(sse.getRootCause());
}
if (queryResponse != null) {
return queryResponse.getBeans(IndexedPublisher.class);
} else {
return new ArrayList<>();
}
}
@Override
public void reindex(List<IndexedPublisher> indexedPublishers) throws IndexingException {
try {
UpdateResponse updateResponse = solrClient.addBeans(collectionName, indexedPublishers);
solrClient.commit(collectionName);
logger.trace("reindex inxdexedPublishers SolrJ UpdateResponse {}", updateResponse);
} catch (IOException ioe) {
throw new IndexingException(ioe);
} catch (SolrServerException sse) {
logger.error("SolrServerException {}", sse);
throw new IndexingException(sse.getRootCause());
}
}
}