-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support spatial fields in field retrieval API. (#59821)
Although we accept a variety of formats during indexing, spatial data is returned in a single consistent format. This is GeoJSON by default, but well-known text is also supported by passing the option 'format: wkt'. Note that points (in addition to shapes) are returned in GeoJSON by default. The reasoning is that this gives better consistency, and is the most convenient format for most REST API users.
- Loading branch information
1 parent
e033880
commit 568d248
Showing
24 changed files
with
661 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -609,5 +609,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws | |
return builder; | ||
} | ||
} | ||
|
||
} |
82 changes: 82 additions & 0 deletions
82
server/src/main/java/org/elasticsearch/common/geo/GeoJsonGeometryFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* | ||
* 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; | ||
|
||
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 Object toXContentAsObject(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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
server/src/main/java/org/elasticsearch/common/geo/WKTGeometryFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 toXContentAsObject(Geometry geometry) { | ||
return wellKnownTextParser.toWKT(geometry); | ||
} | ||
} |
Oops, something went wrong.