Revision 30477
Added by Sandro La Bruzzo almost 10 years ago
SolrIndexClient.java | ||
---|---|---|
6 | 6 |
import java.util.List; |
7 | 7 |
import java.util.Map; |
8 | 8 |
|
9 |
import org.apache.commons.logging.Log; |
|
10 |
import org.apache.commons.logging.LogFactory; |
|
9 | 11 |
import org.apache.solr.client.solrj.SolrServerException; |
10 | 12 |
import org.apache.solr.client.solrj.impl.HttpSolrServer; |
11 | 13 |
import org.apache.solr.client.solrj.request.LukeRequest; |
12 | 14 |
import org.apache.solr.client.solrj.response.LukeResponse; |
13 | 15 |
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo; |
14 | 16 |
import org.apache.solr.client.solrj.response.QueryResponse; |
17 |
import org.apache.solr.client.solrj.response.UpdateResponse; |
|
15 | 18 |
|
16 | 19 |
import com.google.common.collect.BiMap; |
17 | 20 |
import com.google.common.collect.Maps; |
18 | 21 |
|
19 | 22 |
import eu.dnetlib.data.provision.index.rmi.BrowsingRow; |
20 | 23 |
import eu.dnetlib.data.provision.index.rmi.GroupResult; |
24 |
import eu.dnetlib.data.provision.index.rmi.IndexServiceException; |
|
21 | 25 |
import eu.dnetlib.functionality.index.client.AbstractIndexClient; |
22 | 26 |
import eu.dnetlib.functionality.index.client.IndexClient; |
23 | 27 |
import eu.dnetlib.functionality.index.client.IndexClientException; |
... | ... | |
51 | 55 |
|
52 | 56 |
private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory; |
53 | 57 |
|
58 |
private static final Log log = LogFactory.getLog(SolrIndexClient.class); |
|
59 |
|
|
60 |
private static final int STATUS_INDEX_OK = 0; |
|
61 |
|
|
54 | 62 |
/** |
55 | 63 |
* The Constructor. |
56 | 64 |
* |
... | ... | |
68 | 76 |
final SolrIndexQueryFactory indexQueryFactory, final QueryResponseFactory<QueryResponse> queryResponseFactory, |
69 | 77 |
final SolrTypeBasedCqlValueTransformerMapFactory tMapFactory) { |
70 | 78 |
super(format, layout, interpretation, serviceProperties); |
79 |
|
|
80 |
log.debug(String.format("Created a new instance of the index of type %s-%s-%s", format, layout, interpretation)); |
|
71 | 81 |
this.solrIndexQueryFactory = indexQueryFactory; |
72 | 82 |
this.queryResponseFactory = queryResponseFactory; |
73 | 83 |
this.tMapFactory = tMapFactory; |
... | ... | |
75 | 85 |
} |
76 | 86 |
|
77 | 87 |
/** |
88 |
* Do delete. |
|
89 |
* |
|
90 |
* @param query |
|
91 |
* the query |
|
92 |
* @return true, if do delete |
|
93 |
* @throws IndexServiceException |
|
94 |
* the index service exception |
|
95 |
*/ |
|
96 |
@Override |
|
97 |
public long delete(final String query) throws IndexClientException { |
|
98 |
try { |
|
99 |
log.debug("delete by query: " + query); |
|
100 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
|
101 |
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef); |
|
102 |
String tquery = translatedQuery.getQuery(); |
|
103 |
translatedQuery.setQueryLimit(0); |
|
104 |
|
|
105 |
SolrIndexQueryResponse rsp = new SolrIndexQueryResponse(server.query(translatedQuery)); |
|
106 |
QueryResponseParser responseParser = queryResponseFactory.getQueryResponseParser(rsp, mdRef); |
|
107 |
long total = responseParser.getNumFound(); |
|
108 |
|
|
109 |
UpdateResponse response = server.deleteByQuery(tquery); |
|
110 |
|
|
111 |
server.commit(); |
|
112 |
return total; |
|
113 |
} catch (Exception e) { |
|
114 |
throw new IndexClientException("unable to run delete by query: " + query, e); |
|
115 |
} |
|
116 |
} |
|
117 |
|
|
118 |
/** |
|
78 | 119 |
* {@inheritDoc} |
79 | 120 |
* |
80 | 121 |
* @throws IndexClientException |
... | ... | |
83 | 124 |
*/ |
84 | 125 |
@Override |
85 | 126 |
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max) throws IndexClientException { |
127 |
log.debug("Browse request for the index collection for query:" + query); |
|
86 | 128 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
87 | 129 |
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef); |
88 | 130 |
translatedQuery.setFacet(true); |
89 | 131 |
|
90 | 132 |
if (browseFields != null) { |
133 |
log.debug("Browsing fields:"); |
|
91 | 134 |
for (String field : browseFields) { |
92 | 135 |
translatedQuery.addFacetField(field); |
136 |
log.debug("- " + field); |
|
93 | 137 |
|
94 | 138 |
} |
95 | 139 |
translatedQuery.setFacetLimit(max); |
140 |
log.debug("max number of browsing field :" + max); |
|
96 | 141 |
} |
97 | 142 |
|
98 | 143 |
try { |
... | ... | |
118 | 163 |
@Override |
119 | 164 |
public List<BrowseEntry> browse(final String query, final List<String> browseFields, final int max, final List<String> filterQuery) |
120 | 165 |
throws IndexClientException { |
166 |
log.debug("Browse request for the index collection for query:" + query); |
|
121 | 167 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
122 | 168 |
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef); |
123 | 169 |
translatedQuery.setFacet(true); |
124 | 170 |
if (filterQuery != null) { |
171 |
log.debug("Filter Query:"); |
|
125 | 172 |
for (String fq : filterQuery) { |
126 | 173 |
translatedQuery.addFilterQuery(fq); |
174 |
log.debug("- " + fq); |
|
127 | 175 |
} |
128 | 176 |
} |
129 | 177 |
|
130 | 178 |
if (browseFields != null) { |
179 |
log.debug("Browsing fields:"); |
|
131 | 180 |
for (String field : browseFields) { |
132 | 181 |
translatedQuery.addFacetField(field); |
182 |
log.debug("- " + field); |
|
133 | 183 |
|
134 | 184 |
} |
135 | 185 |
translatedQuery.setFacetLimit(max); |
186 |
log.debug("max number of browsing field :" + max); |
|
136 | 187 |
} |
137 | 188 |
|
138 | 189 |
try { |
... | ... | |
172 | 223 |
public HttpSolrServer getServer() throws IndexClientException { |
173 | 224 |
if (this.server == null) { |
174 | 225 |
String url = createConnection(); |
226 |
log.debug("create new Client " + url); |
|
175 | 227 |
server = new HttpSolrServer(url); |
176 | 228 |
try { |
177 | 229 |
server.ping(); |
... | ... | |
194 | 246 |
|
195 | 247 |
@Override |
196 | 248 |
public LookupResponse lookup(final String query, final List<String> filterQuery, final int from, final int to) throws IndexClientException { |
249 |
log.debug("lookup request for the index collection for query:" + query); |
|
197 | 250 |
MetadataReference mdRef = new MetadataReference(getFormat(), getLayout(), getInterpretation()); |
198 | 251 |
SolrIndexQuery translatedQuery = (SolrIndexQuery) solrIndexQueryFactory.getIndexQuery(QueryLanguage.CQL, query, this, mdRef); |
199 | 252 |
translatedQuery.setQueryOffset(from); |
Also available in: Unified diff
implemented delete by query on index client