You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ar...@apache.org on 2020/07/20 06:44:00 UTC
[metamodel] 03/07: Fixed resolution of type of nested objects for
Elasticsearch.
This is an automated email from the ASF dual-hosted git repository.
arjansh pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/metamodel.git
commit bb4ae066218617c961bd2980069c30c7095fbc6e
Author: Arjan Seijkens <a....@quadient.com>
AuthorDate: Fri Jul 3 13:52:08 2020 +0200
Fixed resolution of type of nested objects for Elasticsearch.
---
.../common/ElasticSearchMetaDataParser.java | 8 +++-
.../rest/ElasticSearchRestNestedDataIT.java | 46 ++++------------------
2 files changed, 15 insertions(+), 39 deletions(-)
diff --git a/elasticsearch/common/src/main/java/org/apache/metamodel/elasticsearch/common/ElasticSearchMetaDataParser.java b/elasticsearch/common/src/main/java/org/apache/metamodel/elasticsearch/common/ElasticSearchMetaDataParser.java
index 32f07ff..6979c54 100644
--- a/elasticsearch/common/src/main/java/org/apache/metamodel/elasticsearch/common/ElasticSearchMetaDataParser.java
+++ b/elasticsearch/common/src/main/java/org/apache/metamodel/elasticsearch/common/ElasticSearchMetaDataParser.java
@@ -62,7 +62,13 @@ public class ElasticSearchMetaDataParser {
final String metaDataFieldType = getMetaDataFieldTypeFromMetaDataField(fieldMetadata);
if (metaDataFieldType == null) {
- return ColumnType.STRING;
+ // When we're dealing with a field with nested values, it doesn't have a "type", but it has "properties". In
+ // that case we should handle it as a field with the type "Map".
+ if (fieldMetadata.get("properties") != null) {
+ return ColumnType.MAP;
+ } else {
+ return ColumnType.STRING;
+ }
}
return ElasticSearchUtils.getColumnTypeFromElasticSearchType(metaDataFieldType);
diff --git a/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/ElasticSearchRestNestedDataIT.java b/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/ElasticSearchRestNestedDataIT.java
index fc5ca69..890cda7 100644
--- a/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/ElasticSearchRestNestedDataIT.java
+++ b/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/ElasticSearchRestNestedDataIT.java
@@ -38,7 +38,6 @@ import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
-import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -86,45 +85,11 @@ public class ElasticSearchRestNestedDataIT {
assertThat(table.getColumnNames(), containsInAnyOrder("_id", "message", "user"));
- dataContext.refreshSchemas();
-
- try (final DataSet dataSet = dataContext
- .query()
- .from(DEFAULT_TABLE_NAME)
- .select("user")
- .and("message")
- .execute()) {
- assertEquals(ElasticSearchRestDataSet.class, dataSet.getClass());
-
- assertTrue(dataSet.next());
- final Row row = dataSet.getRow();
- assertEquals("This is what I have to say.", row.getValue(table.getColumnByName("message")));
-
- assertTrue(row.getValue(table.getColumnByName("user")) instanceof Map);
- }
-
assertEquals(ColumnType.STRING, table.getColumnByName("message").getType());
assertEquals(ColumnType.MAP, table.getColumnByName("user").getType());
- }
-
- @Test
- public void testIndexOfDocumentWithDots() throws Exception {
- final String document =
- "{ \"user.fullname\": \"John Doe\", "
- + "\"user.address\": \"Main street 1, Newville\", "
- + "\"message\": \"This is what I have to say.\" }";
-
- final IndexRequest indexRequest = new IndexRequest(INDEX_NAME).id("1");
- indexRequest.source(document, XContentType.JSON);
-
- client.index(indexRequest, RequestOptions.DEFAULT);
-
- final Table table = dataContext.getDefaultSchema().getTableByName(DEFAULT_TABLE_NAME);
-
- assertThat(table.getColumnNames(), containsInAnyOrder("_id", "message", "user"));
dataContext.refreshSchemas();
-
+
try (final DataSet dataSet = dataContext
.query()
.from(DEFAULT_TABLE_NAME)
@@ -136,9 +101,14 @@ public class ElasticSearchRestNestedDataIT {
assertTrue(dataSet.next());
final Row row = dataSet.getRow();
assertEquals("This is what I have to say.", row.getValue(table.getColumnByName("message")));
+
+ final Object userValue = row.getValue(table.getColumnByName("user"));
+ assertTrue(userValue instanceof Map);
- assertNotNull(row.getValue(table.getColumnByName("user")));
+ @SuppressWarnings("rawtypes")
+ final Map userValueMap = (Map) userValue;
+ assertEquals("John Doe", userValueMap.get("fullname"));
+ assertEquals("Main street 1, Newville", userValueMap.get("address"));
}
}
-
}