|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +package org.elasticsearch.xpack.versionfield; |
| 8 | + |
| 9 | +import org.elasticsearch.action.search.SearchResponse; |
| 10 | +import org.elasticsearch.common.xcontent.XContentFactory; |
| 11 | +import org.elasticsearch.plugins.Plugin; |
| 12 | +import org.elasticsearch.search.aggregations.AggregationBuilders; |
| 13 | +import org.elasticsearch.search.aggregations.bucket.terms.Terms; |
| 14 | +import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; |
| 15 | +import org.elasticsearch.test.ESIntegTestCase; |
| 16 | +import org.elasticsearch.xpack.core.LocalStateCompositeXPackPlugin; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | + |
| 21 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 22 | + |
| 23 | +public class VersionFieldIT extends ESIntegTestCase { |
| 24 | + |
| 25 | + @Override |
| 26 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 27 | + return List.of(VersionFieldPlugin.class, LocalStateCompositeXPackPlugin.class); |
| 28 | + } |
| 29 | + |
| 30 | + public void testTermsAggregation() throws Exception { |
| 31 | + String indexName = "test"; |
| 32 | + createIndex(indexName); |
| 33 | + |
| 34 | + client().admin() |
| 35 | + .indices() |
| 36 | + .preparePutMapping(indexName) |
| 37 | + .setSource( |
| 38 | + XContentFactory.jsonBuilder() |
| 39 | + .startObject() |
| 40 | + .startObject("_doc") |
| 41 | + .startObject("properties") |
| 42 | + .startObject("version") |
| 43 | + .field("type", "version") |
| 44 | + .endObject() |
| 45 | + .endObject() |
| 46 | + .endObject() |
| 47 | + .endObject() |
| 48 | + ) |
| 49 | + .get(); |
| 50 | + ensureGreen(); |
| 51 | + |
| 52 | + client().prepareIndex(indexName).setId("1").setSource(jsonBuilder().startObject().field("version", "1.0").endObject()).get(); |
| 53 | + client().prepareIndex(indexName).setId("2").setSource(jsonBuilder().startObject().field("version", "1.3.0").endObject()).get(); |
| 54 | + client().prepareIndex(indexName) |
| 55 | + .setId("3") |
| 56 | + .setSource(jsonBuilder().startObject().field("version", "2.1.0-alpha").endObject()) |
| 57 | + .get(); |
| 58 | + client().prepareIndex(indexName).setId("4").setSource(jsonBuilder().startObject().field("version", "2.1.0").endObject()).get(); |
| 59 | + client().prepareIndex(indexName).setId("5").setSource(jsonBuilder().startObject().field("version", "3.11.5").endObject()).get(); |
| 60 | + refresh(); |
| 61 | + |
| 62 | + // terms aggs |
| 63 | + SearchResponse response = client().prepareSearch(indexName) |
| 64 | + .addAggregation(AggregationBuilders.terms("myterms").field("version")) |
| 65 | + .get(); |
| 66 | + Terms terms = response.getAggregations().get("myterms"); |
| 67 | + List<? extends Bucket> buckets = terms.getBuckets(); |
| 68 | + |
| 69 | + assertEquals(5, buckets.size()); |
| 70 | + assertEquals("1.0", buckets.get(0).getKey()); |
| 71 | + assertEquals("1.3.0", buckets.get(1).getKey()); |
| 72 | + assertEquals("2.1.0-alpha", buckets.get(2).getKey()); |
| 73 | + assertEquals("2.1.0", buckets.get(3).getKey()); |
| 74 | + assertEquals("3.11.5", buckets.get(4).getKey()); |
| 75 | + } |
| 76 | +} |
0 commit comments