SOLR-9385: Add QParser.getParser(String,SolrQueryRequest) variant.

This commit is contained in:
Christine Poerschke 2016-08-09 13:08:40 +01:00
parent 36d67aaba9
commit 9d784a6693
27 changed files with 54 additions and 32 deletions

View File

@ -245,6 +245,8 @@ Other Changes
* SOLR-9392: Fixed CDCR Test failures which were due to leaked resources. (shalin) * SOLR-9392: Fixed CDCR Test failures which were due to leaked resources. (shalin)
* SOLR-9385: Add QParser.getParser(String,SolrQueryRequest) variant. (Christine Poerschke)
================== 6.1.0 ================== ================== 6.1.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release. Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

View File

@ -604,7 +604,7 @@ public class FacetingAccumulator extends BasicAccumulator implements FacetValueA
QueryFacetAccumulator qAcc = new QueryFacetAccumulator(this,qfr.getName(),query); QueryFacetAccumulator qAcc = new QueryFacetAccumulator(this,qfr.getName(),query);
final Query q; final Query q;
try { try {
q = QParser.getParser(query, null, queryRequest).getQuery(); q = QParser.getParser(query, queryRequest).getQuery();
} catch( SyntaxError e ){ } catch( SyntaxError e ){
throw new SolrException(ErrorCode.BAD_REQUEST,"Invalid query '"+query+"'",e); throw new SolrException(ErrorCode.BAD_REQUEST,"Invalid query '"+query+"'",e);
} }

View File

@ -160,7 +160,7 @@ public class BlobHandler extends RequestHandlerBase implements PluginInfoInitial
} else { } else {
String q = "blobName:{0}"; String q = "blobName:{0}";
if (version != -1) q = "id:{0}/{1}"; if (version != -1) q = "id:{0}/{1}";
QParser qparser = QParser.getParser(StrUtils.formatString(q, blobName, version), "lucene", req); QParser qparser = QParser.getParser(StrUtils.formatString(q, blobName, version), req);
final TopDocs docs = req.getSearcher().search(qparser.parse(), 1, new Sort(new SortField("version", SortField.Type.LONG, true))); final TopDocs docs = req.getSearcher().search(qparser.parse(), 1, new Sort(new SortField("version", SortField.Type.LONG, true)));
if (docs.totalHits > 0) { if (docs.totalHits > 0) {
rsp.add(ReplicationHandler.FILE_STREAM, new SolrCore.RawWriter() { rsp.add(ReplicationHandler.FILE_STREAM, new SolrCore.RawWriter() {

View File

@ -125,7 +125,7 @@ public class MoreLikeThisHandler extends RequestHandlerBase
filters = new ArrayList<>(); filters = new ArrayList<>();
for (String fq : fqs) { for (String fq : fqs) {
if (fq != null && fq.trim().length() != 0) { if (fq != null && fq.trim().length() != 0) {
QParser fqp = QParser.getParser(fq, null, req); QParser fqp = QParser.getParser(fq, req);
filters.add(fqp.getQuery()); filters.add(fqp.getQuery());
} }
} }

View File

@ -176,7 +176,7 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
query = rb.getQuery(); query = rb.getQuery();
} else { } else {
try { try {
QParser parser = QParser.getParser(qs, null, req); QParser parser = QParser.getParser(qs, req);
query = parser.getQuery(); query = parser.getQuery();
} catch (Exception e) { } catch (Exception e) {
throw new IOException(e); throw new IOException(e);
@ -198,7 +198,7 @@ public class ExpandComponent extends SearchComponent implements PluginInfoInitia
try { try {
for (String fq : fqs) { for (String fq : fqs) {
if (fq != null && fq.trim().length() != 0 && !fq.equals("*:*")) { if (fq != null && fq.trim().length() != 0 && !fq.equals("*:*")) {
QParser fqp = QParser.getParser(fq, null, req); QParser fqp = QParser.getParser(fq, req);
newFilters.add(fqp.getQuery()); newFilters.add(fqp.getQuery());
} }
} }

View File

@ -202,7 +202,7 @@ public class QueryComponent extends SearchComponent
filters = filters == null ? new ArrayList<Query>(fqs.length) : new ArrayList<>(filters); filters = filters == null ? new ArrayList<Query>(fqs.length) : new ArrayList<>(filters);
for (String fq : fqs) { for (String fq : fqs) {
if (fq != null && fq.trim().length()!=0) { if (fq != null && fq.trim().length()!=0) {
QParser fqp = QParser.getParser(fq, null, req); QParser fqp = QParser.getParser(fq, req);
filters.add(fqp.getQuery()); filters.add(fqp.getQuery());
} }
} }

View File

@ -158,7 +158,7 @@ public class RealTimeGetComponent extends SearchComponent
filters = filters == null ? new ArrayList<Query>(fqs.length) : new ArrayList<>(filters); filters = filters == null ? new ArrayList<Query>(fqs.length) : new ArrayList<>(filters);
for (String fq : fqs) { for (String fq : fqs) {
if (fq != null && fq.trim().length()!=0) { if (fq != null && fq.trim().length()!=0) {
QParser fqp = QParser.getParser(fq, null, req); QParser fqp = QParser.getParser(fq, req);
filters.add(fqp.getQuery()); filters.add(fqp.getQuery());
} }
} }

View File

@ -242,7 +242,7 @@ public class SpellCheckComponent extends SearchComponent implements SolrCoreAwar
try { try {
if (maxResultsFilterQueryString != null) { if (maxResultsFilterQueryString != null) {
// Get the default Lucene query parser // Get the default Lucene query parser
QParser parser = QParser.getParser(maxResultsFilterQueryString, QParserPlugin.DEFAULT_QTYPE, rb.req); QParser parser = QParser.getParser(maxResultsFilterQueryString, rb.req);
DocSet s = searcher.getDocSet(parser.getQuery()); DocSet s = searcher.getDocSet(parser.getQuery());
maxResultsByFilters = s.size(); maxResultsByFilters = s.size();
} else { } else {

View File

@ -321,7 +321,7 @@ public class SimpleFacets {
public void getFacetQueryCount(ParsedParams parsed, NamedList<Integer> res) throws SyntaxError, IOException { public void getFacetQueryCount(ParsedParams parsed, NamedList<Integer> res) throws SyntaxError, IOException {
// TODO: slight optimization would prevent double-parsing of any localParams // TODO: slight optimization would prevent double-parsing of any localParams
// TODO: SOLR-7753 // TODO: SOLR-7753
Query qobj = QParser.getParser(parsed.facetValue, null, req).getQuery(); Query qobj = QParser.getParser(parsed.facetValue, req).getQuery();
if (qobj == null) { if (qobj == null) {
res.add(parsed.key, 0); res.add(parsed.key, 0);

View File

@ -77,7 +77,7 @@ public class ChildDocTransformerFactory extends TransformerFactory {
BitSetProducer parentsFilter = null; BitSetProducer parentsFilter = null;
try { try {
Query parentFilterQuery = QParser.getParser( parentFilter, null, req).getQuery(); Query parentFilterQuery = QParser.getParser( parentFilter, req).getQuery();
parentsFilter = new QueryBitSetProducer(new QueryWrapperFilter(parentFilterQuery)); parentsFilter = new QueryBitSetProducer(new QueryWrapperFilter(parentFilterQuery));
} catch (SyntaxError syntaxError) { } catch (SyntaxError syntaxError) {
throw new SolrException( ErrorCode.BAD_REQUEST, "Failed to create correct parent filter query" ); throw new SolrException( ErrorCode.BAD_REQUEST, "Failed to create correct parent filter query" );
@ -86,7 +86,7 @@ public class ChildDocTransformerFactory extends TransformerFactory {
Query childFilterQuery = null; Query childFilterQuery = null;
if(childFilter != null) { if(childFilter != null) {
try { try {
childFilterQuery = QParser.getParser( childFilter, null, req).getQuery(); childFilterQuery = QParser.getParser( childFilter, req).getQuery();
} catch (SyntaxError syntaxError) { } catch (SyntaxError syntaxError) {
throw new SolrException( ErrorCode.BAD_REQUEST, "Failed to create correct child filter query" ); throw new SolrException( ErrorCode.BAD_REQUEST, "Failed to create correct child filter query" );
} }

View File

@ -223,7 +223,7 @@ public class Grouping {
} }
public void addQueryCommand(String groupByStr, SolrQueryRequest request) throws SyntaxError { public void addQueryCommand(String groupByStr, SolrQueryRequest request) throws SyntaxError {
QParser parser = QParser.getParser(groupByStr, null, request); QParser parser = QParser.getParser(groupByStr, request);
Query gq = parser.getQuery(); Query gq = parser.getQuery();
Grouping.CommandQuery gc = new CommandQuery(); Grouping.CommandQuery gc = new CommandQuery();
gc.query = gq; gc.query = gq;

View File

@ -98,7 +98,7 @@ public class JoinQParserPlugin extends QParserPlugin {
RefCounted<SolrIndexSearcher> fromHolder = null; RefCounted<SolrIndexSearcher> fromHolder = null;
LocalSolrQueryRequest otherReq = new LocalSolrQueryRequest(fromCore, params); LocalSolrQueryRequest otherReq = new LocalSolrQueryRequest(fromCore, params);
try { try {
QParser parser = QParser.getParser(v, "lucene", otherReq); QParser parser = QParser.getParser(v, otherReq);
fromQuery = parser.getQuery(); fromQuery = parser.getQuery();
fromHolder = fromCore.getRegisteredSearcher(); fromHolder = fromCore.getRegisteredSearcher();
if (fromHolder != null) fromCoreOpenTime = fromHolder.get().getOpenNanoTime(); if (fromHolder != null) fromCoreOpenTime = fromHolder.get().getOpenNanoTime();

View File

@ -263,6 +263,17 @@ public abstract class QParser {
debugInfo.add("QParser", this.getClass().getSimpleName()); debugInfo.add("QParser", this.getClass().getSimpleName());
} }
/** Create a <code>QParser</code> to parse <code>qstr</code>,
* using the "lucene" (QParserPlugin.DEFAULT_QTYPE) query parser.
* The query parser may be overridden by local parameters in the query
* string itself. For example if
* qstr=<code>{!prefix f=myfield}foo</code>
* then the prefix query parser will be used.
*/
public static QParser getParser(String qstr, SolrQueryRequest req) throws SyntaxError {
return getParser(qstr, QParserPlugin.DEFAULT_QTYPE, req);
}
/** Create a <code>QParser</code> to parse <code>qstr</code>, /** Create a <code>QParser</code> to parse <code>qstr</code>,
* assuming that the default query parser is <code>defaultParser</code>. * assuming that the default query parser is <code>defaultParser</code>.
* The query parser may be overridden by local parameters in the query * The query parser may be overridden by local parameters in the query

View File

@ -83,7 +83,7 @@ public class ReRankQParserPlugin extends QParserPlugin {
if (reRankQueryString == null || reRankQueryString.trim().length() == 0) { if (reRankQueryString == null || reRankQueryString.trim().length() == 0) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, RERANK_QUERY+" parameter is mandatory"); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, RERANK_QUERY+" parameter is mandatory");
} }
QParser reRankParser = QParser.getParser(reRankQueryString, null, req); QParser reRankParser = QParser.getParser(reRankQueryString, req);
Query reRankQuery = reRankParser.parse(); Query reRankQuery = reRankParser.parse();
int reRankDocs = localParams.getInt(RERANK_DOCS, RERANK_DOCS_DEFAULT); int reRankDocs = localParams.getInt(RERANK_DOCS, RERANK_DOCS_DEFAULT);

View File

@ -96,7 +96,7 @@ public class FacetProcessor<FacetRequestT extends FacetRequest> {
String parentStr = freq.domain.parents; String parentStr = freq.domain.parents;
Query parentQuery; Query parentQuery;
try { try {
QParser parser = QParser.getParser(parentStr, null, fcontext.req); QParser parser = QParser.getParser(parentStr, fcontext.req);
parentQuery = parser.getQuery(); parentQuery = parser.getQuery();
} catch (SyntaxError err) { } catch (SyntaxError err) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing block join parent specification: " + parentStr); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing block join parent specification: " + parentStr);

View File

@ -478,7 +478,7 @@ class FacetQueryParser extends FacetParser<FacetQuery> {
// TODO: substats that are from defaults!!! // TODO: substats that are from defaults!!!
if (qstring != null) { if (qstring != null) {
QParser parser = QParser.getParser(qstring, null, getSolrRequest()); QParser parser = QParser.getParser(qstring, getSolrRequest());
facet.q = parser.getQuery(); facet.q = parser.getQuery();
} }

View File

@ -62,7 +62,7 @@ public class QueryCommand implements Command<QueryCommandResult> {
* @return this * @return this
*/ */
public Builder setQuery(String groupQueryString, SolrQueryRequest request) throws SyntaxError { public Builder setQuery(String groupQueryString, SolrQueryRequest request) throws SyntaxError {
QParser parser = QParser.getParser(groupQueryString, null, request); QParser parser = QParser.getParser(groupQueryString, request);
this.queryString = groupQueryString; this.queryString = groupQueryString;
return setQuery(parser.getQuery()); return setQuery(parser.getQuery());
} }

View File

@ -236,7 +236,7 @@ public class ScoreJoinQParserPlugin extends QParserPlugin {
LocalSolrQueryRequest otherReq = new LocalSolrQueryRequest(fromCore, params); LocalSolrQueryRequest otherReq = new LocalSolrQueryRequest(fromCore, params);
try { try {
QParser fromQueryParser = QParser.getParser(fromQueryStr, "lucene", otherReq); QParser fromQueryParser = QParser.getParser(fromQueryStr, otherReq);
Query fromQuery = fromQueryParser.getQuery(); Query fromQuery = fromQueryParser.getQuery();
fromHolder = fromCore.getRegisteredSearcher(); fromHolder = fromCore.getRegisteredSearcher();

View File

@ -399,7 +399,7 @@ public class DirectUpdateHandler2 extends UpdateHandler implements SolrCoreState
Query q; Query q;
try { try {
// move this higher in the stack? // move this higher in the stack?
QParser parser = QParser.getParser(cmd.getQuery(), "lucene", cmd.req); QParser parser = QParser.getParser(cmd.getQuery(), cmd.req);
q = parser.getQuery(); q = parser.getQuery();
q = QueryUtils.makeQueryable(q); q = QueryUtils.makeQueryable(q);

View File

@ -486,7 +486,7 @@ public class SolrPluginUtils {
String qs = commands.size() >= 1 ? commands.get(0) : ""; String qs = commands.size() >= 1 ? commands.get(0) : "";
try { try {
Query query = QParser.getParser(qs, null, req).getQuery(); Query query = QParser.getParser(qs, req).getQuery();
// If the first non-query, non-filter command is a simple sort on an indexed field, then // If the first non-query, non-filter command is a simple sort on an indexed field, then
// we can use the Lucene sort ability. // we can use the Lucene sort ability.
@ -978,7 +978,7 @@ public class SolrPluginUtils {
List<Query> out = new ArrayList<>(queries.length); List<Query> out = new ArrayList<>(queries.length);
for (String q : queries) { for (String q : queries) {
if (null != q && 0 != q.trim().length()) { if (null != q && 0 != q.trim().length()) {
out.add(QParser.getParser(q, null, req).getQuery()); out.add(QParser.getParser(q, req).getQuery());
} }
} }
return out; return out;

View File

@ -131,7 +131,7 @@ public class TestReversedWildcardFilterFactory extends SolrTestCaseJ4 {
"//result[@numFound=1]"); "//result[@numFound=1]");
SolrQueryRequest req = req(); SolrQueryRequest req = req();
QParser qparser = QParser.getParser("id:1", "lucene", req); QParser qparser = QParser.getParser("id:1", req);
SolrQueryParser parserTwo = new SolrQueryParser(qparser, "two"); SolrQueryParser parserTwo = new SolrQueryParser(qparser, "two");
assertTrue(parserTwo.getAllowLeadingWildcard()); assertTrue(parserTwo.getAllowLeadingWildcard());

View File

@ -133,7 +133,7 @@ public class TestOverriddenPrefixQueryForCustomFieldType extends SolrTestCaseJ4
SolrQueryResponse rsp = new SolrQueryResponse(); SolrQueryResponse rsp = new SolrQueryResponse();
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp)); SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
for (int i = 0; i < inputs.length; i++) { for (int i = 0; i < inputs.length; i++) {
queries[i] = (QParser.getParser(inputs[i], null, req).getQuery()); queries[i] = (QParser.getParser(inputs[i], req).getQuery());
} }
} finally { } finally {
SolrRequestInfo.clearRequestInfo(); SolrRequestInfo.clearRequestInfo();

View File

@ -198,10 +198,10 @@ public class TestSearchPerf extends AbstractSolrTestCase {
String u=t((int)(indexSize*10*fractionCovered)); String u=t((int)(indexSize*10*fractionCovered));
SolrQueryRequest req = lrf.makeRequest(); SolrQueryRequest req = lrf.makeRequest();
QParser parser = QParser.getParser("foomany_s:[" + l + " TO " + u + "]", null, req); QParser parser = QParser.getParser("foomany_s:[" + l + " TO " + u + "]", req);
Query range = parser.getQuery(); Query range = parser.getQuery();
QParser parser2 = QParser.getParser("{!frange l="+l+" u="+u+"}foomany_s", null, req); QParser parser2 = QParser.getParser("{!frange l="+l+" u="+u+"}foomany_s", req);
Query frange = parser2.getQuery(); Query frange = parser2.getQuery();
req.close(); req.close();
@ -224,13 +224,13 @@ public class TestSearchPerf extends AbstractSolrTestCase {
SolrQueryRequest req = lrf.makeRequest(); SolrQueryRequest req = lrf.makeRequest();
QParser parser = QParser.getParser("foomany_s:[" + l + " TO " + u + "]", null, req); QParser parser = QParser.getParser("foomany_s:[" + l + " TO " + u + "]", req);
Query rangeQ = parser.getQuery(); Query rangeQ = parser.getQuery();
List<Query> filters = new ArrayList<>(); List<Query> filters = new ArrayList<>();
filters.add(rangeQ); filters.add(rangeQ);
req.close(); req.close();
parser = QParser.getParser("{!dismax qf=t10_100_ws pf=t10_100_ws ps=20}"+ t(0) + ' ' + t(1) + ' ' + t(2), null, req); parser = QParser.getParser("{!dismax qf=t10_100_ws pf=t10_100_ws ps=20}"+ t(0) + ' ' + t(1) + ' ' + t(2), req);
Query q= parser.getQuery(); Query q= parser.getQuery();
// SolrIndexSearcher searcher = req.getSearcher(); // SolrIndexSearcher searcher = req.getSearcher();

View File

@ -169,13 +169,13 @@ public class TestSolrQueryParser extends SolrTestCaseJ4 {
public void testCSQ() throws Exception { public void testCSQ() throws Exception {
SolrQueryRequest req = req(); SolrQueryRequest req = req();
QParser qParser = QParser.getParser("text:x^=3", "lucene", req); QParser qParser = QParser.getParser("text:x^=3", req);
Query q = qParser.getQuery(); Query q = qParser.getQuery();
assertTrue(q instanceof BoostQuery); assertTrue(q instanceof BoostQuery);
assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery); assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);
assertEquals(3.0, ((BoostQuery) q).getBoost(), 0.0f); assertEquals(3.0, ((BoostQuery) q).getBoost(), 0.0f);
qParser = QParser.getParser("(text:x text:y)^=-3", "lucene", req); qParser = QParser.getParser("(text:x text:y)^=-3", req);
q = qParser.getQuery(); q = qParser.getQuery();
assertTrue(q instanceof BoostQuery); assertTrue(q instanceof BoostQuery);
assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery); assertTrue(((BoostQuery) q).getQuery() instanceof ConstantScoreQuery);

View File

@ -79,4 +79,13 @@ public class TestStandardQParsers extends LuceneTestCase {
} }
/**
* Test that "lucene" is the default query parser.
*/
@Test
public void testDefaultQType() throws Exception {
assertEquals(LuceneQParserPlugin.NAME, QParserPlugin.DEFAULT_QTYPE);
assertEquals("lucene", LuceneQParserPlugin.NAME);
}
} }

View File

@ -168,7 +168,7 @@ public class TestScoreJoinQPNoScore extends SolrTestCaseJ4 {
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp)); SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
{ {
final Query query = QParser.getParser(req.getParams().get("q"), null, req).getQuery(); final Query query = QParser.getParser(req.getParams().get("q"), req).getQuery();
final Query rewrittenQuery = query.rewrite(req.getSearcher().getIndexReader()); final Query rewrittenQuery = query.rewrite(req.getSearcher().getIndexReader());
assertTrue( assertTrue(
rewrittenQuery+" should be Lucene's", rewrittenQuery+" should be Lucene's",
@ -178,7 +178,7 @@ public class TestScoreJoinQPNoScore extends SolrTestCaseJ4 {
{ {
final Query query = QParser.getParser( final Query query = QParser.getParser(
"{!join from=dept_id_s to=dept_ss}text_t:develop" "{!join from=dept_id_s to=dept_ss}text_t:develop"
, null, req).getQuery(); , req).getQuery();
final Query rewrittenQuery = query.rewrite(req.getSearcher().getIndexReader()); final Query rewrittenQuery = query.rewrite(req.getSearcher().getIndexReader());
assertEquals(rewrittenQuery+" is expected to be from Solr", assertEquals(rewrittenQuery+" is expected to be from Solr",
JoinQParserPlugin.class.getPackage().getName(), JoinQParserPlugin.class.getPackage().getName(),

View File

@ -187,7 +187,7 @@ public class TestScoreJoinQPScore extends SolrTestCaseJ4 {
final SolrQueryRequest req = req("q", "{!join from=movieId_s to=id score=" + score + " b=200}title:movie", "fl", "id,score", "omitHeader", "true"); final SolrQueryRequest req = req("q", "{!join from=movieId_s to=id score=" + score + " b=200}title:movie", "fl", "id,score", "omitHeader", "true");
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, new SolrQueryResponse())); SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, new SolrQueryResponse()));
final Query luceneQ = QParser.getParser(req.getParams().get("q"), null, req).getQuery().rewrite(req.getSearcher().getLeafReader()); final Query luceneQ = QParser.getParser(req.getParams().get("q"), req).getQuery().rewrite(req.getSearcher().getLeafReader());
assertTrue(luceneQ instanceof BoostQuery); assertTrue(luceneQ instanceof BoostQuery);
float boost = ((BoostQuery) luceneQ).getBoost(); float boost = ((BoostQuery) luceneQ).getBoost();
assertEquals("" + luceneQ, Float.floatToIntBits(200), Float.floatToIntBits(boost)); assertEquals("" + luceneQ, Float.floatToIntBits(200), Float.floatToIntBits(boost));