-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Support spatial fields in field retrieval API. #59821
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 7 commits
156d741
d50fa6f
804a295
6f9e82a
926993d
4bf9bd1
d3d6282
1b7dad3
9c155cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -609,5 +609,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
| return builder; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.geo; | ||
|
|
||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.StreamInput; | ||
| import org.elasticsearch.common.xcontent.LoggingDeprecationHandler; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentFactory; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
| import org.elasticsearch.geometry.Geometry; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.UncheckedIOException; | ||
| import java.util.Map; | ||
|
|
||
| public class GeoJsonGeometryFormat implements GeometryFormat<Geometry> { | ||
| public static final String NAME = "geojson"; | ||
|
|
||
| private final GeoJson geoJsonParser; | ||
|
|
||
| public GeoJsonGeometryFormat(GeoJson geoJsonParser) { | ||
| this.geoJsonParser = geoJsonParser; | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public Geometry fromXContent(XContentParser parser) throws IOException { | ||
| if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { | ||
| return null; | ||
| } | ||
| return geoJsonParser.fromXContent(parser); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(Geometry geometry, XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
| if (geometry != null) { | ||
| return GeoJson.toXContent(geometry, builder, params); | ||
| } else { | ||
| return builder.nullValue(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public Map<?, ?> toObject(Geometry geometry) { | ||
| try { | ||
| XContentBuilder builder = XContentFactory.jsonBuilder(); | ||
| GeoJson.toXContent(geometry, builder, ToXContent.EMPTY_PARAMS); | ||
| StreamInput input = BytesReference.bytes(builder).streamInput(); | ||
|
|
||
| try (XContentParser parser = XContentType.JSON.xContent() | ||
| .createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, input)) { | ||
| return parser.map(); | ||
| } | ||
| } catch (IOException e) { | ||
| throw new UncheckedIOException(e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,11 @@ | |
| */ | ||
| public interface GeometryFormat<ParsedFormat> { | ||
|
|
||
| /** | ||
| * The name of the format, for example 'wkt'. | ||
| */ | ||
| String name(); | ||
|
|
||
| /** | ||
| * Parser JSON representation of a geometry | ||
| */ | ||
|
|
@@ -41,4 +46,10 @@ public interface GeometryFormat<ParsedFormat> { | |
| */ | ||
| XContentBuilder toXContent(ParsedFormat geometry, XContentBuilder builder, ToXContent.Params params) throws IOException; | ||
|
|
||
| /** | ||
| * Serializes the geometry into a standard Java object. | ||
| * | ||
| * For example, the GeoJson format returns the geometry as a map, while WKT returns a string. | ||
| */ | ||
| Object toObject(ParsedFormat geometry); | ||
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.common.geo; | ||
|
|
||
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.geometry.Geometry; | ||
| import org.elasticsearch.geometry.utils.WellKnownText; | ||
|
|
||
| import java.io.IOException; | ||
| import java.text.ParseException; | ||
|
|
||
| public class WKTGeometryFormat implements GeometryFormat<Geometry> { | ||
| public static final String NAME = "wkt"; | ||
|
|
||
| private final WellKnownText wellKnownTextParser; | ||
|
|
||
| public WKTGeometryFormat(WellKnownText wellKnownTextParser) { | ||
| this.wellKnownTextParser = wellKnownTextParser; | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public Geometry fromXContent(XContentParser parser) throws IOException, ParseException { | ||
| if (parser.currentToken() == XContentParser.Token.VALUE_NULL) { | ||
| return null; | ||
| } | ||
| return wellKnownTextParser.fromWKT(parser.text()); | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(Geometry geometry, XContentBuilder builder, ToXContent.Params params) throws IOException { | ||
| if (geometry != null) { | ||
| return builder.value(wellKnownTextParser.toWKT(geometry)); | ||
| } else { | ||
| return builder.nullValue(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String toObject(Geometry geometry) { | ||
| return wellKnownTextParser.toWKT(geometry); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe return Object here as well for consistency. I don't think the fact that it returns Map is significant here.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I sometimes like using covariance like this to clarify that a subclass always returns a specific type. I don't feel strongly about it though, happy to change it. We can always change it back later if we find it helpful for unit testing, etc. to have the exact type.