Search in sources :

Example 1 with AbstractSpatialFieldType

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

the class GeoFieldUpdater method create.

@Override
public DocTransformer create(String display, SolrParams params, SolrQueryRequest req) {
    String fname = params.get("f", display);
    if (fname.startsWith("[") && fname.endsWith("]")) {
        fname = display.substring(1, display.length() - 1);
    }
    SchemaField sf = req.getSchema().getFieldOrNull(fname);
    if (sf == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, this.getClass().getSimpleName() + " using unknown field: " + fname);
    }
    if (!(sf.getType() instanceof AbstractSpatialFieldType)) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoTransformer requested non-spatial field: " + fname + " (" + sf.getType().getClass().getSimpleName() + ")");
    }
    final GeoFieldUpdater updater = new GeoFieldUpdater();
    updater.field = fname;
    updater.display = display;
    updater.display_error = display + "_error";
    ValueSource shapes = null;
    AbstractSpatialFieldType<?> sdv = (AbstractSpatialFieldType<?>) sf.getType();
    SpatialStrategy strategy = sdv.getStrategy(fname);
    if (strategy instanceof CompositeSpatialStrategy) {
        shapes = ((CompositeSpatialStrategy) strategy).getGeometryStrategy().makeShapeValueSource();
    } else if (strategy instanceof SerializedDVStrategy) {
        shapes = ((SerializedDVStrategy) strategy).makeShapeValueSource();
    }
    String writerName = params.get("w", "GeoJSON");
    updater.formats = strategy.getSpatialContext().getFormats();
    updater.writer = updater.formats.getWriter(writerName);
    if (updater.writer == null) {
        StringBuilder str = new StringBuilder();
        str.append("Unknown Spatial Writer: ").append(writerName);
        str.append(" [");
        for (ShapeWriter w : updater.formats.getWriters()) {
            str.append(w.getFormatName()).append(' ');
        }
        str.append("]");
        throw new SolrException(ErrorCode.BAD_REQUEST, str.toString());
    }
    QueryResponseWriter qw = req.getCore().getQueryResponseWriter(req);
    updater.isJSON = (qw.getClass() == JSONResponseWriter.class) && (updater.writer instanceof GeoJSONWriter);
    // Using ValueSource
    if (shapes != null) {
        // we don't really need the qparser... just so we can reuse valueSource
        QParser parser = new QParser(null, null, params, req) {

            @Override
            public Query parse() throws SyntaxError {
                return new MatchAllDocsQuery();
            }
        };
        return new ValueSourceAugmenter(display, parser, shapes) {

            @Override
            protected void setValue(SolrDocument doc, Object val) {
                updater.setValue(doc, val);
            }
        };
    }
    // Using the raw stored values
    return new DocTransformer() {

        @Override
        public void transform(SolrDocument doc, int docid, float score) throws IOException {
            Object val = doc.remove(updater.field);
            if (val != null) {
                updater.setValue(doc, val);
            }
        }

        @Override
        public String getName() {
            return updater.display;
        }

        @Override
        public String[] getExtraRequestFields() {
            return new String[] { updater.field };
        }
    };
}
Also used : CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) ShapeWriter(org.locationtech.spatial4j.io.ShapeWriter) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) SerializedDVStrategy(org.apache.lucene.spatial.serialized.SerializedDVStrategy) SchemaField(org.apache.solr.schema.SchemaField) SolrDocument(org.apache.solr.common.SolrDocument) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) GeoJSONWriter(org.locationtech.spatial4j.io.GeoJSONWriter) SolrException(org.apache.solr.common.SolrException)

Example 2 with AbstractSpatialFieldType

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

the class GeoJSONWriter method write.

@Override
public void write(Writer writer, SolrQueryRequest req, SolrQueryResponse rsp) throws IOException {
    String geofield = req.getParams().get(FIELD, null);
    if (geofield == null || geofield.length() == 0) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON.  Missing parameter: '" + FIELD + "'");
    }
    SchemaField sf = req.getSchema().getFieldOrNull(geofield);
    if (sf == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoJSON.  Unknown field: '" + FIELD + "'=" + geofield);
    }
    SupportedFormats formats = null;
    if (sf.getType() instanceof AbstractSpatialFieldType) {
        SpatialContext ctx = ((AbstractSpatialFieldType) sf.getType()).getSpatialContext();
        formats = ctx.getFormats();
    }
    JSONWriter w = new GeoJSONWriter(writer, req, rsp, geofield, formats);
    try {
        w.writeResponse();
    } finally {
        w.close();
    }
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SpatialContext(org.locationtech.spatial4j.context.SpatialContext) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) SolrException(org.apache.solr.common.SolrException) SupportedFormats(org.locationtech.spatial4j.io.SupportedFormats)

Example 3 with AbstractSpatialFieldType

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

the class GeoDistValueSourceParser method parseSfield.

private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError {
    String sfield = fp.getParam(SpatialParams.FIELD);
    if (sfield == null)
        return null;
    SchemaField sf = fp.getReq().getSchema().getField(sfield);
    FieldType type = sf.getType();
    if (type instanceof AbstractSpatialFieldType) {
        AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
        return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield), asft.getDistanceUnits());
    }
    ValueSource vs = type.getValueSource(sf, fp);
    if (vs instanceof MultiValueSource) {
        return (MultiValueSource) vs;
    }
    throw new SyntaxError("Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SyntaxError(org.apache.solr.search.SyntaxError) ValueSource(org.apache.lucene.queries.function.ValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) FieldType(org.apache.solr.schema.FieldType) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType)

Aggregations

AbstractSpatialFieldType (org.apache.solr.schema.AbstractSpatialFieldType)3 SchemaField (org.apache.solr.schema.SchemaField)3 ValueSource (org.apache.lucene.queries.function.ValueSource)2 SolrException (org.apache.solr.common.SolrException)2 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)1 MultiValueSource (org.apache.lucene.queries.function.valuesource.MultiValueSource)1 VectorValueSource (org.apache.lucene.queries.function.valuesource.VectorValueSource)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)1 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)1 SerializedDVStrategy (org.apache.lucene.spatial.serialized.SerializedDVStrategy)1 SolrDocument (org.apache.solr.common.SolrDocument)1 JSONResponseWriter (org.apache.solr.response.JSONResponseWriter)1 QueryResponseWriter (org.apache.solr.response.QueryResponseWriter)1 FieldType (org.apache.solr.schema.FieldType)1 QParser (org.apache.solr.search.QParser)1 SyntaxError (org.apache.solr.search.SyntaxError)1 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)1 GeoJSONWriter (org.locationtech.spatial4j.io.GeoJSONWriter)1 ShapeWriter (org.locationtech.spatial4j.io.ShapeWriter)1