Search in sources :

Example 26 with FieldType

use of org.apache.solr.schema.FieldType 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)

Example 27 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class SpatialFilterQParser method parse.

@Override
public Query parse() throws SyntaxError {
    //if more than one, we need to treat them as a point...
    //TODO: Should we accept multiple fields
    String[] fields = localParams.getParams("f");
    if (fields == null || fields.length == 0) {
        String field = getParam(SpatialParams.FIELD);
        if (field == null)
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, " missing sfield for spatial request");
        fields = new String[] { field };
    }
    String pointStr = getParam(SpatialParams.POINT);
    if (pointStr == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, SpatialParams.POINT + " missing.");
    }
    double dist = -1;
    String distS = getParam(SpatialParams.DISTANCE);
    if (distS != null)
        dist = Double.parseDouble(distS);
    if (dist < 0) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, SpatialParams.DISTANCE + " must be >= 0");
    }
    String measStr = localParams.get(SpatialParams.MEASURE);
    //TODO: Need to do something with Measures
    Query result = null;
    //fields is valid at this point
    if (fields.length == 1) {
        SchemaField sf = req.getSchema().getField(fields[0]);
        FieldType type = sf.getType();
        if (type instanceof SpatialQueryable) {
            SpatialQueryable queryable = ((SpatialQueryable) type);
            double radius = localParams.getDouble(SpatialParams.SPHERE_RADIUS, queryable.getSphereRadius());
            SpatialOptions opts = new SpatialOptions(pointStr, dist, sf, measStr, radius);
            opts.bbox = bbox;
            result = queryable.createSpatialQuery(this, opts);
        } else {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "The field " + fields[0] + " does not support spatial filtering");
        }
    } else {
    // fields.length > 1
    //TODO: Not sure about this just yet, is there a way to delegate, or do we just have a helper class?
    //Seems like we could just use FunctionQuery, but then what about scoring
    /*List<ValueSource> sources = new ArrayList<ValueSource>(fields.length);
      for (String field : fields) {
        SchemaField sf = schema.getField(field);
        sources.add(sf.getType().getValueSource(sf, this));
      }
      MultiValueSource vs = new VectorValueSource(sources);
      ValueSourceRangeFilter rf = new ValueSourceRangeFilter(vs, "0", String.valueOf(dist), true, true);
      result = new SolrConstantScoreQuery(rf);*/
    }
    return result;
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) Query(org.apache.lucene.search.Query) SpatialQueryable(org.apache.solr.schema.SpatialQueryable) SolrException(org.apache.solr.common.SolrException) FieldType(org.apache.solr.schema.FieldType)

Example 28 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class TermQParserPlugin 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() {
            String fname = localParams.get(QueryParsing.F);
            FieldType ft = req.getSchema().getFieldTypeNoEx(fname);
            String val = localParams.get(QueryParsing.V);
            BytesRefBuilder term;
            if (ft != null) {
                if (ft.isPointField()) {
                    return ft.getFieldQuery(this, req.getSchema().getField(fname), val);
                } else {
                    term = new BytesRefBuilder();
                    ft.readableToIndexed(val, term);
                }
            } else {
                term = new BytesRefBuilder();
                term.copyChars(val);
            }
            return new TermQuery(new Term(fname, term.get()));
        }
    };
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Term(org.apache.lucene.index.Term) FieldType(org.apache.solr.schema.FieldType)

Example 29 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class QueryParsing method writeFieldName.

///////////////////////////
///////////////////////////
///////////////////////////
static FieldType writeFieldName(String name, IndexSchema schema, Appendable out, int flags) throws IOException {
    FieldType ft = null;
    ft = schema.getFieldTypeNoEx(name);
    out.append(name);
    if (ft == null) {
        out.append("(UNKNOWN FIELD " + name + ')');
    }
    out.append(':');
    return ft;
}
Also used : FieldType(org.apache.solr.schema.FieldType)

Example 30 with FieldType

use of org.apache.solr.schema.FieldType in project lucene-solr by apache.

the class XLSXWriter method writeResponse.

public void writeResponse(OutputStream out, LinkedHashMap<String, String> colNamesMap, LinkedHashMap<String, Integer> colWidthsMap) throws IOException {
    SolrParams params = req.getParams();
    Collection<String> fields = returnFields.getRequestedFieldNames();
    Object responseObj = rsp.getValues().get("response");
    boolean returnOnlyStored = false;
    if (fields == null || returnFields.hasPatternMatching()) {
        if (responseObj instanceof SolrDocumentList) {
            // get the list of fields from the SolrDocumentList
            if (fields == null) {
                fields = new LinkedHashSet<String>();
            }
            for (SolrDocument sdoc : (SolrDocumentList) responseObj) {
                fields.addAll(sdoc.getFieldNames());
            }
        } else {
            // get the list of fields from the index
            Iterable<String> all = req.getSearcher().getFieldNames();
            if (fields == null) {
                fields = Sets.newHashSet(all);
            } else {
                Iterables.addAll(fields, all);
            }
        }
        if (returnFields.wantsScore()) {
            fields.add("score");
        } else {
            fields.remove("score");
        }
        returnOnlyStored = true;
    }
    for (String field : fields) {
        if (!returnFields.wantsField(field)) {
            continue;
        }
        if (field.equals("score")) {
            XLField xlField = new XLField();
            xlField.name = "score";
            xlFields.put("score", xlField);
            continue;
        }
        SchemaField sf = schema.getFieldOrNull(field);
        if (sf == null) {
            FieldType ft = new StrField();
            sf = new SchemaField(field, ft);
        }
        // Return only stored fields, unless an explicit field list is specified
        if (returnOnlyStored && sf != null && !sf.stored()) {
            continue;
        }
        XLField xlField = new XLField();
        xlField.name = field;
        xlField.sf = sf;
        xlFields.put(field, xlField);
    }
    wb.addRow();
    //write header
    for (XLField xlField : xlFields.values()) {
        String printName = xlField.name;
        int colWidth = 14;
        String niceName = colNamesMap.get(xlField.name);
        if (niceName != null) {
            printName = niceName;
        }
        Integer niceWidth = colWidthsMap.get(xlField.name);
        if (niceWidth != null) {
            colWidth = niceWidth.intValue();
        }
        writeStr(xlField.name, printName, false);
        wb.setColWidth(colWidth);
        wb.setHeaderCell();
    }
    wb.setHeaderRow();
    wb.addRow();
    if (responseObj instanceof ResultContext) {
        writeDocuments(null, (ResultContext) responseObj);
    } else if (responseObj instanceof DocList) {
        ResultContext ctx = new BasicResultContext((DocList) responseObj, returnFields, null, null, req);
        writeDocuments(null, ctx);
    } else if (responseObj instanceof SolrDocumentList) {
        writeSolrDocumentList(null, (SolrDocumentList) responseObj, returnFields);
    }
    wb.flush(out);
    wb = null;
}
Also used : BasicResultContext(org.apache.solr.response.BasicResultContext) ResultContext(org.apache.solr.response.ResultContext) StrField(org.apache.solr.schema.StrField) SolrDocumentList(org.apache.solr.common.SolrDocumentList) FieldType(org.apache.solr.schema.FieldType) SchemaField(org.apache.solr.schema.SchemaField) BasicResultContext(org.apache.solr.response.BasicResultContext) SolrDocument(org.apache.solr.common.SolrDocument) SolrParams(org.apache.solr.common.params.SolrParams) DocList(org.apache.solr.search.DocList)

Aggregations

FieldType (org.apache.solr.schema.FieldType)93 SchemaField (org.apache.solr.schema.SchemaField)37 SolrException (org.apache.solr.common.SolrException)29 ArrayList (java.util.ArrayList)23 BytesRef (org.apache.lucene.util.BytesRef)23 NamedList (org.apache.solr.common.util.NamedList)23 IOException (java.io.IOException)18 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)15 IndexSchema (org.apache.solr.schema.IndexSchema)14 Query (org.apache.lucene.search.Query)13 BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)13 Analyzer (org.apache.lucene.analysis.Analyzer)12 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)10 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)10 StrField (org.apache.solr.schema.StrField)8 HashMap (java.util.HashMap)7 List (java.util.List)7 Map (java.util.Map)7 DocIterator (org.apache.solr.search.DocIterator)7 DocList (org.apache.solr.search.DocList)7