@Test
  @SuppressWarnings("squid:S2925")
  public void testThatMappingFromTemplateIsApplied() throws Exception {
    registry.counter(name("test", "cache-evictions")).inc();
    reportAndRefresh();

    // somehow the cluster state is not immediately updated... need to check
    Thread.sleep(200);
    ClusterStateResponse clusterStateResponse =
        client()
            .admin()
            .cluster()
            .prepareState()
            .setRoutingTable(false)
            .setLocal(false)
            .setNodes(true)
            .setIndices(indexWithDate)
            .execute()
            .actionGet();

    org.assertj.core.api.Assertions.assertThat(
            clusterStateResponse.getState().getMetaData().getIndices().containsKey(indexWithDate))
        .isTrue();
    IndexMetaData indexMetaData =
        clusterStateResponse.getState().getMetaData().getIndices().get(indexWithDate);
    org.assertj.core.api.Assertions.assertThat(indexMetaData.getMappings().containsKey("counter"))
        .isTrue();
    Map<String, Object> properties =
        getAsMap(indexMetaData.mapping("counter").sourceAsMap(), "properties");
    Map<String, Object> mapping = getAsMap(properties, "name");
    org.assertj.core.api.Assertions.assertThat(mapping).containsKey("index");
    org.assertj.core.api.Assertions.assertThat(mapping.get("index").toString())
        .isEqualTo("not_analyzed");
  }
Example #2
0
    @NotNull
    protected EsMapping readMapping() {
      try {
        try {
          IndexMetaData indexMetaData = getIndexMetaData();

          if (indexMetaData != null) {
            MappingMetaData metaData = indexMetaData.mapping(getIndexName());
            if (metaData != null) {
              byte[] mappingSource = metaData.source().uncompressed();
              return new EsMapping(getIndexName(), mappingSource);
            }
          }

          mappingCreated = false;
          return new EsMapping(getIndexName());
        } catch (IndexMissingException e) {
          mappingCreated = false;
          return new EsMapping(getIndexName());
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
    }
  private void applyMappings(ClusterChangedEvent event) {
    // go over and update mappings
    for (IndexMetaData indexMetaData : event.state().metaData()) {
      if (!indicesService.hasIndex(indexMetaData.index())) {
        // we only create / update here
        continue;
      }
      List<String> typesToRefresh = Lists.newArrayList();
      String index = indexMetaData.index();
      IndexService indexService = indicesService.indexService(index);
      if (indexService == null) {
        // got deleted on us, ignore (closing the node)
        return;
      }
      try {
        MapperService mapperService = indexService.mapperService();
        // first, go over and update the _default_ mapping (if exists)
        if (indexMetaData.mappings().containsKey(MapperService.DEFAULT_MAPPING)) {
          boolean requireRefresh =
              processMapping(
                  index,
                  mapperService,
                  MapperService.DEFAULT_MAPPING,
                  indexMetaData.mapping(MapperService.DEFAULT_MAPPING).source());
          if (requireRefresh) {
            typesToRefresh.add(MapperService.DEFAULT_MAPPING);
          }
        }

        // go over and add the relevant mappings (or update them)
        for (ObjectCursor<MappingMetaData> cursor : indexMetaData.mappings().values()) {
          MappingMetaData mappingMd = cursor.value;
          String mappingType = mappingMd.type();
          CompressedXContent mappingSource = mappingMd.source();
          if (mappingType.equals(MapperService.DEFAULT_MAPPING)) { // we processed _default_ first
            continue;
          }
          boolean requireRefresh = processMapping(index, mapperService, mappingType, mappingSource);
          if (requireRefresh) {
            typesToRefresh.add(mappingType);
          }
        }
        if (!typesToRefresh.isEmpty() && sendRefreshMapping) {
          nodeMappingRefreshAction.nodeMappingRefresh(
              event.state(),
              new NodeMappingRefreshAction.NodeMappingRefreshRequest(
                  index,
                  indexMetaData.indexUUID(),
                  typesToRefresh.toArray(new String[typesToRefresh.size()]),
                  event.state().nodes().localNodeId()));
        }
      } catch (Throwable t) {
        // if we failed the mappings anywhere, we need to fail the shards for this index, note, we
        // safeguard
        // by creating the processing the mappings on the master, or on the node the mapping was
        // introduced on,
        // so this failure typically means wrong node level configuration or something similar
        for (IndexShard indexShard : indexService) {
          ShardRouting shardRouting = indexShard.routingEntry();
          failAndRemoveShard(shardRouting, indexService, true, "failed to update mappings", t);
        }
      }
    }
  }