-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Adding ParsedCardinality #23973
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
Adding ParsedCardinality #23973
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,3 +128,4 @@ HyperLogLogPlusPlus getState() { | |
| return counts; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.aggregations.metrics.cardinality; | ||
|
|
||
| import org.elasticsearch.common.xcontent.ObjectParser; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.search.DocValueFormat; | ||
| import org.elasticsearch.search.aggregations.ParsedAggregation; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| public class ParsedCardinality extends ParsedAggregation implements Cardinality { | ||
|
|
||
| private long cardinalityValue; | ||
|
|
||
| @Override | ||
| public String getValueAsString() { | ||
| // InternalCardinality doesn't print "value_as_string", but you can get a formated value using | ||
| // getValueAsString(). That method uses the raw formatter so we also use it here. | ||
| return DocValueFormat.RAW.format((double) cardinalityValue); | ||
| } | ||
|
|
||
| @Override | ||
| public double value() { | ||
| return getValue(); | ||
| } | ||
|
|
||
| @Override | ||
| public long getValue() { | ||
| return cardinalityValue; | ||
| } | ||
|
|
||
| private void setValue(long cardinalityValue) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a bit pointless to have a private setter for a private field, but we need it for object parser I guess?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, thats the idea |
||
| this.cardinalityValue = cardinalityValue; | ||
| } | ||
|
|
||
| @Override | ||
| protected String getType() { | ||
| return CardinalityAggregationBuilder.NAME; | ||
| } | ||
|
|
||
| private static final ObjectParser<ParsedCardinality, Void> PARSER = new ObjectParser<>( | ||
| CardinalityAggregationBuilder.NAME, true, ParsedCardinality::new); | ||
|
|
||
| static { | ||
| declareCommonFields(PARSER); | ||
| PARSER.declareLong(ParsedCardinality::setValue, CommonFields.VALUE); | ||
| } | ||
|
|
||
| public static ParsedCardinality fromXContent(XContentParser parser, final String name) { | ||
| ParsedCardinality cardinality = PARSER.apply(parser, null); | ||
| cardinality.setName(name); | ||
| return cardinality; | ||
| } | ||
|
|
||
| @Override | ||
| protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) | ||
| throws IOException { | ||
| builder.field(CommonFields.VALUE.getPreferredName(), cardinalityValue); | ||
| return builder; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,20 +19,33 @@ | |
|
|
||
| package org.elasticsearch.search.aggregations.metrics.cardinality; | ||
|
|
||
| import org.elasticsearch.common.ParseField; | ||
| import org.elasticsearch.common.bytes.BytesReference; | ||
| import org.elasticsearch.common.io.stream.Writeable.Reader; | ||
| import org.elasticsearch.common.lease.Releasables; | ||
| import org.elasticsearch.common.settings.Settings; | ||
| import org.elasticsearch.common.util.MockBigArrays; | ||
| import org.elasticsearch.common.xcontent.NamedXContentRegistry; | ||
| import org.elasticsearch.common.xcontent.ToXContent; | ||
| import org.elasticsearch.common.xcontent.XContentHelper; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentType; | ||
| import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; | ||
| import org.elasticsearch.rest.action.search.RestSearchAction; | ||
| import org.elasticsearch.search.aggregations.Aggregation; | ||
| import org.elasticsearch.search.aggregations.InternalAggregationTestCase; | ||
| import org.elasticsearch.search.aggregations.pipeline.PipelineAggregator; | ||
| import org.junit.After; | ||
| import org.junit.Before; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; | ||
|
|
||
| public class InternalCardinalityTests extends InternalAggregationTestCase<InternalCardinality> { | ||
| private static List<HyperLogLogPlusPlus> algos; | ||
| private static int p; | ||
|
|
@@ -73,6 +86,41 @@ protected void assertReduced(InternalCardinality reduced, List<InternalCardinali | |
| } | ||
| } | ||
|
|
||
| public void testFromXContent() throws IOException { | ||
| InternalCardinality cardinality = createTestInstance(); | ||
| String type = cardinality.getWriteableName(); | ||
| String name = cardinality.getName(); | ||
| ToXContent.Params params = new ToXContent.MapParams(Collections.singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true")); | ||
| boolean humanReadable = randomBoolean(); | ||
| XContentType xContentType = randomFrom(XContentType.values()); | ||
| BytesReference originalBytes = toShuffledXContent(cardinality, xContentType, params, humanReadable); | ||
| ParsedCardinality parsed; | ||
| try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) { | ||
| assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); | ||
| assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); | ||
| assertEquals(type + "#" + name, parser.currentName()); | ||
| assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); | ||
| parsed = (ParsedCardinality) parser.namedObject(Aggregation.class, type, name); | ||
| assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken()); | ||
| assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); | ||
| assertNull(parser.nextToken()); | ||
| } | ||
| assertEquals(cardinality.getName(), parsed.getName()); | ||
| assertEquals(cardinality.getValue(), parsed.getValue(), Double.MIN_VALUE); | ||
|
||
| assertEquals(cardinality.getValueAsString(), parsed.getValueAsString()); | ||
| assertEquals(cardinality.getMetaData(), parsed.getMetaData()); | ||
| BytesReference finalAgg = XContentHelper.toXContent(parsed, xContentType, params, humanReadable); | ||
| assertToXContentEquivalent(originalBytes, finalAgg, xContentType); | ||
| } | ||
|
|
||
| @Override | ||
| protected NamedXContentRegistry xContentRegistry() { | ||
| NamedXContentRegistry.Entry entry = new NamedXContentRegistry.Entry(Aggregation.class, | ||
| new ParseField(CardinalityAggregationBuilder.NAME), | ||
| (parser, name) -> ParsedCardinality.fromXContent(parser, (String) name)); | ||
| return new NamedXContentRegistry(Collections.singletonList(entry)); | ||
| } | ||
|
|
||
| @After | ||
| public void cleanup() { | ||
| Releasables.close(algos); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that with this protected setter, you can now make the instance member private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done