Bladeren bron

Allow parsing on non-string routing fields (#97729)

Allow parsing on non-string routing fields

Closes https://github.com/elastic/elasticsearch/issues/96552
tmgordeeva 2 jaren geleden
bovenliggende
commit
5b2917c876

+ 5 - 0
docs/changelog/97729.yaml

@@ -0,0 +1,5 @@
+pr: 97729
+summary: Allow parsing on non-string routing fields
+area: Aggregations
+type: bug
+issues: []

+ 36 - 0
modules/aggregations/src/yamlRestTest/resources/rest-api-spec/test/aggregations/time_series.yml

@@ -302,3 +302,39 @@ setup:
           mappings:
             _source:
               enabled: false
+
+---
+"Number for keyword routing field":
+  - skip:
+      version: " - 8.10.99"
+      reason: "Fix in 8.11"
+
+  - do:
+      bulk:
+        index: tsdb
+        refresh: true
+        body:
+          - '{ "index": {} }'
+          - '{ "key": 10, "val": 1, "@timestamp": "2021-10-01T00:00:10Z" }'
+          - '{ "index": {}}'
+          - '{ "key": 11, "val": 2, "@timestamp": "2021-10-01T00:00:00Z" }'
+
+  - do:
+      search:
+        index: tsdb
+        body:
+          query:
+            range:
+              "@timestamp":
+                gte: "2021-10-01T00:00:00Z"
+          size: 0
+          aggs:
+            ts:
+              time_series:
+                keyed: false
+
+  - match: { hits.total.value: 2 }
+  - length: { aggregations: 1 }
+
+  - match: { aggregations.ts.buckets.0.key: { "key": "10" } }
+  - match: { aggregations.ts.buckets.0.doc_count: 1 }

+ 4 - 0
server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java

@@ -337,6 +337,10 @@ public abstract class IndexRouting {
                     case VALUE_NULL:
                         source.nextToken();
                         break;
+                    case VALUE_NUMBER: // allow parsing numbers assuming routing fields are always keyword fields
+                        hashes.add(new NameAndHash(new BytesRef(path), hash(new BytesRef(source.text()))));
+                        source.nextToken();
+                        break;
                     default:
                         throw new ParsingException(
                             source.getTokenLocation(),

+ 10 - 0
server/src/test/java/org/elasticsearch/cluster/routing/IndexRoutingTests.java

@@ -583,6 +583,16 @@ public class IndexRoutingTests extends ESTestCase {
         assertIndexShard(routing, Map.of("foo.bar", "cat", "baz", "dog"), Math.floorMod(hash(List.of("foo.bar", "cat")), shards));
     }
 
+    public void testRoutingPathNumbersInSource() throws IOException {
+        int shards = between(2, 1000);
+        IndexRouting routing = indexRoutingForPath(shards, "foo");
+        long randomLong = randomLong();
+        assertIndexShard(routing, Map.of("foo", randomLong), Math.floorMod(hash(List.of("foo", Long.toString(randomLong))), shards));
+        double randomDouble = randomDouble();
+        assertIndexShard(routing, Map.of("foo", randomDouble), Math.floorMod(hash(List.of("foo", Double.toString(randomDouble))), shards));
+        assertIndexShard(routing, Map.of("foo", 123), Math.floorMod(hash(List.of("foo", "123")), shards));
+    }
+
     public void testRoutingPathBwc() throws IOException {
         IndexVersion version = IndexVersionUtils.randomCompatibleVersion(random());
         IndexRouting routing = indexRoutingForPath(version, 8, "dim.*,other.*,top");