You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2019/11/06 15:26:56 UTC

[GitHub] [calcite] shlok7296 commented on a change in pull request #1530: [CALCITE-3437] ElasticSearch adapter. Support MatchQuery in elasticse…

shlok7296 commented on a change in pull request #1530: [CALCITE-3437] ElasticSearch adapter. Support MatchQuery in elasticse…
URL: https://github.com/apache/calcite/pull/1530#discussion_r343158486
 
 

 ##########
 File path: elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticSearchAdapterTest.java
 ##########
 @@ -705,6 +737,97 @@ public void approximateCount() throws Exception {
             "state=AL; EXPR$1=3; EXPR$2=3",
             "state=AR; EXPR$1=3; EXPR$2=3");
   }
+  /**
+   * Test the ElasticSearch match query. The match query is translated from CONTAINS query which
+   * is build using RelBuilder, RexBuilder because the normal sql query assumes CONTAINS query
+   * is for date/period range
+   *
+   * Equivalent SQL query: select * from zipswithtext where city contains 'waltham'
+   *
+   * ElasticSearch query for it:
+   * {"query":{"constant_score":{"filter":{"match":{"city":"waltham"}}}}}
+   *
+   * Pre-requisite:
+   * The Match query can be best tested on "text" fields. Text fields create tokens and stores
+   * data in lowercase.
+   * The ZIPS index is having fields of type 'keyword'. Changing their type to text will fail
+   * aggregation, sort tests which should be tested on keyword field only.
+   *
+   * So created a new index 'zipswithtext' which is having city field as type 'text'.
+   *
+   * @throws Exception
+   */
+  @Test
+  public void testMatchQuery() throws Exception {
+
+    final Map<String, String> mapping = ImmutableMap.of("city", "text", "state",
+        "keyword", "pop", "long");
+    setupInstanceWithMapping(mapping, ZIPS_WITH_TEXT);
+
+    try {
+      CalciteConnection con = (CalciteConnection) newConnectionFactoryForCustomIndex
+          (ZIPS_WITH_TEXT).createConnection();
+      SchemaPlus postschema = con.getRootSchema().getSubSchema("elastic");
+
+      FrameworkConfig postConfig = Frameworks.newConfigBuilder()
+          .parserConfig(SqlParser.Config.DEFAULT)
+          .defaultSchema(postschema)
+          .build();
+
+      final RelBuilder builder = RelBuilder.create(postConfig);
+      builder.scan(ZIPS_WITH_TEXT);
+
+      final RelDataTypeFactory relDataTypeFactory = new SqlTypeFactoryImpl(
+          RelDataTypeSystem.DEFAULT);
+      final RexBuilder rexbuilder = new RexBuilder(relDataTypeFactory);
+
+      RexNode nameRexNode = rexbuilder.makeCall(SqlStdOperatorTable.ITEM,
+          rexbuilder.makeInputRef(relDataTypeFactory.createSqlType(SqlTypeName.ANY), 0),
+          rexbuilder.makeCharLiteral(
+            new NlsString("city", rexbuilder.getTypeFactory().getDefaultCharset().name(),
+            SqlCollation.COERCIBLE)));
+
+      RelDataType mapType = relDataTypeFactory.createMapType(
+          relDataTypeFactory.createSqlType(SqlTypeName.VARCHAR),
+          relDataTypeFactory.createTypeWithNullability(
+            relDataTypeFactory.createSqlType(SqlTypeName.ANY), true));
+
+      ArrayList<RexNode> namedList = new ArrayList<RexNode>(2);
+      namedList.add(rexbuilder.makeInputRef(mapType, 0));
+      namedList.add(nameRexNode);
+
+      //Add fields in builder stack so it is accessible while filter preparation
+      builder.projectNamed(namedList, Arrays.asList("_MAP", "city"), true);
+
+      RexNode filterRexNode = builder
+          .call(SqlStdOperatorTable.CONTAINS, builder.field("city"),
+          builder.literal("waltham"));
+      builder.filter(filterRexNode);
+
+      String builderExpected = ""
+          + "LogicalFilter(condition=[CONTAINS($1, 'waltham')])\n"
+          + "  LogicalProject(_MAP=[$0], city=[ITEM($0, 'city')])\n"
+          + "    LogicalTableScan(table=[[elastic, zipswithtext]])\n";
+
+      RelNode root = builder.build();
+
+      RelRunner ru = (RelRunner) con.unwrap(Class.forName("org.apache.calcite.tools.RelRunner"));
+      try (PreparedStatement preparedStatement = ru.prepare(root)) {
+
+        String s = CalciteAssert.toString(preparedStatement.executeQuery());
+        final String result = ""
+            + "_MAP={id=02154, city=NORTH WALTHAM, loc=[-71.236497, 42.382492], pop=57871, state=MA}; city=NORTH WALTHAM\n";
+
+        //Validate query prepared
+        assertThat(root, hasTree(builderExpected));
+
+        //Validate result returned from ES
+        assertThat(s, is(result));
+      }
+    } catch (Exception e) {
 
 Review comment:
   Done

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services