org.elasticsearch.client.Client Java Examples

The following examples show how to use org.elasticsearch.client.Client. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FullTextSearchLookupDescriptor.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 6 votes vote down vote up
@Builder
private FullTextSearchLookupDescriptor(
		@NonNull final Client elasticsearchClient,
		@NonNull final String modelTableName,
		@NonNull final String esIndexName,
		@NonNull final Set<String> esSearchFieldNames,
		@Nullable final ISqlLookupDescriptor sqlLookupDescriptor,
		@NonNull final LookupDataSource databaseLookup)
{
	this.elasticsearchClient = elasticsearchClient;

	this.modelTableName = modelTableName;

	this.esIndexName = esIndexName;
	esKeyColumnName = InterfaceWrapperHelper.getKeyColumnName(modelTableName);

	this.esSearchFieldNames = esSearchFieldNames.toArray(new String[esSearchFieldNames.size()]);

	this.sqlLookupDescriptor = sqlLookupDescriptor;
	this.databaseLookup = databaseLookup;
}
 
Example #2
Source File: RestGetRepositoriesAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] repositories = request.paramAsStringArray("repository", Strings.EMPTY_ARRAY);
    GetRepositoriesRequest getRepositoriesRequest = getRepositoryRequest(repositories);
    getRepositoriesRequest.masterNodeTimeout(request.paramAsTime("master_timeout", getRepositoriesRequest.masterNodeTimeout()));
    getRepositoriesRequest.local(request.paramAsBoolean("local", getRepositoriesRequest.local()));
    settingsFilter.addFilterSettingParams(request);
    client.admin().cluster().getRepositories(getRepositoriesRequest, new RestBuilderListener<GetRepositoriesResponse>(channel) {
        @Override
        public RestResponse buildResponse(GetRepositoriesResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            for (RepositoryMetaData repositoryMetaData : response.repositories()) {
                RepositoriesMetaData.toXContent(repositoryMetaData, builder, request);
            }
            builder.endObject();

            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example #3
Source File: CSVResultRestExecutor.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Override
public void execute(Client client, Map<String, String> params, QueryAction queryAction, RestChannel channel) throws Exception {
    Object queryResult = QueryActionElasticExecutor.executeAnyAction(client, queryAction);

    boolean flat = getBooleanOrDefault(params,"flat",false);
    String separator = ",";
    if(params.containsKey("separator")){
     separator = params.get("separator");
    }
    boolean includeScore = getBooleanOrDefault(params,"_score",false);
    boolean includeType = getBooleanOrDefault(params,"_type",false);
    boolean includeId = getBooleanOrDefault(params,"_id",false);
    boolean includeScrollId = getBooleanOrDefault(params,"_scroll_id",false);
    CSVResult result  = new CSVResultsExtractor(includeScore,includeType,includeId,includeScrollId,queryAction).extractResults(queryResult,flat,separator);
    String newLine = "\n";
    if(params.containsKey("newLine")){
     newLine = params.get("newLine");
    }
    boolean showHeader = getBooleanOrDefault(params, "showHeader", true);
    String csvString = buildString(separator, result, newLine, showHeader);
    BytesRestResponse bytesRestResponse = new BytesRestResponse(RestStatus.OK, csvString);
    channel.sendResponse(bytesRestResponse);
}
 
Example #4
Source File: RestForceMergeAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    ForceMergeRequest mergeRequest = new ForceMergeRequest(Strings.splitStringByCommaToArray(request.param("index")));
    mergeRequest.indicesOptions(IndicesOptions.fromRequest(request, mergeRequest.indicesOptions()));
    mergeRequest.maxNumSegments(request.paramAsInt("max_num_segments", mergeRequest.maxNumSegments()));
    mergeRequest.onlyExpungeDeletes(request.paramAsBoolean("only_expunge_deletes", mergeRequest.onlyExpungeDeletes()));
    mergeRequest.flush(request.paramAsBoolean("flush", mergeRequest.flush()));
    client.admin().indices().forceMerge(mergeRequest, new RestBuilderListener<ForceMergeResponse>(channel) {
        @Override
        public RestResponse buildResponse(ForceMergeResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            buildBroadcastShardsHeader(builder, request, response);
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example #5
Source File: RestClusterGetSettingsAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    ClusterStateRequest clusterStateRequest = Requests.clusterStateRequest()
            .routingTable(false)
            .nodes(false);
    clusterStateRequest.local(request.paramAsBoolean("local", clusterStateRequest.local()));
    client.admin().cluster().state(clusterStateRequest, new RestBuilderListener<ClusterStateResponse>(channel) {
        @Override
        public RestResponse buildResponse(ClusterStateResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();

            builder.startObject("persistent");
            response.getState().metaData().persistentSettings().toXContent(builder, request);
            builder.endObject();

            builder.startObject("transient");
            response.getState().metaData().transientSettings().toXContent(builder, request);
            builder.endObject();

            builder.endObject();

            return new BytesRestResponse(RestStatus.OK, builder);
        }
    });
}
 
Example #6
Source File: ElasticsearchSinkTestBase.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that the Elasticsearch sink works properly.
 */
public void runElasticsearchSinkTest() throws Exception {
	final String index = "elasticsearch-sink-test-index";

	final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

	DataStreamSource<Tuple2<Integer, String>> source = env.addSource(new SourceSinkDataTestKit.TestDataSourceFunction());

	source.addSink(createElasticsearchSinkForEmbeddedNode(
			1,
			CLUSTER_NAME,
			new SourceSinkDataTestKit.TestElasticsearchSinkFunction(index)));

	env.execute("Elasticsearch Sink Test");

	// verify the results
	Client client = embeddedNodeEnv.getClient();
	SourceSinkDataTestKit.verifyProducedSinkData(client, index);

	client.close();
}
 
Example #7
Source File: ESClient.java    From Gather-Platform with GNU General Public License v3.0 6 votes vote down vote up
public Client getClient() {
    if (!staticValue.isNeedEs()) {
        LOG.info("已在配置文件中声明不需要ES,如需要ES,请在配置文件中进行配置");
        return null;
    }
    if (client != null) return client;
    try {
        LOG.info("正在初始化ElasticSearch客户端," + staticValue.getEsHost());
        Settings settings = Settings.builder()
                .put("cluster.name", staticValue.getEsClusterName()).build();
        client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(staticValue.getEsHost()), 9300));
        final ClusterHealthResponse healthResponse = client.admin().cluster().prepareHealth()
                .setTimeout(TimeValue.timeValueMinutes(1)).execute().actionGet();
        if (healthResponse.isTimedOut()) {
            LOG.error("ES客户端初始化失败");
        } else {
            LOG.info("ES客户端初始化成功");
        }
    } catch (IOException e) {
        LOG.fatal("构建ElasticSearch客户端失败!");
    }
    return client;
}
 
Example #8
Source File: CompleteSetupIntegrationTest.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
private void FlumeESSinkAndTestData(List<Event> searchEvents)
		throws EventDeliveryException, IOException, FileNotFoundException {
	flumeESSinkService.processEvents(searchEvents);

	Client client = searchClientService.getClient();
	client.admin().indices().refresh(Requests.refreshRequest()).actionGet();

	String indexName = "recentlyviewed" + '-'
			+ ElasticSearchIndexRequestBuilderFactory.df.format(new Date());
	long totalCount = client.prepareCount(indexName).get().getCount();
	System.out.println("Search total count is: " + totalCount);

	SearchHits hits = client.prepareSearch(indexName).get().getHits();
	System.out.println("Total hits: " + hits.getTotalHits());
	for (SearchHit searchHit : hits) {
		System.out.println(searchHit.getSource());
	}
}
 
Example #9
Source File: RestForceMergeAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Inject
public RestForceMergeAction(Settings settings, RestController controller, Client client) {
    super(settings, controller, client);
    controller.registerHandler(POST, "/_forcemerge", this);
    controller.registerHandler(POST, "/{index}/_forcemerge", this);

    controller.registerHandler(GET, "/_forcemerge", this);
    controller.registerHandler(GET, "/{index}/_forcemerge", this);

    // TODO: Remove for 3.0
    controller.registerHandler(POST, "/_optimize", this);
    controller.registerHandler(POST, "/{index}/_optimize", this);

    controller.registerHandler(GET, "/_optimize", this);
    controller.registerHandler(GET, "/{index}/_optimize", this);
}
 
Example #10
Source File: EsEventPersistence.java    From logsniffer with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Event getEvent(final long snifferId, final String eventId) {
	return clientTpl.executeWithClient(new ClientCallback<Event>() {
		@Override
		public Event execute(final Client client) {
			try {
				final SearchResponse r = client.prepareSearch(indexNamingStrategy.getRetrievalNames(snifferId))
						.setIndicesOptions(IndicesOptions.lenientExpandOpen())
						.setQuery(QueryBuilders.idsQuery().ids(eventId)).execute().get();
				if (r != null && r.getHits().hits().length > 0) {
					final SearchHit hit = r.getHits().hits()[0];
					final Event event = jsonMapper.readValue(hit.getSourceAsString(), Event.class);
					event.setId(hit.getId());
					return event;
				} else {
					return null;
				}
			} catch (final Exception e) {
				throw new DataAccessException("Failed to load for sniffer=" + snifferId + " the event: " + eventId,
						e);
			}
		}
	});
}
 
Example #11
Source File: TorrentDaoImpl.java    From Dodder with MIT License 6 votes vote down vote up
@Override
public void index(Torrent torrent) throws IOException {
    Client client = elasticsearchTemplate.getClient();
    XContentBuilder source = jsonBuilder()
            .startObject()
            .field("fileName", torrent.getFileName())
            .field("fileType", torrent.getFileType())
            .field("fileSize", torrent.getFileSize())
            .field("createDate", torrent.getCreateDate())
            .endObject();

    IndexRequest indexRequest = new IndexRequest("dodder", "torrent", torrent.getInfoHash())
            .source(source);
    UpdateRequest updateRequest = new UpdateRequest("dodder", "torrent", torrent.getInfoHash())
            .doc(indexRequest)
            .docAsUpsert(true);
    client.update(updateRequest);
}
 
Example #12
Source File: ElasticsearchResource.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private Client getRemoteClient() {
    if (remoteClient == null) {
        Settings settings = Settings.builder()
            .put("cluster.name", System.getProperty("REMOTE_ES_CLUSTER_NAME", "elasticsearch"))
            .build();
        TransportAddress[] transportAddresses = Arrays.stream(getRemoteEsAddresses().split(","))
            .map(address -> {
                String[] parts = address.split(":");
                try {
                    InetAddress inetAddress = InetAddress.getByName(parts[0]);
                    int port = parts.length > 1 ? Integer.parseInt(parts[1]) : 9300;
                    return new InetSocketTransportAddress(inetAddress, port);
                } catch (Exception ex) {
                    throw new VertexiumException("cannot find host: " + address, ex);
                }
            })
            .toArray(TransportAddress[]::new);
        remoteClient = new PreBuiltTransportClient(settings)
            .addTransportAddresses(transportAddresses);
    }
    return remoteClient;
}
 
Example #13
Source File: RestListTasksAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    boolean detailed = request.paramAsBoolean("detailed", false);
    String[] nodesIds = Strings.splitStringByCommaToArray(request.param("node_id"));
    TaskId taskId = new TaskId(request.param("taskId"));
    String[] actions = Strings.splitStringByCommaToArray(request.param("actions"));
    TaskId parentTaskId = new TaskId(request.param("parent_task_id"));
    boolean waitForCompletion = request.paramAsBoolean("wait_for_completion", false);
    TimeValue timeout = request.paramAsTime("timeout", null);

    ListTasksRequest listTasksRequest = new ListTasksRequest();
    listTasksRequest.setTaskId(taskId);
    listTasksRequest.setNodesIds(nodesIds);
    listTasksRequest.setDetailed(detailed);
    listTasksRequest.setActions(actions);
    listTasksRequest.setParentTaskId(parentTaskId);
    listTasksRequest.setWaitForCompletion(waitForCompletion);
    listTasksRequest.setTimeout(timeout);
    client.admin().cluster().listTasks(listTasksRequest, new RestToXContentListener<ListTasksResponse>(channel));
}
 
Example #14
Source File: RestExportActionTest.java    From elasticsearch-inout-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * If _ttl is enabled in the mapping, the _ttl field delivers the time stamp
 * when the object is expired.
 */
@Test
public void testTTLEnabled() {
    esSetup.execute(deleteAll(), createIndex("ttlenabled").withSettings(
            fromClassPath("essetup/settings/test_a.json")).withMapping("d",
                    "{\"d\": {\"_ttl\": {\"enabled\": true, \"default\": \"1d\"}}}"));
    Client client = esSetup.client();
    client.prepareIndex("ttlenabled", "d", "1").setSource("field1", "value1").execute().actionGet();
    client.admin().indices().prepareRefresh().execute().actionGet();

    Date now = new Date();
    ExportResponse response = executeExportRequest(
            "{\"output_cmd\": \"cat\", \"fields\": [\"_id\", \"_ttl\"]}");
    List<Map<String, Object>> infos = getExports(response);
    String stdout = infos.get(1).get("stdout").toString();
    assertTrue(stdout.startsWith("{\"_id\":\"1\",\"_ttl\":"));
    String lsplit  = stdout.substring(18);
    long ttl = Long.valueOf(lsplit.substring(0, lsplit.length() - 2));
    long diff = ttl - now.getTime();
    assertTrue(diff < 86400000 && diff > 86390000);
}
 
Example #15
Source File: ReadOnlyNodeIntegrationTest.java    From crate with Apache License 2.0 6 votes vote down vote up
public ReadOnlyNodeIntegrationTest() {
    super(new SQLTransportExecutor(
        new SQLTransportExecutor.ClientProvider() {
            @Override
            public Client client() {
                // make sure we use the read-only client
                return internalCluster().client(internalCluster().getNodeNames()[1]);
            }

            @Override
            public String pgUrl() {
                return null;
            }

            @Override
            public SQLOperations sqlOperations() {
                return internalCluster().getInstance(SQLOperations.class, internalCluster().getNodeNames()[1]);
            }
        }
    ));
}
 
Example #16
Source File: TestBackupRestore.java    From Raigad with Apache License 2.0 5 votes vote down vote up
@Mock
public RestoreSnapshotResponse getRestoreSnapshotResponse(
        Client esTransportClient, String commaSeparatedIndices, String restoreRepositoryName, String snapshotN) {
    snapshotN = snapshotBackupManager.getSnapshotName("_all", false);
    return client0.admin().cluster().prepareRestoreSnapshot(repositoryName, snapshotN)
            .setIndices("test-idx-*")
            .setWaitForCompletion(true)
            .execute()
            .actionGet();
}
 
Example #17
Source File: ESActionFactory.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private static QueryAction handleSelect(Client client, Select select) {
    if (select.isAgg) {
        return new AggregationQueryAction(client, select);
    } else {
        return new DefaultQueryAction(client, select);
    }
}
 
Example #18
Source File: ACLDocumentManager.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public BulkRequest buildRequest(Client client, BulkRequestBuilder builder, Collection<SearchGuardACLDocument> docs) throws IOException{
    for (SearchGuardACLDocument doc : docs) {
        logContent("Expired doc {} to be: {}", doc.getType(), doc);
        Map<String, Object> content = new HashMap<>();
        content.put(doc.getType(), new BytesArray(XContentHelper.toString(doc)));
        IndexRequestBuilder indexBuilder = client
                .prepareIndex(searchGuardIndex, doc.getType(), SEARCHGUARD_CONFIG_ID)
                .setOpType(OpType.INDEX)
                .setVersion(doc.getVersion())
                .setSource(content);
        builder.add(indexBuilder.request());
    }
    return builder.request();
}
 
Example #19
Source File: SysPlantBarOrLineStatisticsChartByElasticsearchService.java    From danyuan-application with Apache License 2.0 5 votes vote down vote up
/**
 * 方法名: buildBarOrLineType1
 * 功 能: TODO(这里用一句话描述这个方法的作用)
 * 参 数: @param queryBuilder
 * 参 数: @param type1
 * 参 数: @param info
 * 参 数: @param map
 * 返 回: void
 * 作 者 : Administrator
 * @throws
 */
private void buildBarOrLineType1(QueryBuilder queryBuilder, String type1, SysDbmsChartDimension info, Map<String, Object> map) {
	Client client = elasticsearchTemplate.getClient();
	SearchRequestBuilder requestBuilder = client.prepareSearch(ELASTICSEARCH_INDEX_NAME).setTypes(ELASTICSEARCH_INDEX_TYPE);

	TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms(type1 + "_count").field(type1).size(0);

	requestBuilder.setQuery(queryBuilder).addAggregation(termsAggregationBuilder);
	SearchResponse response = requestBuilder.execute().actionGet();

	Terms aggregation = response.getAggregations().get(type1 + "_count");
	List<Map<String, Object>> series_data = new ArrayList<>();
	List<String> legend_data = new ArrayList<>();
	legend_data.add("数量");
	Map<String, Object> sdata = new HashMap<>();
	sdata.put("type", "tbar".equals(info.getChartType()) ? "bar" : info.getChartType());
	sdata.put("name", "数量");
	List<Long> series_data_data = new ArrayList<>();
	List<String> xAxis_data = new ArrayList<>();
	for (Terms.Bucket bucket : aggregation.getBuckets()) {
		if (bucket.getKey() == null || "".equals(bucket.getKey().toString())) {
			continue;
		}
		legend_data.add(bucket.getKey().toString());
		series_data_data.add(bucket.getDocCount());
		xAxis_data.add(bucket.getKey().toString());

	}

	sdata.put("data", series_data_data);
	series_data.add(sdata);
	map.put("series_data", series_data);
	map.put("xAxis_data", xAxis_data);
	map.put("legend_data", legend_data);
	map.put("chartType", info.getChartType());

}
 
Example #20
Source File: ReadOnlyNodeIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
private SQLResponse executeWrite(String stmt, Object[] args) {
    if (writeExecutor == null) {
        writeExecutor = new SQLTransportExecutor(
            new SQLTransportExecutor.ClientProvider() {
                @Override
                public Client client() {
                    // make sure we use NOT the read-only client
                    return internalCluster().client(internalCluster().getNodeNames()[0]);
                }

                @Nullable
                @Override
                public String pgUrl() {
                    return null;
                }

                @Override
                public SQLOperations sqlOperations() {
                    // make sure we use NOT the read-only operations
                    return internalCluster().getInstance(SQLOperations.class, internalCluster().getNodeNames()[0]);
                }
            }
        );
    }
    response = writeExecutor.exec(stmt, args);
    return response;
}
 
Example #21
Source File: SearchClientServiceMockImpl.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
private void closeAllNodes() {
	for (final Client client : clients.values()) {
		client.close();
	}
	clients.clear();
	for (final Node node : nodes.values()) {
		node.close();
	}
	nodes.clear();
}
 
Example #22
Source File: ReferenceService.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void doStart() {
    // get the client from the injector
    Client client = injector.getInstance(Client.class);
    // copy the client to the mapper type parser
    ReferenceMapperTypeParser referenceMapperTypeParser = injector.getInstance(ReferenceMapperTypeParser.class);
    referenceMapperTypeParser.setClient(client);
}
 
Example #23
Source File: DynamicSynonymPlugin.java    From elasticsearch-analysis-dynamic-synonym with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<Object> createComponents(Client client,
                                           ClusterService clusterService,
                                           ThreadPool threadPool,
                                           ResourceWatcherService resourceWatcherService,
                                           ScriptService scriptService,
                                           NamedXContentRegistry xContentRegistry) {
    Collection<Object> components = new ArrayList<>();
    components.add(pluginComponent);
    return components;
}
 
Example #24
Source File: AbstractElasticsearch5TransportClientProcessor.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
/**
 * Dispose of ElasticSearch client
 */
public void closeClient() {
    Client client = esClient.get();
    if (client != null) {
        getLogger().info("Closing ElasticSearch Client");
        esClient.set(null);
        client.close();
    }
}
 
Example #25
Source File: ElasticSearchRepositoryFactoryAutoConfiguration.java    From elastic-crud with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(DatabaseRepositoryFactory.class)
@ConditionalOnBean(value={Client.class, JsonSerializationFactory.class, DatabaseScrollingFactory.class})
DatabaseRepositoryFactory databaseRepositoryFactory(
  final Client client,
  final JsonSerializationFactory serialization,
  final DatabaseScrollingFactory factory) {
  return new ElasticSearchRepositoryFactory(serialization, client, factory);
}
 
Example #26
Source File: TestElasticsearchIndexManager.java    From Raigad with Apache License 2.0 5 votes vote down vote up
@Test
public void testRunIndexManagement() throws Exception {
    String serializedIndexMetadata = "[{\"retentionType\": \"yearly\", \"retentionPeriod\": 3, \"indexName\": \"nf_errors_log\"}]";
    when(config.getIndexMetadata()).thenReturn(serializedIndexMetadata);

    Map<String, IndexStats> indexStats = new HashMap<>();
    indexStats.put("nf_errors_log2018", new IndexStats("nf_errors_log2018", new ShardStats[]{}));
    indexStats.put("nf_errors_log2017", new IndexStats("nf_errors_log2017", new ShardStats[]{}));
    indexStats.put("nf_errors_log2016", new IndexStats("nf_errors_log2016", new ShardStats[]{}));
    indexStats.put("nf_errors_log2015", new IndexStats("nf_errors_log2015", new ShardStats[]{}));
    indexStats.put("nf_errors_log2014", new IndexStats("nf_errors_log2014", new ShardStats[]{}));
    indexStats.put("nf_errors_log2013", new IndexStats("nf_errors_log2013", new ShardStats[]{}));
    indexStats.put("nf_errors_log2012", new IndexStats("nf_errors_log2012", new ShardStats[]{}));

    IndicesStatsResponse indicesStatsResponse = mock(IndicesStatsResponse.class);
    when(indicesStatsResponse.getIndices()).thenReturn(indexStats);

    doReturn(indicesStatsResponse).when(elasticsearchIndexManager).getIndicesStatsResponse(elasticsearchClient);

    elasticsearchIndexManager.runIndexManagement();

    verify(elasticsearchIndexManager, times(1)).checkIndexRetention(any(Client.class), anySet(), any(IndexMetadata.class), any(DateTime.class));

    verify(elasticsearchIndexManager, times(1)).deleteIndices(any(Client.class), eq("nf_errors_log2012"), eq(AUTO_CREATE_INDEX_TIMEOUT));
    verify(elasticsearchIndexManager, times(1)).deleteIndices(any(Client.class), eq("nf_errors_log2013"), eq(AUTO_CREATE_INDEX_TIMEOUT));

    verify(elasticsearchIndexManager, times(0)).preCreateIndex(any(Client.class), any(IndexMetadata.class), any(DateTime.class));
}
 
Example #27
Source File: TestPutElasticsearch.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
public void createElasticsearchClient(ProcessContext context) throws ProcessException {
    final Client mockClient = mock(Client.class);
    BulkRequestBuilder bulkRequestBuilder = spy(new BulkRequestBuilder(mockClient, BulkAction.INSTANCE));
    if (exceptionToThrow != null) {
        doThrow(exceptionToThrow).when(bulkRequestBuilder).execute();
    } else {
        doReturn(new MockBulkRequestBuilderExecutor(responseHasFailures)).when(bulkRequestBuilder).execute();
    }
    when(mockClient.prepareBulk()).thenReturn(bulkRequestBuilder);

    when(mockClient.prepareIndex(anyString(), anyString(), anyString())).thenAnswer(new Answer<IndexRequestBuilder>() {
        @Override
        public IndexRequestBuilder answer(InvocationOnMock invocationOnMock) throws Throwable {
            Object[] args = invocationOnMock.getArguments();
            String arg1 = (String) args[0];
            if (arg1.isEmpty()) {
                throw new NoNodeAvailableException("Needs index");
            }
            String arg2 = (String) args[1];
            if (arg2.isEmpty()) {
                throw new NoNodeAvailableException("Needs doc type");
            } else {
                IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(mockClient, IndexAction.INSTANCE);
                return indexRequestBuilder;
            }
        }
    });

    esClient.set(mockClient);
}
 
Example #28
Source File: AuditLogImpl.java    From deprecated-security-advanced-modules with Apache License 2.0 5 votes vote down vote up
public AuditLogImpl(final Settings settings, final Path configPath, Client clientProvider, ThreadPool threadPool,
					final IndexNameExpressionResolver resolver, final ClusterService clusterService) {
	super(settings, threadPool, resolver, clusterService);

	this.messageRouter = new AuditMessageRouter(settings, clientProvider, threadPool, configPath);
	this.enabled = messageRouter.isEnabled();

	log.info("Message routing enabled: {}", this.enabled);

	final SecurityManager sm = System.getSecurityManager();

	if (sm != null) {
		log.debug("Security Manager present");
		sm.checkPermission(new SpecialPermission());
	}

	AccessController.doPrivileged(new PrivilegedAction<Object>() {
		@Override
		public Object run() {
			Runtime.getRuntime().addShutdownHook(new Thread() {

				@Override
				public void run() {
					try {
						close();
					} catch (final IOException e) {
						log.warn("Exception while shutting down message router", e);
					}
				}
			});
			log.debug("Shutdown Hook registered");
			return null;
		}
	});

}
 
Example #29
Source File: BulkProcessorFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public BatchEmitter createInstance(int batchSize, int deliveryInterval, ClientObjectFactory clientObjectFactory, FailoverPolicy failoverPolicy) {

    Function<BulkRequest, Boolean> failureHandler = clientObjectFactory.createFailureHandler(failoverPolicy);

    BulkProcessor.Listener listener = new BulkExecutionListener(failureHandler);

    BulkProcessor.Builder builder = BulkProcessor.builder((Client) clientObjectFactory.createClient(), listener)
            .setBulkActions(batchSize)
            .setFlushInterval(TimeValue.timeValueMillis(deliveryInterval));
    return new BulkProcessorDelegate(builder.build());
}
 
Example #30
Source File: CrateRestMainAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, Client client) throws IOException {
    RestStatus status = RestStatus.OK;
    if (clusterService.state().blocks().hasGlobalBlock(RestStatus.SERVICE_UNAVAILABLE)) {
        status = RestStatus.SERVICE_UNAVAILABLE;
    }
    if (request.method() == RestRequest.Method.HEAD) {
        channel.sendResponse(new BytesRestResponse(status));
        return;
    }
    XContentBuilder builder = channel.newBuilder();
    builder.prettyPrint().lfAtEnd();
    builder.startObject();
    builder.field("ok", status.equals(RestStatus.OK));
    builder.field("status", status.getStatus());
    if (settings.get("name") != null) {
        builder.field("name", settings.get("name"));
    }
    builder.field("cluster_name", clusterName.value());
    builder.startObject("version")
            .field("number", version.number())
            .field("build_hash", Build.CURRENT.hashShort())
            .field("build_timestamp", Build.CURRENT.timestamp())
            .field("build_snapshot", version.snapshot)
            .field("baidu_version", Build.CURRENT.baiduVersion())
            .field("es_version", version.esVersion)
                    // We use the lucene version from lucene constants since
                    // this includes bugfix release version as well and is already in
                    // the right format. We can also be sure that the format is maitained
                    // since this is also recorded in lucene segments and has BW compat
            .field("lucene_version", org.apache.lucene.util.Version.LATEST.toString())
            .endObject();
    builder.endObject();
    channel.sendResponse(new BytesRestResponse(status, builder));
}