/*
 * Licensed to ElasticSearch and Shay Banon 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.indices.mapping;

import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.AbstractIntegrationTest;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;

import static org.hamcrest.Matchers.emptyIterable;

public class ConcurrentDynamicTemplateTests extends AbstractIntegrationTest {

    private final String mappingType = "test-mapping";

    @Test // see #3544
    public void testConcurrentDynamicMapping() throws Exception {
        final String mapping = "{" + mappingType + ": {" + "\"properties\": {" + "\"an_id\": {"
                + "\"type\": \"string\"," + "\"store\": \"yes\"," + "\"index\": \"not_analyzed\"" + "}" + "}," + "\"dynamic_templates\": ["
                + "{" + "\"participants\": {" + "\"path_match\": \"*\"," + "\"mapping\": {" + "\"type\": \"string\"," + "\"store\": \"yes\","
                + "\"index\": \"analyzed\"," + "\"analyzer\": \"whitespace\"" + "}" + "}" + "}" + "]" + "}" + "}";
        // The 'fieldNames' array is used to help with retrieval of index terms
        // after testing

        final String fieldName = "participants.ACCEPTED";
        int iters = atLeast(5);
        for (int i = 0; i < iters; i++) {
            wipeIndex("test");
            client().admin().indices().prepareCreate("test").addMapping(mappingType, mapping).execute().actionGet();
            ensureYellow();
            int numDocs = atLeast(5);
            final CountDownLatch latch = new CountDownLatch(numDocs);
            final List<Throwable> throwable = new CopyOnWriteArrayList<Throwable>();
            for (int j = 0; j < numDocs; j++) {
                Map<String, Object> source = new HashMap<String, Object>();
                source.put("an_id", UUID.randomUUID().toString());
                source.put(fieldName, "test-user");
                client().prepareIndex("test", mappingType).setSource(source).setConsistencyLevel(WriteConsistencyLevel.QUORUM).execute(new ActionListener<IndexResponse>() {
                    @Override
                    public void onResponse(IndexResponse response) {
                        latch.countDown();
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        throwable.add(e);
                        latch.countDown();
                    }
                });
            }
            latch.await();
            assertThat(throwable, emptyIterable());
            refresh();
            assertEquals(numDocs, client().prepareSearch("test").setQuery(QueryBuilders.matchQuery(fieldName, "test-user")).execute().actionGet().getHits().getTotalHits());
            assertEquals(0, client().prepareSearch("test").setQuery(QueryBuilders.matchQuery(fieldName, "test user")).execute().actionGet().getHits().getTotalHits());

        }
    }

}
@@ -844,16 +844,17 @@
 pping;%0A%0A
+%0A
 import o
@@ -929,82 +929,128 @@
 ion.
-WriteConsistencyLevel;%0Aimport org.elasticsearch.action.index.IndexResponse
+index.IndexResponse;%0Aimport org.elasticsearch.common.Strings;%0Aimport org.elasticsearch.common.settings.ImmutableSettings
 ;%0Aim
@@ -1253,31 +1253,8 @@
 ap;%0A
-import java.util.UUID;%0A
 impo
@@ -1344,16 +1344,102 @@
 Latch;%0A%0A
+import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.assertHitCount;%0A
 import s
@@ -2528,16 +2528,268 @@
 (%22test%22)
+%0A                .setSettings(%0A                        ImmutableSettings.settingsBuilder()%0A                        .put(%22number_of_shards%22, between(1,5))%0A                        .put(%22number_of_replicas%22, between(0,1)).build())%0A                        
 .addMapp
@@ -2891,33 +2891,34 @@
 mDocs = atLeast(
-5
+10
 );%0A            f
@@ -3223,35 +3223,44 @@
 d%22, 
-UUID.randomUUID().toString(
+Strings.randomBase64UUID(getRandom()
 ));%0A
@@ -3392,58 +3392,8 @@
 ce).
-setConsistencyLevel(WriteConsistencyLevel.QUORUM).
 exec
@@ -3951,24 +3951,17 @@
 sert
-Equals(numDocs, 
+HitCount(
 clie
@@ -4050,54 +4050,22 @@
 %22)).
-execute().actionGet().getHits().getTotalHits()
+get(), numDocs
 );%0A 
@@ -4085,18 +4085,17 @@
 sert
-Equals(0, 
+HitCount(
 clie
@@ -4184,54 +4184,16 @@
 %22)).
-execute().actionGet().getHits().getTotalHits()
+get(), 0
 );%0A%0A

Make testcase more reproducible