private void test(
     String name, Class<? extends HashFunction> expectedHashFunction, boolean expectedUseType)
     throws Exception {
   Path zippedIndexDir = getDataPath("/org/elasticsearch/cluster/routing/" + name + ".zip");
   Settings baseSettings = prepareBackwardsDataDir(zippedIndexDir);
   internalCluster()
       .startNode(Settings.builder().put(baseSettings).put(Node.HTTP_ENABLED, true).build());
   ensureYellow("test");
   GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().get();
   assertArrayEquals(new String[] {"test"}, getIndexResponse.indices());
   GetSettingsResponse getSettingsResponse =
       client().admin().indices().prepareGetSettings("test").get();
   assertEquals(
       expectedHashFunction.getName(),
       getSettingsResponse.getSetting("test", IndexMetaData.SETTING_LEGACY_ROUTING_HASH_FUNCTION));
   assertEquals(
       Boolean.valueOf(expectedUseType).toString(),
       getSettingsResponse.getSetting("test", IndexMetaData.SETTING_LEGACY_ROUTING_USE_TYPE));
   SearchResponse allDocs = client().prepareSearch("test").get();
   assertSearchResponse(allDocs);
   assertHitCount(allDocs, 4);
   // Make sure routing works
   for (SearchHit hit : allDocs.getHits().hits()) {
     GetResponse get = client().prepareGet(hit.index(), hit.type(), hit.id()).get();
     assertTrue(get.isExists());
   }
 }
  public void testAutoCreateWithDisabledDynamicMappings() throws Exception {
    assertAcked(
        client()
            .admin()
            .indices()
            .preparePutTemplate("my_template")
            .setCreate(true)
            .setPatterns(Collections.singletonList("index_*"))
            .addMapping("foo", "field", "type=keyword")
            .setSettings(Settings.builder().put("index.mapper.dynamic", false).build())
            .get());

    // succeeds since 'foo' has an explicit mapping in the template
    indexRandom(
        true, false, client().prepareIndex("index_1", "foo", "1").setSource("field", "abc"));

    // fails since 'bar' does not have an explicit mapping in the template and dynamic template
    // creation is disabled
    TypeMissingException e1 =
        expectThrows(
            TypeMissingException.class,
            () -> client().prepareIndex("index_2", "bar", "1").setSource("field", "abc").get());
    assertEquals("type[bar] missing", e1.getMessage());
    assertEquals(
        "trying to auto create mapping, but dynamic mapping is disabled",
        e1.getCause().getMessage());

    // make sure no mappings were created for bar
    GetIndexResponse getIndexResponse =
        client().admin().indices().prepareGetIndex().addIndices("index_2").get();
    assertFalse(getIndexResponse.mappings().containsKey("bar"));
  }
 void assertIndexSanity() {
   GetIndexResponse getIndexResponse = client().admin().indices().prepareGetIndex().get();
   logger.info("Found indices: {}", Arrays.toString(getIndexResponse.indices()));
   assertEquals(1, getIndexResponse.indices().length);
   assertEquals("test", getIndexResponse.indices()[0]);
   ensureYellow("test");
   SearchResponse test = client().prepareSearch("test").get();
   assertThat(test.getHits().getTotalHits(), greaterThanOrEqualTo(1l));
 }
 void assertIndexSanity(String indexName) {
   GetIndexResponse getIndexResponse =
       client().admin().indices().prepareGetIndex().addIndices(indexName).get();
   assertEquals(1, getIndexResponse.indices().length);
   assertEquals(indexName, getIndexResponse.indices()[0]);
   ensureYellow(indexName);
   SearchResponse test = client().prepareSearch(indexName).get();
   assertThat(test.getHits().getTotalHits(), greaterThanOrEqualTo(1l));
 }
Esempio n. 5
0
 void assertIndexSanity(String indexName, Version indexCreated) {
   GetIndexResponse getIndexResponse =
       client().admin().indices().prepareGetIndex().addIndices(indexName).get();
   assertEquals(1, getIndexResponse.indices().length);
   assertEquals(indexName, getIndexResponse.indices()[0]);
   Version actualVersionCreated =
       Version.indexCreated(getIndexResponse.getSettings().get(indexName));
   assertEquals(indexCreated, actualVersionCreated);
   ensureYellow(indexName);
   RecoveryResponse recoveryResponse =
       client()
           .admin()
           .indices()
           .prepareRecoveries(indexName)
           .setDetailed(true)
           .setActiveOnly(false)
           .get();
   boolean foundTranslog = false;
   for (List<RecoveryState> states : recoveryResponse.shardRecoveryStates().values()) {
     for (RecoveryState state : states) {
       if (state.getStage() == RecoveryState.Stage.DONE
           && state.getPrimary()
           && state.getRecoverySource().getType() == RecoverySource.Type.EXISTING_STORE) {
         assertFalse("more than one primary recoverd?", foundTranslog);
         assertNotEquals(0, state.getTranslog().recoveredOperations());
         foundTranslog = true;
       }
     }
   }
   assertTrue("expected translog but nothing was recovered", foundTranslog);
   IndicesSegmentResponse segmentsResponse =
       client().admin().indices().prepareSegments(indexName).get();
   IndexSegments segments = segmentsResponse.getIndices().get(indexName);
   int numCurrent = 0;
   int numBWC = 0;
   for (IndexShardSegments indexShardSegments : segments) {
     for (ShardSegments shardSegments : indexShardSegments) {
       for (Segment segment : shardSegments) {
         if (indexCreated.luceneVersion.equals(segment.version)) {
           numBWC++;
           if (Version.CURRENT.luceneVersion.equals(segment.version)) {
             numCurrent++;
           }
         } else if (Version.CURRENT.luceneVersion.equals(segment.version)) {
           numCurrent++;
         } else {
           fail("unexpected version " + segment.version);
         }
       }
     }
   }
   assertNotEquals("expected at least 1 current segment after translog recovery", 0, numCurrent);
   assertNotEquals("expected at least 1 old segment", 0, numBWC);
   SearchResponse test = client().prepareSearch(indexName).get();
   assertThat(test.getHits().getTotalHits(), greaterThanOrEqualTo(1L));
 }