Skip to content

Adding support for wildcard fields #1823

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

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public enum FieldType {
TOKEN_COUNT,
// ES 5.x
TEXT, KEYWORD, HALF_FLOAT, SCALED_FLOAT,
WILDCARD,

GEO_POINT,
GEO_SHAPE,
Expand Down Expand Up @@ -81,6 +82,7 @@ public enum FieldType {
CAST_HIERARCHY.put(TOKEN_COUNT, new LinkedHashSet<FieldType>(Arrays.asList(LONG, KEYWORD)));
CAST_HIERARCHY.put(TEXT, new LinkedHashSet<FieldType>(Collections.singletonList(KEYWORD)));
CAST_HIERARCHY.put(KEYWORD, new LinkedHashSet<FieldType>());
CAST_HIERARCHY.put(WILDCARD, new LinkedHashSet<FieldType>(Collections.singletonList(KEYWORD)));
CAST_HIERARCHY.put(HALF_FLOAT, new LinkedHashSet<FieldType>(Arrays.asList(FLOAT, DOUBLE, KEYWORD)));
CAST_HIERARCHY.put(SCALED_FLOAT, new LinkedHashSet<FieldType>(Arrays.asList(DOUBLE, KEYWORD)));
CAST_HIERARCHY.put(GEO_POINT, new LinkedHashSet<FieldType>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import static org.elasticsearch.hadoop.serialization.FieldType.SHORT;
import static org.elasticsearch.hadoop.serialization.FieldType.STRING;
import static org.elasticsearch.hadoop.serialization.FieldType.TEXT;
import static org.elasticsearch.hadoop.serialization.FieldType.WILDCARD;

import static org.elasticsearch.hadoop.serialization.dto.mapping.MappingUtils.findTypos;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -167,6 +168,8 @@ public void testPrimitivesParsing() throws Exception {
assertEquals(SCALED_FLOAT, props[13].type());
assertEquals("field15", props[14].name());
assertEquals(DATE_NANOS, props[14].type());
assertEquals("field16", props[15].name());
assertEquals(WILDCARD, props[15].type());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
},
"field15" : {
"type" : "date_nanos"
},
"field16" : {
"type" : "wildcard"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import org.elasticsearch.hadoop.serialization.FieldType.SHORT
import org.elasticsearch.hadoop.serialization.FieldType.STRING
import org.elasticsearch.hadoop.serialization.FieldType.TEXT
import org.elasticsearch.hadoop.serialization.FieldType.TOKEN_COUNT
import org.elasticsearch.hadoop.serialization.FieldType.WILDCARD
import org.elasticsearch.hadoop.serialization.Parser
import org.elasticsearch.hadoop.serialization.Parser.Token.VALUE_BOOLEAN
import org.elasticsearch.hadoop.serialization.Parser.Token.VALUE_NULL
Expand Down Expand Up @@ -82,6 +83,7 @@ class ScalaValueReader extends AbstractValueReader with SettingsAware {
case STRING => textValue(value, parser)
case TEXT => textValue(value, parser)
case KEYWORD => textValue(value, parser)
case WILDCARD => textValue(value, parser)
case BYTE => byteValue(value, parser)
case SHORT => shortValue(value, parser)
case INTEGER => intValue(value, parser)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,32 @@ class AbstractScalaEsScalaSparkSQL(prefix: String, readMetadata: jl.Boolean, pus
assertEquals(0, result(0).size)
}

@Test
def testWildcard() {
val mapping = wrapMapping("data", s"""{
| "properties": {
| "name": {
| "type": "wildcard"
| }
| }
| }
""".stripMargin)

val index = wrapIndex("sparksql-test-wildcard")
val typed = "data"
val (target, docPath) = makeTargets(index, typed)
RestUtils.touch(index)
RestUtils.putMapping(index, typed, mapping.getBytes(StringUtils.UTF_8))
val wildcardDocument = """{ "name" : "Chipotle Mexican Grill"}""".stripMargin
sc.makeRDD(Seq(wildcardDocument)).saveJsonToEs(target)
RestUtils.refresh(index)
val df = sqc.read.format("es").load(index)
val dataType = df.schema("name").dataType
assertEquals("string", dataType.typeName)
val head = df.head()
assertThat(head.getString(0), containsString("Chipotle"))
}

/**
* Take advantage of the fixed method order and clear out all created indices.
* The indices will last in Elasticsearch for all parameters of this test suite.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import org.elasticsearch.hadoop.serialization.FieldType.OBJECT
import org.elasticsearch.hadoop.serialization.FieldType.SHORT
import org.elasticsearch.hadoop.serialization.FieldType.STRING
import org.elasticsearch.hadoop.serialization.FieldType.TEXT
import org.elasticsearch.hadoop.serialization.FieldType.WILDCARD
import org.elasticsearch.hadoop.serialization.dto.mapping.Field
import org.elasticsearch.hadoop.serialization.dto.mapping.GeoField
import org.elasticsearch.hadoop.serialization.dto.mapping.GeoPointType
Expand Down Expand Up @@ -163,6 +164,7 @@ private[sql] object SchemaUtils {
case STRING => StringType
case TEXT => StringType
case KEYWORD => StringType
case WILDCARD => StringType
case HALF_FLOAT => FloatType
case SCALED_FLOAT => FloatType
case DATE => if (cfg.getMappingDateRich) TimestampType else StringType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,32 @@ class AbstractScalaEsScalaSparkSQL(prefix: String, readMetadata: jl.Boolean, pus
assertEquals(0, result(0).size)
}

@Test
def testWildcard() {
val mapping = wrapMapping("data", s"""{
| "properties": {
| "name": {
| "type": "wildcard"
| }
| }
| }
""".stripMargin)

val index = wrapIndex("sparksql-test-wildcard")
val typed = "data"
val (target, docPath) = makeTargets(index, typed)
RestUtils.touch(index)
RestUtils.putMapping(index, typed, mapping.getBytes(StringUtils.UTF_8))
val wildcardDocument = """{ "name" : "Chipotle Mexican Grill"}""".stripMargin
sc.makeRDD(Seq(wildcardDocument)).saveJsonToEs(target)
RestUtils.refresh(index)
val df = sqc.read.format("es").load(index)
val dataType = df.schema("name").dataType
assertEquals("string", dataType.typeName)
val head = df.head()
assertThat(head.getString(0), containsString("Chipotle"))
}

/**
* Take advantage of the fixed method order and clear out all created indices.
* The indices will last in Elasticsearch for all parameters of this test suite.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import org.elasticsearch.hadoop.serialization.FieldType.OBJECT
import org.elasticsearch.hadoop.serialization.FieldType.SHORT
import org.elasticsearch.hadoop.serialization.FieldType.STRING
import org.elasticsearch.hadoop.serialization.FieldType.TEXT
import org.elasticsearch.hadoop.serialization.FieldType.WILDCARD
import org.elasticsearch.hadoop.serialization.dto.mapping.Field
import org.elasticsearch.hadoop.serialization.dto.mapping.GeoField
import org.elasticsearch.hadoop.serialization.dto.mapping.GeoPointType
Expand Down Expand Up @@ -165,6 +166,7 @@ private[sql] object SchemaUtils {
case STRING => StringType
case TEXT => StringType
case KEYWORD => StringType
case WILDCARD => StringType
case DATE => if (cfg.getMappingDateRich) TimestampType else StringType
case DATE_NANOS => if (cfg.getMappingDateRich) TimestampType else StringType
case OBJECT => convertToStruct(field, geoInfo, absoluteName, arrayIncludes, arrayExcludes, cfg)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2369,6 +2369,31 @@ class AbstractScalaEsScalaSparkSQL(prefix: String, readMetadata: jl.Boolean, pus
assertEquals(0, result(0).size)
}

@Test
def testWildcard() {
val mapping = wrapMapping("data", s"""{
| "properties": {
| "name": {
| "type": "wildcard"
| }
| }
| }
""".stripMargin)

val index = wrapIndex("sparksql-test-wildcard")
val typed = "data"
val (target, docPath) = makeTargets(index, typed)
RestUtils.touch(index)
RestUtils.putMapping(index, typed, mapping.getBytes(StringUtils.UTF_8))
val wildcardDocument = """{ "name" : "Chipotle Mexican Grill"}""".stripMargin
sc.makeRDD(Seq(wildcardDocument)).saveJsonToEs(target)
RestUtils.refresh(index)
val df = sqc.read.format("es").load(index)
val dataType = df.schema("name").dataType
assertEquals("string", dataType.typeName)
val head = df.head()
assertThat(head.getString(0), containsString("Chipotle"))
}

/**
* Take advantage of the fixed method order and clear out all created indices.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import org.elasticsearch.hadoop.serialization.FieldType.OBJECT
import org.elasticsearch.hadoop.serialization.FieldType.SHORT
import org.elasticsearch.hadoop.serialization.FieldType.STRING
import org.elasticsearch.hadoop.serialization.FieldType.TEXT
import org.elasticsearch.hadoop.serialization.FieldType.WILDCARD
import org.elasticsearch.hadoop.serialization.dto.mapping.Field
import org.elasticsearch.hadoop.serialization.dto.mapping.GeoField
import org.elasticsearch.hadoop.serialization.dto.mapping.GeoPointType
Expand Down Expand Up @@ -165,6 +166,7 @@ private[sql] object SchemaUtils {
case STRING => StringType
case TEXT => StringType
case KEYWORD => StringType
case WILDCARD => StringType
case DATE => if (cfg.getMappingDateRich) TimestampType else StringType
case DATE_NANOS => if (cfg.getMappingDateRich) TimestampType else StringType
case OBJECT => convertToStruct(field, geoInfo, absoluteName, arrayIncludes, arrayExcludes, cfg)
Expand Down