use of org.apache.lucene.analysis.payloads.PayloadEncoder in project lucene-solr by apache.
the class PayloadCheckQParserPlugin method createParser.
@Override
public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
return new QParser(qstr, localParams, params, req) {
@Override
public Query parse() throws SyntaxError {
String field = localParams.get(QueryParsing.F);
String value = localParams.get(QueryParsing.V);
String p = localParams.get("payloads");
if (field == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'f' not specified");
}
if (value == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "query string missing");
}
if (p == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "'payloads' not specified");
}
FieldType ft = req.getCore().getLatestSchema().getFieldType(field);
Analyzer analyzer = ft.getQueryAnalyzer();
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");
}
PayloadEncoder encoder = null;
String e = PayloadUtils.getPayloadEncoder(ft);
if ("float".equals(e)) {
// TODO: centralize this string->PayloadEncoder logic (see DelimitedPayloadTokenFilterFactory)
encoder = new FloatEncoder();
} else if ("integer".equals(e)) {
encoder = new IntegerEncoder();
} else if ("identity".equals(e)) {
encoder = new IdentityEncoder();
}
if (encoder == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "invalid encoder: " + e + " for field: " + field);
}
List<BytesRef> payloads = new ArrayList<>();
// since payloads (most likely) came in whitespace delimited, just split
String[] rawPayloads = p.split(" ");
for (String rawPayload : rawPayloads) {
if (rawPayload.length() > 0)
payloads.add(encoder.encode(rawPayload.toCharArray()));
}
return new SpanPayloadCheckQuery(query, payloads);
}
};
}
Aggregations