public void testBackCompatOverrideDefaultIndexAndSearchAnalyzer() {
   Version version =
       VersionUtils.randomVersionBetween(
           getRandom(),
           VersionUtils.getFirstVersion(),
           VersionUtils.getPreviousVersion(Version.V_3_0_0));
   Settings settings =
       Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
   Map<String, AnalyzerProvider> analyzers = new HashMap<>();
   analyzers.put("default_index", analyzerProvider("default_index"));
   analyzers.put("default_search", analyzerProvider("default_search"));
   AnalysisService analysisService =
       new AnalysisService(
           IndexSettingsModule.newIndexSettings("index", settings),
           analyzers,
           Collections.emptyMap(),
           Collections.emptyMap(),
           Collections.emptyMap());
   assertThat(
       analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
   assertThat(
       analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
   assertThat(
       analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
 }
  @Inject
  public MapperService(
      Index index,
      @IndexSettings Settings indexSettings,
      AnalysisService analysisService,
      SimilarityLookupService similarityLookupService,
      ScriptService scriptService) {
    super(index, indexSettings);
    this.analysisService = analysisService;
    this.fieldTypes = new FieldTypeLookup();
    this.documentParser =
        new DocumentMapperParser(
            indexSettings, this, analysisService, similarityLookupService, scriptService);
    this.indexAnalyzer =
        new MapperAnalyzerWrapper(analysisService.defaultIndexAnalyzer(), INDEX_ANALYZER_EXTRACTOR);
    this.searchAnalyzer =
        new MapperAnalyzerWrapper(
            analysisService.defaultSearchAnalyzer(), SEARCH_ANALYZER_EXTRACTOR);
    this.searchQuoteAnalyzer =
        new MapperAnalyzerWrapper(
            analysisService.defaultSearchQuoteAnalyzer(), SEARCH_QUOTE_ANALYZER_EXTRACTOR);

    this.dynamic = indexSettings.getAsBoolean("index.mapper.dynamic", true);
    defaultPercolatorMappingSource =
        "{\n"
            + "\"_default_\":{\n"
            + "\"properties\" : {\n"
            + "\"query\" : {\n"
            + "\"type\" : \"object\",\n"
            + "\"enabled\" : false\n"
            + "}\n"
            + "}\n"
            + "}\n"
            + "}";
    if (index.getName().equals(ScriptService.SCRIPT_INDEX)) {
      defaultMappingSource =
          "{"
              + "\"_default_\": {"
              + "\"properties\": {"
              + "\"script\": { \"enabled\": false },"
              + "\"template\": { \"enabled\": false }"
              + "}"
              + "}"
              + "}";
    } else {
      defaultMappingSource = "{\"_default_\":{}}";
    }

    if (logger.isTraceEnabled()) {
      logger.trace(
          "using dynamic[{}], default mapping source[{}], default percolator mapping source[{}]",
          dynamic,
          defaultMappingSource,
          defaultPercolatorMappingSource);
    } else if (logger.isDebugEnabled()) {
      logger.debug("using dynamic[{}]", dynamic);
    }
  }
  @Inject
  public MapperService(
      Index index,
      @IndexSettings Settings indexSettings,
      Environment environment,
      AnalysisService analysisService) {
    super(index, indexSettings);
    this.analysisService = analysisService;
    this.documentParser = new DocumentMapperParser(index, indexSettings, analysisService);
    this.searchAnalyzer = new SmartIndexNameSearchAnalyzer(analysisService.defaultSearchAnalyzer());

    this.dynamic = componentSettings.getAsBoolean("dynamic", true);
    String defaultMappingLocation = componentSettings.get("default_mapping_location");
    URL defaultMappingUrl;
    if (defaultMappingLocation == null) {
      try {
        defaultMappingUrl = environment.resolveConfig("default-mapping.json");
      } catch (FailedToResolveConfigException e) {
        // not there, default to the built in one
        defaultMappingUrl =
            indexSettings
                .getClassLoader()
                .getResource("org/elasticsearch/index/mapper/default-mapping.json");
      }
    } else {
      try {
        defaultMappingUrl = environment.resolveConfig(defaultMappingLocation);
      } catch (FailedToResolveConfigException e) {
        // not there, default to the built in one
        try {
          defaultMappingUrl = new File(defaultMappingLocation).toURI().toURL();
        } catch (MalformedURLException e1) {
          throw new FailedToResolveConfigException(
              "Failed to resolve dynamic mapping location [" + defaultMappingLocation + "]");
        }
      }
    }

    try {
      defaultMappingSource =
          Streams.copyToString(
              new InputStreamReader(defaultMappingUrl.openStream(), Charsets.UTF_8));
    } catch (IOException e) {
      throw new MapperException(
          "Failed to load default mapping source from [" + defaultMappingLocation + "]", e);
    }

    logger.debug(
        "using dynamic[{}], default mapping: default_mapping_location[{}], loaded_from[{}] and source[{}]",
        dynamic,
        defaultMappingLocation,
        defaultMappingUrl,
        defaultMappingSource);
  }
  public MapperService(
      IndexSettings indexSettings,
      AnalysisService analysisService,
      SimilarityService similarityService,
      MapperRegistry mapperRegistry,
      Supplier<QueryShardContext> queryShardContextSupplier) {
    super(indexSettings);
    this.analysisService = analysisService;
    this.fieldTypes = new FieldTypeLookup();
    this.documentParser =
        new DocumentMapperParser(
            indexSettings,
            this,
            analysisService,
            similarityService,
            mapperRegistry,
            queryShardContextSupplier);
    this.indexAnalyzer =
        new MapperAnalyzerWrapper(analysisService.defaultIndexAnalyzer(), p -> p.indexAnalyzer());
    this.searchAnalyzer =
        new MapperAnalyzerWrapper(analysisService.defaultSearchAnalyzer(), p -> p.searchAnalyzer());
    this.searchQuoteAnalyzer =
        new MapperAnalyzerWrapper(
            analysisService.defaultSearchQuoteAnalyzer(), p -> p.searchQuoteAnalyzer());
    this.mapperRegistry = mapperRegistry;

    this.dynamic = this.indexSettings.getValue(INDEX_MAPPER_DYNAMIC_SETTING);
    if (index().getName().equals(ScriptService.SCRIPT_INDEX)) {
      defaultMappingSource =
          "{"
              + "\"_default_\": {"
              + "\"properties\": {"
              + "\"script\": { \"enabled\": false },"
              + "\"template\": { \"enabled\": false }"
              + "}"
              + "}"
              + "}";
    } else {
      defaultMappingSource = "{\"_default_\":{}}";
    }

    if (logger.isTraceEnabled()) {
      logger.trace("using dynamic[{}], default mapping source[{}]", dynamic, defaultMappingSource);
    } else if (logger.isDebugEnabled()) {
      logger.debug("using dynamic[{}]", dynamic);
    }
  }
 public void testOverrideDefaultAnalyzer() throws IOException {
   Version version = VersionUtils.randomVersion(getRandom());
   Settings settings =
       Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
   AnalysisService analysisService =
       new AnalysisService(
           IndexSettingsModule.newIndexSettings("index", settings),
           Collections.singletonMap("default", analyzerProvider("default")),
           Collections.emptyMap(),
           Collections.emptyMap(),
           Collections.emptyMap());
   assertThat(
       analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
   assertThat(
       analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
   assertThat(
       analysisService.defaultSearchQuoteAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
 }
 public void testDefaultAnalyzers() throws IOException {
   Version version = VersionUtils.randomVersion(getRandom());
   Settings settings =
       Settings.builder()
           .put(IndexMetaData.SETTING_VERSION_CREATED, version)
           .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
           .build();
   IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings);
   AnalysisService analysisService =
       new AnalysisRegistry(null, new Environment(settings)).build(idxSettings);
   assertThat(
       analysisService.defaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
   assertThat(
       analysisService.defaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
   assertThat(
       analysisService.defaultSearchQuoteAnalyzer().analyzer(),
       instanceOf(StandardAnalyzer.class));
 }
  @Inject
  public MapperService(
      Index index,
      @IndexSettings Settings indexSettings,
      Environment environment,
      AnalysisService analysisService,
      PostingsFormatService postingsFormatService,
      DocValuesFormatService docValuesFormatService,
      SimilarityLookupService similarityLookupService) {
    super(index, indexSettings);
    this.analysisService = analysisService;
    this.documentParser =
        new DocumentMapperParser(
            index,
            indexSettings,
            analysisService,
            postingsFormatService,
            docValuesFormatService,
            similarityLookupService);
    this.searchAnalyzer = new SmartIndexNameSearchAnalyzer(analysisService.defaultSearchAnalyzer());
    this.searchQuoteAnalyzer =
        new SmartIndexNameSearchQuoteAnalyzer(analysisService.defaultSearchQuoteAnalyzer());

    this.dynamic = componentSettings.getAsBoolean("dynamic", true);
    String defaultMappingLocation = componentSettings.get("default_mapping_location");
    URL defaultMappingUrl;
    if (defaultMappingLocation == null) {
      try {
        defaultMappingUrl = environment.resolveConfig("default-mapping.json");
      } catch (FailedToResolveConfigException e) {
        // not there, default to the built in one
        defaultMappingUrl =
            indexSettings
                .getClassLoader()
                .getResource("org/elasticsearch/index/mapper/default-mapping.json");
        if (defaultMappingUrl == null) {
          defaultMappingUrl =
              MapperService.class
                  .getClassLoader()
                  .getResource("org/elasticsearch/index/mapper/default-mapping.json");
        }
      }
    } else {
      try {
        defaultMappingUrl = environment.resolveConfig(defaultMappingLocation);
      } catch (FailedToResolveConfigException e) {
        // not there, default to the built in one
        try {
          defaultMappingUrl = new File(defaultMappingLocation).toURI().toURL();
        } catch (MalformedURLException e1) {
          throw new FailedToResolveConfigException(
              "Failed to resolve dynamic mapping location [" + defaultMappingLocation + "]");
        }
      }
    }

    if (defaultMappingUrl == null) {
      logger.info(
          "failed to find default-mapping.json in the classpath, using the default template");
      defaultMappingSource = "{\n" + "    \"_default_\":{\n" + "    }\n" + "}";
    } else {
      try {
        defaultMappingSource =
            Streams.copyToString(
                new InputStreamReader(defaultMappingUrl.openStream(), Charsets.UTF_8));
      } catch (IOException e) {
        throw new MapperException(
            "Failed to load default mapping source from [" + defaultMappingLocation + "]", e);
      }
    }

    String percolatorMappingLocation = componentSettings.get("default_percolator_mapping_location");
    URL percolatorMappingUrl = null;
    if (percolatorMappingLocation != null) {
      try {
        percolatorMappingUrl = environment.resolveConfig(percolatorMappingLocation);
      } catch (FailedToResolveConfigException e) {
        // not there, default to the built in one
        try {
          percolatorMappingUrl = new File(percolatorMappingLocation).toURI().toURL();
        } catch (MalformedURLException e1) {
          throw new FailedToResolveConfigException(
              "Failed to resolve default percolator mapping location ["
                  + percolatorMappingLocation
                  + "]");
        }
      }
    }
    if (percolatorMappingUrl != null) {
      try {
        defaultPercolatorMappingSource =
            Streams.copyToString(
                new InputStreamReader(percolatorMappingUrl.openStream(), Charsets.UTF_8));
      } catch (IOException e) {
        throw new MapperException(
            "Failed to load default percolator mapping source from [" + percolatorMappingUrl + "]",
            e);
      }
    } else {
      defaultPercolatorMappingSource =
          "{\n"
              +
              // "    \"" + PercolatorService.TYPE_NAME + "\":{\n" +
              "    \""
              + "_default_"
              + "\":{\n"
              + "        \"_id\" : {\"index\": \"not_analyzed\"},"
              + "        \"properties\" : {\n"
              + "            \"query\" : {\n"
              + "                \"type\" : \"object\",\n"
              + "                \"enabled\" : false\n"
              + "            }\n"
              + "        }\n"
              + "    }\n"
              + "}";
    }

    if (logger.isDebugEnabled()) {
      logger.debug(
          "using dynamic[{}], default mapping: default_mapping_location[{}], loaded_from[{}], default percolator mapping: location[{}], loaded_from[{}]",
          dynamic,
          defaultMappingLocation,
          defaultMappingUrl,
          percolatorMappingLocation,
          percolatorMappingUrl);
    } else if (logger.isTraceEnabled()) {
      logger.trace(
          "using dynamic[{}], default mapping: default_mapping_location[{}], loaded_from[{}] and source[{}], default percolator mapping: location[{}], loaded_from[{}] and source[{}]",
          dynamic,
          defaultMappingLocation,
          defaultMappingUrl,
          defaultMappingSource,
          percolatorMappingLocation,
          percolatorMappingUrl,
          defaultPercolatorMappingSource);
    }
  }