mirror of https://github.com/apache/lucene.git
SOLR-1485: improve TokenStream API usage
This commit is contained in:
parent
0be8e17832
commit
5d42177b92
|
@ -17,6 +17,7 @@
|
|||
|
||||
package org.apache.solr.search;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -67,7 +68,12 @@ public class PayloadCheckQParserPlugin extends QParserPlugin {
|
|||
|
||||
FieldType ft = req.getCore().getLatestSchema().getFieldType(field);
|
||||
Analyzer analyzer = ft.getQueryAnalyzer();
|
||||
SpanQuery query = PayloadUtils.createSpanQuery(field, value, analyzer);
|
||||
SpanQuery query = null;
|
||||
try {
|
||||
query = PayloadUtils.createSpanQuery(field, value, analyzer);
|
||||
} catch (IOException e) {
|
||||
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
|
||||
}
|
||||
|
||||
if (query == null) {
|
||||
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SpanQuery is null");
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
|
||||
package org.apache.solr.search;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.apache.lucene.analysis.Analyzer;
|
||||
import org.apache.lucene.queries.payloads.PayloadFunction;
|
||||
import org.apache.lucene.queries.payloads.PayloadScoreQuery;
|
||||
|
@ -61,7 +63,12 @@ public class PayloadScoreQParserPlugin extends QParserPlugin {
|
|||
|
||||
FieldType ft = req.getCore().getLatestSchema().getFieldType(field);
|
||||
Analyzer analyzer = ft.getQueryAnalyzer();
|
||||
SpanQuery query = PayloadUtils.createSpanQuery(field, value, analyzer);
|
||||
SpanQuery query = null;
|
||||
try {
|
||||
query = PayloadUtils.createSpanQuery(field, value, analyzer);
|
||||
} catch (IOException e) {
|
||||
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,e);
|
||||
}
|
||||
|
||||
if (query == null) {
|
||||
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SpanQuery is null");
|
||||
|
|
|
@ -38,7 +38,6 @@ import org.apache.lucene.search.spans.SpanQuery;
|
|||
import org.apache.lucene.search.spans.SpanTermQuery;
|
||||
import org.apache.lucene.util.BytesRef;
|
||||
import org.apache.solr.analysis.TokenizerChain;
|
||||
import org.apache.solr.common.SolrException;
|
||||
import org.apache.solr.schema.FieldType;
|
||||
|
||||
public class PayloadUtils {
|
||||
|
@ -100,22 +99,24 @@ public class PayloadUtils {
|
|||
return payloadFunction;
|
||||
}
|
||||
|
||||
public static SpanQuery createSpanQuery(String field, String value, Analyzer analyzer) {
|
||||
SpanQuery query;
|
||||
try {
|
||||
/**
|
||||
* The generated SpanQuery will be either a SpanTermQuery or an ordered, zero slop SpanNearQuery, depending
|
||||
* on how many tokens are emitted.
|
||||
*/
|
||||
public static SpanQuery createSpanQuery(String field, String value, Analyzer analyzer) throws IOException {
|
||||
// adapted this from QueryBuilder.createSpanQuery (which isn't currently public) and added reset(), end(), and close() calls
|
||||
TokenStream in = analyzer.tokenStream(field, value);
|
||||
List<SpanTermQuery> terms = new ArrayList<>();
|
||||
try (TokenStream in = analyzer.tokenStream(field, value)) {
|
||||
in.reset();
|
||||
|
||||
TermToBytesRefAttribute termAtt = in.getAttribute(TermToBytesRefAttribute.class);
|
||||
|
||||
List<SpanTermQuery> terms = new ArrayList<>();
|
||||
while (in.incrementToken()) {
|
||||
terms.add(new SpanTermQuery(new Term(field, termAtt.getBytesRef())));
|
||||
}
|
||||
in.end();
|
||||
in.close();
|
||||
}
|
||||
|
||||
SpanQuery query;
|
||||
if (terms.isEmpty()) {
|
||||
query = null;
|
||||
} else if (terms.size() == 1) {
|
||||
|
@ -123,11 +124,6 @@ public class PayloadUtils {
|
|||
} else {
|
||||
query = new SpanNearQuery(terms.toArray(new SpanTermQuery[terms.size()]), 0, true);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, e);
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue