Skip to content
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

Re-enable support for array-valued geo_shape fields. #58786

Merged
merged 5 commits into from Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -283,11 +283,6 @@ protected void parseCreateField(ParseContext context) throws IOException {
protected abstract void addDocValuesFields(String name, Processed geometry, List<IndexableField> fields, ParseContext context);
protected abstract void addMultiFields(ParseContext context, Processed geometry) throws IOException;

@Override
public final boolean parsesArrayValue() {
return true;
}

/** parsing logic for geometry indexing */
@Override
public void parse(ParseContext context) throws IOException {
Expand Down
Expand Up @@ -130,6 +130,11 @@ protected AbstractPointGeometryFieldMapper(String simpleName, FieldType fieldTyp
this.nullValue = nullValue;
}

@Override
public final boolean parsesArrayValue() {
return true;
}

@Override
protected void mergeOptions(FieldMapper other, List<String> conflicts) {
AbstractPointGeometryFieldMapper gpfm = (AbstractPointGeometryFieldMapper)other;
Expand Down
Expand Up @@ -197,6 +197,11 @@ protected AbstractShapeGeometryFieldMapper(String simpleName, FieldType fieldTyp
this.orientation = orientation;
}

@Override
public final boolean parsesArrayValue() {
return false;
}

@Override
protected final void mergeOptions(FieldMapper other, List<String> conflicts) {
AbstractShapeGeometryFieldMapper gsfm = (AbstractShapeGeometryFieldMapper)other;
Expand Down
Expand Up @@ -18,13 +18,16 @@
*/
package org.elasticsearch.index.mapper;

import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.test.InternalSettingsPlugin;
import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin;
Expand All @@ -38,6 +41,7 @@
import static org.elasticsearch.index.mapper.AbstractGeometryFieldMapper.Names.IGNORE_Z_VALUE;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;

public class GeoShapeFieldMapperTests extends FieldMapperTestCase<GeoShapeFieldMapper.Builder> {
Expand Down Expand Up @@ -302,6 +306,41 @@ public void testSerializeDefaults() throws Exception {
}
}

public void testGeoShapeArrayParsing() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("location")
.field("type", "geo_shape")
.endObject()
.endObject()
.endObject());

DocumentMapper mapper = createIndex("test").mapperService().documentMapperParser()
.parse("_doc", new CompressedXContent(mapping));

BytesReference arrayedDoc = BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject()
.startArray("shape")
.startObject()
.field("type", "Point")
.startArray("coordinates").value(176.0).value(15.0).endArray()
.endObject()
.startObject()
.field("type", "Point")
.startArray("coordinates").value(76.0).value(-15.0).endArray()
.endObject()
.endArray()
.endObject()
);

SourceToParse sourceToParse = new SourceToParse("test", "1", arrayedDoc, XContentType.JSON);
ParsedDocument document = mapper.parse(sourceToParse);
assertThat(document.docs(), hasSize(1));
IndexableField[] fields = document.docs().get(0).getFields("shape.type");
assertThat(fields.length, equalTo(2));
}

public String toXContentString(GeoShapeFieldMapper mapper, boolean includeDefaults) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
ToXContent.Params params;
Expand Down
Expand Up @@ -5,24 +5,30 @@
*/
package org.elasticsearch.xpack.spatial.index.mapper;

import org.apache.lucene.index.IndexableField;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.compress.CompressedXContent;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.mapper.AbstractShapeGeometryFieldMapper;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.Mapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.index.mapper.SourceToParse;

import java.io.IOException;
import java.util.Collections;

import static org.elasticsearch.index.mapper.AbstractPointGeometryFieldMapper.Names.IGNORE_Z_VALUE;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;

/** testing for {@link org.elasticsearch.xpack.spatial.index.mapper.ShapeFieldMapper} */
Expand Down Expand Up @@ -268,6 +274,41 @@ public void testSerializeDefaults() throws Exception {
}
}

public void testShapeArrayParsing() throws Exception {
String mapping = Strings.toString(XContentFactory.jsonBuilder()
.startObject()
.startObject("properties")
.startObject("location")
.field("type", "shape")
.endObject()
.endObject()
.endObject());

DocumentMapper mapper = createIndex("test").mapperService().documentMapperParser()
.parse("_doc", new CompressedXContent(mapping));

BytesReference arrayedDoc = BytesReference.bytes(XContentFactory.jsonBuilder()
.startObject()
.startArray("shape")
.startObject()
.field("type", "Point")
.startArray("coordinates").value(176.0).value(15.0).endArray()
.endObject()
.startObject()
.field("type", "Point")
.startArray("coordinates").value(76.0).value(-15.0).endArray()
.endObject()
.endArray()
.endObject()
);

SourceToParse sourceToParse = new SourceToParse("test", "1", arrayedDoc, XContentType.JSON);
ParsedDocument document = mapper.parse(sourceToParse);
assertThat(document.docs(), hasSize(1));
IndexableField[] fields = document.docs().get(0).getFields("shape.type");
assertThat(fields.length, equalTo(2));
}

public String toXContentString(ShapeFieldMapper mapper, boolean includeDefaults) throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
ToXContent.Params params;
Expand Down