use more predefined Solr query parameter constants

- use CommonParams and DisMaxParams constants
- fix typo in get sort parameter
- getDocumentCountByParams redundant implementation and risk of not optimized call (row parameter unspecified) -> as only used from getCountByQuery removed from interface
pull/1/head
reger 11 years ago
parent f9db5dd6c5
commit 7584352e7b

@ -49,6 +49,8 @@ import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.DisMaxParams;
import org.apache.solr.common.params.FacetParams;
import org.apache.solr.common.params.ModifiableSolrParams;
@ -293,7 +295,7 @@ public abstract class AbstractSolrConnector implements SolrConnector {
//}
params.clearSorts();
if (sort != null) {
params.set("sort", sort);
params.set(CommonParams.SORT, sort);
}
params.setRows(count);
params.setStart(offset);
@ -301,17 +303,10 @@ public abstract class AbstractSolrConnector implements SolrConnector {
if (fields.length > 0) params.setFields(fields);
params.setIncludeScore(false);
params.setParam("defType", "edismax");
params.setParam("qf", CollectionSchema.text_t.getSolrFieldName() + "^1.0");
params.setParam(DisMaxParams.QF, CollectionSchema.text_t.getSolrFieldName() + "^1.0");
return params;
}
@Override
public long getDocumentCountByParams(ModifiableSolrParams params) throws IOException, SolrException {
final SolrDocumentList sdl = getDocumentListByParams(params);
return sdl == null ? 0 : sdl.getNumFound();
}
/**
* check if a given document, identified by url hash as document id exists
* @param id the url hash and document id
@ -351,7 +346,7 @@ public abstract class AbstractSolrConnector implements SolrConnector {
// construct query
final SolrQuery params = new SolrQuery();
params.setQuery(querystring);
params.setRows(0);
params.setRows(0); // essential to just get count
params.setStart(0);
params.setFacet(false);
params.clearSorts();
@ -359,7 +354,8 @@ public abstract class AbstractSolrConnector implements SolrConnector {
params.setIncludeScore(false);
// query the server
return getDocumentCountByParams(params);
final SolrDocumentList sdl = getDocumentListByParams(params);
return sdl == null ? 0 : sdl.getNumFound();
}
/**

@ -333,12 +333,6 @@ public class ConcurrentUpdateSolrConnector implements SolrConnector {
}
return sdl;
}
@Override
public long getDocumentCountByParams(ModifiableSolrParams params) throws IOException, SolrException {
final SolrDocumentList sdl = getDocumentListByParams(params);
return sdl == null ? 0 : sdl.getNumFound();
}
@Override
public SolrDocumentList getDocumentListByQuery(String querystring, String sort, int offset, int count, String... fields) throws IOException, SolrException {

@ -46,6 +46,7 @@ import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
@ -198,9 +199,9 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo
final long startTime = System.currentTimeMillis();
// during the solr query we set the thread name to the query string to get more debugging info in thread dumps
String q = req.getParams().get("q");
String fq = req.getParams().get("fq");
String sort = req.getParams().get("sor");
String q = req.getParams().get(CommonParams.Q);
String fq = req.getParams().get(CommonParams.FQ);
String sort = req.getParams().get(CommonParams.SORT);
String threadname = Thread.currentThread().getName();
if (q != null) Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq) + (sort == null ? "" : ", sort = " + sort)); // for debugging in Threaddump
@ -309,8 +310,8 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo
public QueryResponse getResponseByParams(ModifiableSolrParams params) throws IOException {
if (this.server == null) throw new IOException("server disconnected");
// during the solr query we set the thread name to the query string to get more debugging info in thread dumps
String q = params.get("q");
String fq = params.get("fq");
String q = params.get(CommonParams.Q);
String fq = params.get(CommonParams.FQ);
String threadname = Thread.currentThread().getName();
if (q != null) Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq));
QueryResponse rsp;
@ -339,9 +340,9 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo
public SolrDocumentList getDocumentListByParams(ModifiableSolrParams params) throws IOException, SolrException {
SolrQueryRequest req = this.request(params);
SolrQueryResponse response = null;
String q = params.get("q");
String fq = params.get("fq");
String sort = params.get("sort");
String q = params.get(CommonParams.Q);
String fq = params.get(CommonParams.FQ);
String sort = params.get(CommonParams.SORT);
String threadname = Thread.currentThread().getName();
try {
if (q != null) Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq) + (sort == null ? "" : ", sort = " + sort)); // for debugging in Threaddump
@ -355,21 +356,6 @@ public class EmbeddedSolrConnector extends SolrServerConnector implements SolrCo
}
}
@Override
public long getDocumentCountByParams(ModifiableSolrParams params) throws IOException, SolrException {
SolrQueryRequest req = this.request(params);
SolrQueryResponse response = null;
try {
response = this.query(req);
if (response == null) throw new IOException("response == null");
NamedList<?> nl = response.getValues();
ResultContext resultContext = (ResultContext) nl.get("response");
return resultContext == null ? 0 : resultContext.docs.matches();
} finally {
req.close();
SolrRequestInfo.clearRequestInfo();
}
}
private class DocListSearcher {
private SolrQueryRequest request;

@ -34,6 +34,7 @@ import org.apache.solr.client.solrj.impl.BinaryResponseParser;
import org.apache.solr.client.solrj.impl.XMLResponseParser;
import org.apache.solr.client.solrj.request.QueryRequest;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.util.NamedList;
@ -98,8 +99,8 @@ public class RemoteSolrConnector extends SolrServerConnector implements SolrConn
@Override
public QueryResponse getResponseByParams(ModifiableSolrParams params) throws IOException {
// during the solr query we set the thread name to the query string to get more debugging info in thread dumps
String q = params.get("q");
String fq = params.get("fq");
String q = params.get(CommonParams.Q);
String fq = params.get(CommonParams.FQ);
String threadname = Thread.currentThread().getName();
if (q != null) Thread.currentThread().setName("solr query: q = " + q + (fq == null ? "" : ", fq = " + fq));

@ -166,16 +166,7 @@ public interface SolrConnector extends Iterable<String> /* Iterable of document
* @throws SolrException
*/
public SolrDocumentList getDocumentListByParams(ModifiableSolrParams params) throws IOException;
/**
* get the number of results for a query response
* @param params
* @return
* @throws IOException
* @throws SolrException
*/
public long getDocumentCountByParams(ModifiableSolrParams params) throws IOException;
/**
* get a query result from solr
* to get all results set the query String to "*:*"

@ -46,6 +46,7 @@ import org.apache.solr.client.solrj.request.UpdateRequest;
import org.apache.solr.client.solrj.response.LukeResponse.FieldInfo;
import org.apache.solr.client.solrj.response.LukeResponse;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.params.CommonParams;
public abstract class SolrServerConnector extends AbstractSolrConnector implements SolrConnector {
@ -291,9 +292,9 @@ public abstract class SolrServerConnector extends AbstractSolrConnector implemen
public SolrDocumentList getDocumentListByParams(ModifiableSolrParams params) throws IOException {
if (this.server == null) throw new IOException("server disconnected");
// during the solr query we set the thread name to the query string to get more debugging info in thread dumps
String q = params.get("q");
String fq = params.get("fq");
String sort = params.get("sort");
String q = params.get(CommonParams.Q);
String fq = params.get(CommonParams.FQ);
String sort = params.get(CommonParams.SORT);
String threadname = Thread.currentThread().getName();
QueryResponse rsp;
int retry = 0;

@ -32,6 +32,7 @@ import net.yacy.document.SentenceReader;
import net.yacy.search.schema.CollectionSchema;
import org.apache.lucene.document.Document;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.common.util.XML;
@ -87,7 +88,7 @@ public class GrepHTMLResponseWriter implements QueryResponseWriter {
SolrParams params = request.getOriginalParams();
String grep = params.get("grep");
String query = "";
String q = params.get("q"); if (q == null) q = "";
String q = params.get(CommonParams.Q); if (q == null) q = "";
int p = q.indexOf(':');
if (p >= 0) {
int r = q.charAt(p + 1) == '"' ? q.indexOf(p + 2, '"') : q.indexOf(' ');

@ -65,6 +65,7 @@ import net.yacy.search.schema.CollectionSchema;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrQuery.SortClause;
import org.apache.solr.common.params.CommonParams;
import org.apache.solr.common.params.DisMaxParams;
import org.apache.solr.common.params.FacetParams;
public final class QueryParams {
@ -363,7 +364,7 @@ public final class QueryParams {
String oldfq = params.get(CommonParams.FQ);
params.setParam(CommonParams.FQ, oldfq == null || oldfq.length() == 0 ? fq : "(" + oldfq + ") AND (" + fq + ")");
}
if (bq.length() > 0) params.setParam("bq", bq);
if (bq.length() > 0) params.setParam(DisMaxParams.BQ, bq);
if (bf.length() > 0) params.setParam("boost", bf); // a boost function extension, see http://wiki.apache.org/solr/ExtendedDisMax#bf_.28Boost_Function.2C_additive.29
// prepare result
@ -389,7 +390,7 @@ public final class QueryParams {
bq.append(" OR ").append(CollectionSchema.url_file_ext_s.getSolrFieldName()).append(":\"tif\"");
bq.append(" OR ").append(CollectionSchema.url_file_ext_s.getSolrFieldName()).append(":\"tiff\"");
bq.append(" OR ").append(CollectionSchema.url_file_ext_s.getSolrFieldName()).append(":\"png\"");
params.setParam("bq", bq.toString());
params.setParam(DisMaxParams.BQ, bq.toString());
// prepare result
ConcurrentLog.info("Protocol", "SOLR QUERY: " + params.toString());
@ -400,7 +401,7 @@ public final class QueryParams {
private SolrQuery getBasicParams(boolean getFacets) {
final SolrQuery params = new SolrQuery();
params.setParam("defType", "edismax");
params.setParam("qf", CollectionSchema.text_t.getSolrFieldName() + "^1.0");
params.setParam(DisMaxParams.QF, CollectionSchema.text_t.getSolrFieldName() + "^1.0");
params.setStart(this.offset);
params.setRows(this.itemsPerPage);
params.setFacet(false);

Loading…
Cancel
Save