<commit_before>/*
 * 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;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
import org.elasticsearch.search.aggregations.bucket.global.Global;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Test;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.search.aggregations.AggregationBuilders.cardinality;
import static org.elasticsearch.search.aggregations.AggregationBuilders.global;
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;

@ESIntegTestCase.SuiteScopeTestCase
public class LocalReduceCardinalityIT extends ESIntegTestCase {

    @Override
    public Settings indexSettings() {
        return Settings.builder()
                .put("index.number_of_shards", numberOfShards())
                .put("index.number_of_replicas", numberOfReplicas())
                .build();
    }

    static long numDocs;
    static long precisionThreshold;

    @Override
    public void setupSuiteScopeCluster() throws Exception {

        prepareCreate("idx").addMapping("type",
                jsonBuilder().startObject().startObject("type").startObject("properties")
                    .startObject("str_value")
                        .field("type", "string")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("str_values")
                        .field("type", "string")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("l_value")
                        .field("type", "long")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("l_values")
                        .field("type", "long")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                        .startObject("d_value")
                            .field("type", "double")
                            .startObject("fields")
                                .startObject("hash")
                                    .field("type", "murmur3")
                                .endObject()
                            .endObject()
                        .endObject()
                        .startObject("d_values")
                            .field("type", "double")
                                .startObject("fields")
                                    .startObject("hash")
                                        .field("type", "murmur3")
                                    .endObject()
                                .endObject()
                            .endObject()
                        .endObject()
                        .endObject().endObject()).execute().actionGet();

        numDocs = randomIntBetween(2, 100);
        precisionThreshold = randomIntBetween(0, 1 << randomInt(20));
        IndexRequestBuilder[] builders = new IndexRequestBuilder[(int) numDocs];
        for (int i = 0; i < numDocs; ++i) {
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder()
                    .startObject()
                        .field("str_value", "s" + i)
                        .field("str_values", new String[]{"s" + (i * 2), "s" + (i * 2 + 1)})
                        .field("l_value", i)
                        .field("l_values", new int[] {i * 2, i * 2 + 1})
                        .field("d_value", i)
                        .field("d_values", new double[]{i * 2, i * 2 + 1})
                    .endObject());
            builders[i].setRouting("str_value");
        }
        indexRandom(true, builders);
        createIndex("idx_unmapped");

        IndexRequestBuilder[] dummyDocsBuilder = new IndexRequestBuilder[10];
        for (int i = 0; i < dummyDocsBuilder.length; i++) {
            dummyDocsBuilder[i] = client().prepareIndex("idx", "type").setSource("a_field", "1");
        }
        indexRandom(true, dummyDocsBuilder);
<commit_msg>Change some variables<commit_after>/*
 * 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;

import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.script.Script;
import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode;
import org.elasticsearch.search.aggregations.bucket.global.Global;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
import org.elasticsearch.test.ESIntegTestCase;
import org.junit.Test;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.search.aggregations.AggregationBuilders.cardinality;
import static org.elasticsearch.search.aggregations.AggregationBuilders.global;
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
import org.elasticsearch.search.aggregations.metrics.cardinality.CardinalityBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.notNullValue;

@ESIntegTestCase.SuiteScopeTestCase
public class LocalReduceCardinalityIT extends ESIntegTestCase {

    @Override
    public Settings indexSettings() {
        return Settings.builder()
                .put("index.number_of_shards", numberOfShards())
                .put("index.number_of_replicas", numberOfReplicas())
                .build();
    }

    static long numDocs;
    static long precisionThreshold;

    @Override
    public void setupSuiteScopeCluster() throws Exception {

        prepareCreate("idx").addMapping("type",
                jsonBuilder().startObject().startObject("type").startObject("properties")
                    .startObject("str_value")
                        .field("type", "string")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("str_values")
                        .field("type", "string")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("l_value")
                        .field("type", "long")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("l_values")
                        .field("type", "long")
                        .startObject("fields")
                            .startObject("hash")
                                .field("type", "murmur3")
                            .endObject()
                        .endObject()
                    .endObject()
                        .startObject("d_value")
                            .field("type", "double")
                            .startObject("fields")
                                .startObject("hash")
                                    .field("type", "murmur3")
                                .endObject()
                            .endObject()
                        .endObject()
                        .startObject("d_values")
                            .field("type", "double")
                                .startObject("fields")
                                    .startObject("hash")
                                        .field("type", "murmur3")
                                    .endObject()
                                .endObject()
                            .endObject()
                        .endObject()
                        .endObject().endObject()).execute().actionGet();

        numDocs = randomIntBetween(2, 100);
        precisionThreshold = randomIntBetween(0, 1 << randomInt(20));
        IndexRequestBuilder[] builders = new IndexRequestBuilder[(int) numDocs];
        for (int i = 0; i < numDocs; ++i) {
            final String strVal = "s" + i;
            builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder()
                    .startObject()
                        .field("str_value", strVal)
                        .field("str_values", new String[]{"s" + (i * 2), "s" + (i * 2 + 1)})
                        .field("l_value", i)
                        .field("l_values", new int[] {i * 2, i * 2 + 1})
                        .field("d_value", i)
                        .field("d_values", new double[]{i * 2, i * 2 + 1})
                    .endObject());
            builders[i].setRouting("str_value");
        }
        indexRandom(true, builders);
        createIndex("idx_unmapped");

        IndexRequestBuilder[] dummyDocsBuilder = new IndexRequestBuilder[10];
        for (int i = 0; i < dummyDocsBuilder.length; i++) {
            dummyDocsBuilder[i] = client().prepareIndex("idx", "type").setSource("a_field", "1");
        }
        indexRandom(true, dummyDocsBuilder);

{'fraction_non_alphanumeric': 0.10578434454574033, 'fraction_numerical': 0.005029864822382898, 'mean_word_length': 1.7016985138004246, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3115015', 'n_tokens_mistral': 3147, 'n_tokens_neox': 2992, 'n_words': 662}