public void testDefaultAnalyzers() throws IOException {
   Version version = VersionUtils.randomVersion(random());
   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);
   IndexAnalyzers indexAnalyzers =
       new AnalysisRegistry(
               new Environment(settings),
               emptyMap(),
               emptyMap(),
               emptyMap(),
               emptyMap(),
               emptyMap())
           .build(idxSettings);
   assertThat(
       indexAnalyzers.getDefaultIndexAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
   assertThat(
       indexAnalyzers.getDefaultSearchAnalyzer().analyzer(), instanceOf(StandardAnalyzer.class));
   assertThat(
       indexAnalyzers.getDefaultSearchQuoteAnalyzer().analyzer(),
       instanceOf(StandardAnalyzer.class));
 }
  public void testNoTypeOrTokenizerErrorMessage() throws IOException {
    Version version = VersionUtils.randomVersion(random());
    Settings settings =
        Settings.builder()
            .put(IndexMetaData.SETTING_VERSION_CREATED, version)
            .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
            .putArray(
                "index.analysis.analyzer.test_analyzer.filter",
                new String[] {"lowercase", "stop", "shingle"})
            .putArray(
                "index.analysis.analyzer.test_analyzer.char_filter", new String[] {"html_strip"})
            .build();
    IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings);

    IllegalArgumentException e =
        expectThrows(
            IllegalArgumentException.class,
            () ->
                new AnalysisRegistry(
                        new Environment(settings),
                        emptyMap(),
                        emptyMap(),
                        emptyMap(),
                        emptyMap(),
                        emptyMap())
                    .build(idxSettings));
    assertThat(
        e.getMessage(),
        equalTo("analyzer [test_analyzer] must specify either an analyzer type, or a tokenizer"));
  }
 @SuppressWarnings("unchecked")
 private IngestProxyActionFilter buildFilter(int ingestNodes, int totalNodes) {
   ClusterState.Builder clusterState = new ClusterState.Builder(new ClusterName("_name"));
   DiscoveryNodes.Builder builder = new DiscoveryNodes.Builder();
   DiscoveryNode localNode = null;
   for (int i = 0; i < totalNodes; i++) {
     String nodeId = "node" + i;
     Map<String, String> attributes = new HashMap<>();
     if (i >= ingestNodes) {
       attributes.put("ingest", "false");
     } else if (randomBoolean()) {
       attributes.put("ingest", "true");
     }
     DiscoveryNode node =
         new DiscoveryNode(
             nodeId,
             nodeId,
             DummyTransportAddress.INSTANCE,
             attributes,
             VersionUtils.randomVersion(random()));
     builder.put(node);
     if (i == totalNodes - 1) {
       localNode = node;
     }
   }
   clusterState.nodes(builder);
   ClusterService clusterService = mock(ClusterService.class);
   when(clusterService.localNode()).thenReturn(localNode);
   when(clusterService.state()).thenReturn(clusterState.build());
   transportService = mock(TransportService.class);
   return new IngestProxyActionFilter(clusterService, transportService);
 }
 private <T extends Throwable> T serialize(T exception) throws IOException {
   ElasticsearchAssertions.assertVersionSerializable(
       VersionUtils.randomVersion(random()), exception);
   BytesStreamOutput out = new BytesStreamOutput();
   out.writeThrowable(exception);
   StreamInput in = StreamInput.wrap(out.bytes());
   return in.readThrowable();
 }
 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 testOverrideDefaultAnalyzer() throws IOException {
   Version version = VersionUtils.randomVersion(random());
   Settings settings =
       Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
   IndexAnalyzers indexAnalyzers =
       registry.build(
           IndexSettingsModule.newIndexSettings("index", settings),
           singletonMap("default", analyzerProvider("default")),
           emptyMap(),
           emptyMap(),
           emptyMap(),
           emptyMap());
   assertThat(
       indexAnalyzers.getDefaultIndexAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
   assertThat(
       indexAnalyzers.getDefaultSearchAnalyzer().analyzer(), instanceOf(EnglishAnalyzer.class));
   assertThat(
       indexAnalyzers.getDefaultSearchQuoteAnalyzer().analyzer(),
       instanceOf(EnglishAnalyzer.class));
 }
  public void testInitialSearchParamsFields() {
    SearchRequest searchRequest = new SearchRequest().source(new SearchSourceBuilder());

    // Test request without any fields
    Version remoteVersion = VersionUtils.randomVersion(random());
    assertThat(
        initialSearchParams(searchRequest, remoteVersion),
        not(either(hasKey("stored_fields")).or(hasKey("fields"))));

    // Setup some fields for the next two tests
    searchRequest.source().storedField("_source").storedField("_id");

    // Test stored_fields for versions that support it
    remoteVersion = VersionUtils.randomVersionBetween(random(), Version.V_5_0_0_alpha4, null);
    assertThat(
        initialSearchParams(searchRequest, remoteVersion),
        hasEntry("stored_fields", "_source,_id"));

    // Test fields for versions that support it
    remoteVersion = VersionUtils.randomVersionBetween(random(), null, Version.V_5_0_0_alpha3);
    assertThat(
        initialSearchParams(searchRequest, remoteVersion), hasEntry("fields", "_source,_id"));
  }