]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4904 Disable dynamic mapping in ES
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 22 Nov 2013 17:47:17 +0000 (18:47 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Fri, 22 Nov 2013 17:47:17 +0000 (18:47 +0100)
sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java
sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java

index 9912a933b97491b153959e36f7ea3a7d2644aa54..622c2048c6397c6fda8fd2633c014658dd9df741 100644 (file)
@@ -54,10 +54,13 @@ public class SearchIndex {
   private static final Logger LOG = LoggerFactory.getLogger(SearchIndex.class);
 
   private static final Settings INDEX_DEFAULT_SETTINGS = ImmutableSettings.builder()
-    .put("index.number_of_shards", 1)
-    .put("index.number_of_replicas", 0)
+    .put("number_of_shards", 1)
+    .put("number_of_replicas", 0)
+    .put("mapper.dynamic", false)
     .build();
 
+  private static final String INDEX_DEFAULT_MAPPING = "{ \"_default_\": { \"dynamic\": \"strict\" } }";
+
   private SearchNode searchNode;
   private Client client;
 
@@ -138,6 +141,7 @@ public class SearchIndex {
         profiler.start(format("create index '%s'", index));
         indices.prepareCreate(index)
           .setSettings(INDEX_DEFAULT_SETTINGS)
+          .addMapping("_default_", INDEX_DEFAULT_MAPPING)
           .execute().actionGet();
       }
     } catch (Exception e) {
index f0dc8f37e66c237ac54c5e976ad52fb7edfb222f..aee360967c7136b95d4b8db0a19def8d68a12504 100644 (file)
@@ -23,6 +23,7 @@ package org.sonar.server.search;
 import com.github.tlrx.elasticsearch.test.EsSetup;
 import org.elasticsearch.common.io.BytesStream;
 import org.elasticsearch.common.xcontent.XContentFactory;
+import org.elasticsearch.index.mapper.StrictDynamicMappingException;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
@@ -124,4 +125,12 @@ public class SearchIndexTest {
     List<String> docIds = searchIndex.findDocumentIds(SearchQuery.create());
     assertThat(docIds).hasSize(numberOfDocuments);
   }
+
+  @Test(expected = StrictDynamicMappingException.class)
+  public void should_forbid_dynamic_mapping() throws Exception {
+    searchIndex.addMappingFromClasspath("index", "type1", "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json");
+    searchIndex.put("index", "type1", "666",
+      XContentFactory.jsonBuilder().startObject().field("unknown", "plouf").endObject()
+    );
+  }
 }