Search in sources :

Example 1 with InternalBucket

use of org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket in project anomaly-detection by opensearch-project.

the class NoPowermockSearchFeatureDaoTests method testParseBuckets.

@SuppressWarnings("rawtypes")
public void testParseBuckets() throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
    // cannot mock final class HyperLogLogPlusPlus
    HyperLogLogPlusPlus hllpp = new HyperLogLogPlusPlus(randomIntBetween(AbstractHyperLogLog.MIN_PRECISION, AbstractHyperLogLog.MAX_PRECISION), new MockBigArrays(new MockPageCacheRecycler(Settings.EMPTY), new NoneCircuitBreakerService()), 1);
    hllpp.collect(0, BitMixer.mix64(randomIntBetween(1, 100)));
    hllpp.collect(0, BitMixer.mix64(randomIntBetween(1, 100)));
    Constructor ctor = null;
    ctor = InternalCardinality.class.getDeclaredConstructor(String.class, AbstractHyperLogLogPlusPlus.class, Map.class);
    ctor.setAccessible(true);
    InternalCardinality cardinality = (InternalCardinality) ctor.newInstance("impactUniqueAccounts", hllpp, new HashMap<>());
    // have to use reflection as all of InternalFilter's constructor are not public
    ctor = InternalFilter.class.getDeclaredConstructor(String.class, long.class, InternalAggregations.class, Map.class);
    ctor.setAccessible(true);
    String featureId = "deny_max";
    InternalFilter internalFilter = (InternalFilter) ctor.newInstance(featureId, 100, InternalAggregations.from(Arrays.asList(cardinality)), new HashMap<>());
    InternalBucket bucket = new InternalFilters.InternalBucket("test", randomIntBetween(0, 1000), InternalAggregations.from(Arrays.asList(internalFilter)), true);
    Optional<double[]> parsedResult = searchFeatureDao.parseBucket(bucket, Arrays.asList(featureId));
    assertTrue(parsedResult.isPresent());
    double[] parsedCardinality = parsedResult.get();
    assertEquals(1, parsedCardinality.length);
    assertEquals(2, parsedCardinality[0], 0.001);
    // release MockBigArrays; otherwise, test will fail
    Releasables.close(hllpp);
}
Also used : MockPageCacheRecycler(org.opensearch.common.util.MockPageCacheRecycler) HashMap(java.util.HashMap) Constructor(java.lang.reflect.Constructor) MockBigArrays(org.opensearch.common.util.MockBigArrays) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) InternalFilter(org.opensearch.search.aggregations.bucket.filter.InternalFilter) InternalBucket(org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket) AbstractHyperLogLogPlusPlus(org.opensearch.search.aggregations.metrics.AbstractHyperLogLogPlusPlus) HyperLogLogPlusPlus(org.opensearch.search.aggregations.metrics.HyperLogLogPlusPlus) AbstractHyperLogLogPlusPlus(org.opensearch.search.aggregations.metrics.AbstractHyperLogLogPlusPlus) InternalCardinality(org.opensearch.search.aggregations.metrics.InternalCardinality) Map(java.util.Map) HashMap(java.util.HashMap) NoneCircuitBreakerService(org.opensearch.indices.breaker.NoneCircuitBreakerService)

Example 2 with InternalBucket

use of org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket in project OpenSearch by opensearch-project.

the class InternalFiltersTests method mutateInstance.

@Override
protected InternalFilters mutateInstance(InternalFilters instance) {
    String name = instance.getName();
    List<InternalBucket> buckets = instance.getBuckets();
    Map<String, Object> metadata = instance.getMetadata();
    switch(between(0, 2)) {
        case 0:
            name += randomAlphaOfLength(5);
            break;
        case 1:
            buckets = new ArrayList<>(buckets);
            buckets.add(new InternalFilters.InternalBucket("test", randomIntBetween(0, 1000), InternalAggregations.EMPTY, keyed));
            break;
        case 2:
        default:
            if (metadata == null) {
                metadata = new HashMap<>(1);
            } else {
                metadata = new HashMap<>(instance.getMetadata());
            }
            metadata.put(randomAlphaOfLength(15), randomInt());
            break;
    }
    return new InternalFilters(name, buckets, keyed, metadata);
}
Also used : InternalBucket(org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket) InternalBucket(org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket)

Aggregations

InternalBucket (org.opensearch.search.aggregations.bucket.filter.InternalFilters.InternalBucket)2 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 MockBigArrays (org.opensearch.common.util.MockBigArrays)1 MockPageCacheRecycler (org.opensearch.common.util.MockPageCacheRecycler)1 NoneCircuitBreakerService (org.opensearch.indices.breaker.NoneCircuitBreakerService)1 InternalAggregations (org.opensearch.search.aggregations.InternalAggregations)1 InternalFilter (org.opensearch.search.aggregations.bucket.filter.InternalFilter)1 AbstractHyperLogLogPlusPlus (org.opensearch.search.aggregations.metrics.AbstractHyperLogLogPlusPlus)1 HyperLogLogPlusPlus (org.opensearch.search.aggregations.metrics.HyperLogLogPlusPlus)1 InternalCardinality (org.opensearch.search.aggregations.metrics.InternalCardinality)1