Search in sources :

Example 1 with PayloadEncoder

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);
        }
    };
}
Also used : PayloadEncoder(org.apache.lucene.analysis.payloads.PayloadEncoder) SpanPayloadCheckQuery(org.apache.lucene.queries.payloads.SpanPayloadCheckQuery) FloatEncoder(org.apache.lucene.analysis.payloads.FloatEncoder) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IdentityEncoder(org.apache.lucene.analysis.payloads.IdentityEncoder) Analyzer(org.apache.lucene.analysis.Analyzer) FieldType(org.apache.solr.schema.FieldType) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SolrException(org.apache.solr.common.SolrException) BytesRef(org.apache.lucene.util.BytesRef) IntegerEncoder(org.apache.lucene.analysis.payloads.IntegerEncoder)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 FloatEncoder (org.apache.lucene.analysis.payloads.FloatEncoder)1 IdentityEncoder (org.apache.lucene.analysis.payloads.IdentityEncoder)1 IntegerEncoder (org.apache.lucene.analysis.payloads.IntegerEncoder)1 PayloadEncoder (org.apache.lucene.analysis.payloads.PayloadEncoder)1 SpanPayloadCheckQuery (org.apache.lucene.queries.payloads.SpanPayloadCheckQuery)1 SpanQuery (org.apache.lucene.search.spans.SpanQuery)1 BytesRef (org.apache.lucene.util.BytesRef)1 SolrException (org.apache.solr.common.SolrException)1 FieldType (org.apache.solr.schema.FieldType)1