Java Code Examples for org.elasticsearch.client.Requests

The following examples show how to use org.elasticsearch.client.Requests. These examples are extracted from open source projects. 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 Project: searchanalytics-bigdata   Source File: FlumeESSinkServiceTest.java    License: MIT License 6 votes vote down vote up
@Test
public void testProcessEvents() {
	int searchEventsCount = 101;
	List<Event> searchEvents = generateSearchAnalyticsDataService
			.getSearchEvents(searchEventsCount);

	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 2
Source Project: Flink-CEPplus   Source File: Elasticsearch6SinkExample.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onFailure(ActionRequest action, Throwable failure, int restStatusCode, RequestIndexer indexer) throws Throwable {
	if (action instanceof IndexRequest) {
		Map<String, Object> json = new HashMap<>();
		json.put("data", ((IndexRequest) action).source());

		indexer.add(
			Requests.indexRequest()
				.index(index)
				.type(type)
				.id(((IndexRequest) action).id())
				.source(json));
	} else {
		throw new IllegalStateException("unexpected");
	}
}
 
Example 3
Source Project: Flink-CEPplus   Source File: Elasticsearch6SinkExample.java    License: Apache License 2.0 6 votes vote down vote up
private static IndexRequest createIndexRequest(String element, ParameterTool parameterTool) {
	Map<String, Object> json = new HashMap<>();
	json.put("data", element);

	String index;
	String type;

	if (element.startsWith("message #15")) {
		index = ":intentional invalid index:";
		type = ":intentional invalid type:";
	} else {
		index = parameterTool.getRequired("index");
		type = parameterTool.getRequired("type");
	}

	return Requests.indexRequest()
		.index(index)
		.type(type)
		.id(element)
		.source(json);
}
 
Example 4
Source Project: streams   Source File: ElasticsearchParentChildUpdaterIT.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTestParentChildPersistUpdater() throws Exception {

  testConfiguration = new ComponentConfigurator<>(ElasticsearchWriterConfiguration.class).detectConfiguration( "ElasticsearchParentChildUpdaterIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertTrue(indicesExistsResponse.isExists());

  Reflections reflections = new Reflections(new ConfigurationBuilder()
      .setUrls(ClasspathHelper.forPackage("org.apache.streams.pojo.json"))
      .setScanners(new SubTypesScanner()));
  objectTypes = reflections.getSubTypesOf(ActivityObject.class);

  Path testdataDir = Paths.get("target/dependency/activitystreams-testdata");
  files = Files.list(testdataDir).collect(Collectors.toList());

}
 
Example 5
Source Project: streams   Source File: ElasticsearchPersistWriterIT.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTestPersistWriter() throws Exception {

  testConfiguration = new ComponentConfigurator<>(ElasticsearchWriterConfiguration.class).detectConfiguration("ElasticsearchPersistWriterIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  if(indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  }

}
 
Example 6
Source Project: flink-learning   Source File: Sink2ES6Main.java    License: Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    final ParameterTool parameterTool = ExecutionEnvUtil.createParameterTool(args);
    StreamExecutionEnvironment env = ExecutionEnvUtil.prepare(parameterTool);
    DataStreamSource<MetricEvent> data = KafkaConfigUtil.buildSource(env);

    List<HttpHost> esAddresses = ESSinkUtil.getEsAddresses(parameterTool.get(ELASTICSEARCH_HOSTS));
    int bulkSize = parameterTool.getInt(ELASTICSEARCH_BULK_FLUSH_MAX_ACTIONS, 40);
    int sinkParallelism = parameterTool.getInt(STREAM_SINK_PARALLELISM, 5);

    log.info("-----esAddresses = {}, parameterTool = {}, ", esAddresses, parameterTool);

    ESSinkUtil.addSink(esAddresses, bulkSize, sinkParallelism, data,
            (MetricEvent metric, RuntimeContext runtimeContext, RequestIndexer requestIndexer) -> {
                requestIndexer.add(Requests.indexRequest()
                        .index(ZHISHENG + "_" + metric.getName())
                        .type(ZHISHENG)
                        .source(GsonUtil.toJSONBytes(metric), XContentType.JSON));
            },
            parameterTool);
    env.execute("flink learning connectors es6");
}
 
Example 7
/**
   * Waits for all relocating shards to become active and the cluster has reached the given health status
   * using the cluster health API.
   */
  public ClusterHealthStatus waitForRelocation(ClusterHealthStatus status) {
    ClusterHealthRequest request = Requests.clusterHealthRequest().waitForRelocatingShards(0);
    if (status != null) {
      request.waitForStatus(status);
    }
    ClusterHealthResponse actionGet = adminClient.cluster()
        .health(request).actionGet();
    if (actionGet.isTimedOut()) {
//      logger.info("waitForRelocation timed out (status={}), cluster state:\n{}\n{}", status, adminClient.cluster().prepareState().get().getState().prettyPrint(), adminClient.cluster().preparePendingClusterTasks().get().prettyPrint());
      assertThat("timed out waiting for relocation", actionGet.isTimedOut(), equalTo(false));
    }
    if (status != null) {
      assertThat(actionGet.getStatus(), equalTo(status));
    }
    return actionGet.getStatus();
  }
 
Example 8
Source Project: flink   Source File: Elasticsearch7SinkExample.java    License: Apache License 2.0 6 votes vote down vote up
private static IndexRequest createIndexRequest(String element, ParameterTool parameterTool) {
	Map<String, Object> json = new HashMap<>();
	json.put("data", element);

	String index;
	String type;

	if (element.startsWith("message #15")) {
		index = ":intentional invalid index:";
		type = ":intentional invalid type:";
	} else {
		index = parameterTool.getRequired("index");
	}

	return Requests.indexRequest()
		.index(index)
		.id(element)
		.source(json);
}
 
Example 9
Source Project: flink   Source File: Elasticsearch6SinkExample.java    License: Apache License 2.0 6 votes vote down vote up
private static IndexRequest createIndexRequest(String element, ParameterTool parameterTool) {
	Map<String, Object> json = new HashMap<>();
	json.put("data", element);

	String index;
	String type;

	if (element.startsWith("message #15")) {
		index = ":intentional invalid index:";
		type = ":intentional invalid type:";
	} else {
		index = parameterTool.getRequired("index");
		type = parameterTool.getRequired("type");
	}

	return Requests.indexRequest()
		.index(index)
		.type(type)
		.id(element)
		.source(json);
}
 
Example 10
Source Project: blog-sample   Source File: IndexController.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 删除索引
 * @param index 索引名
 * @author jitwxs
 * @since 2018/10/9 15:08
 */
@DeleteMapping("/index/delete/{index}")
public ResultBean deleteIndex(@PathVariable String index) {
    if (StringUtils.isBlank(index)) {
        return ResultBean.error("参数错误,索引为空!");
    }
    try {
        // 删除索引
        DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(index);
        DeleteIndexResponse response = transportClient.admin().indices().delete(deleteIndexRequest).get();

        logger.info("删除索引结果:{}",response.isAcknowledged());
        return ResultBean.success("删除索引成功!");
    } catch (Exception e) {
        logger.error("删除索引失败!要删除的索引为{},异常为:",index,e.getMessage(),e);
        return ResultBean.error("删除索引失败!");
    }
}
 
Example 11
Source Project: elasticshell   Source File: ShellScope.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new <code>ShellScope</code> given the actual scope object
 * @param scope the actual scope object that depends on the engine in use
 */
ShellScope(Scope scope, ResourceRegistry resourceRegistry) {
    this.scope = scope;
    this.resourceRegistry = resourceRegistry;
    registerJavaClass(Requests.class);
    registerJavaClass(SearchSourceBuilder.class);
    registerJavaClass(QueryBuilders.class);
    registerJavaClass(FilterBuilders.class);
    registerJavaClass(SortBuilders.class);
    registerJavaClass(FacetBuilders.class);
    registerJavaClass(RescoreBuilder.class);
    registerJavaClass(SuggestBuilder.class);
    registerJavaClass(AliasAction.class);
    registerJavaClass(HttpParameters.class);
    registerJavaClass(AllocateAllocationCommand.class);
    registerJavaClass(CancelAllocationCommand.class);
    registerJavaClass(MoveAllocationCommand.class);
    registerJavaClass(ShardId.class);
}
 
Example 12
@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 13
Source Project: streams   Source File: TwitterUserstreamElasticsearchIT.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(TwitterUserstreamElasticsearchConfiguration.class).detectCustomConfiguration();

  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getElasticsearch()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getElasticsearch().getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();

  if(indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getElasticsearch().getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  };

  CreateIndexRequest createIndexRequest = Requests.createIndexRequest(testConfiguration.getElasticsearch().getIndex());
  CreateIndexResponse createIndexResponse = testClient.admin().indices().create(createIndexRequest).actionGet();
  assertTrue(createIndexResponse.isAcknowledged());

}
 
Example 14
Source Project: streams   Source File: ElasticsearchReindexIT.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(ElasticsearchReindexConfiguration.class).detectCustomConfiguration("ElasticsearchReindexIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getSource()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertThat(clusterHealthResponse.getStatus(), not(ClusterHealthStatus.RED));

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getSource().getIndexes().get(0));
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertThat(indicesExistsResponse.isExists(), is(true));

  SearchRequestBuilder countRequest = testClient
      .prepareSearch(testConfiguration.getSource().getIndexes().get(0))
      .setTypes(testConfiguration.getSource().getTypes().get(0));
  SearchResponse countResponse = countRequest.execute().actionGet();

  count = (int)countResponse.getHits().getTotalHits();

  assertThat(count, not(0));

}
 
Example 15
Source Project: crate   Source File: SQLTransportExecutor.java    License: Apache License 2.0 6 votes vote down vote up
private ClusterHealthStatus ensureState(ClusterHealthStatus state) {
    Client client = clientProvider.client();
    ClusterHealthResponse actionGet = client.admin().cluster().health(
        Requests.clusterHealthRequest()
            .waitForStatus(state)
            .waitForEvents(Priority.LANGUID).waitForNoRelocatingShards(false)
    ).actionGet();

    if (actionGet.isTimedOut()) {
        LOGGER.info("ensure state timed out, cluster state:\n{}\n{}",
            client.admin().cluster().prepareState().get().getState(),
            client.admin().cluster().preparePendingClusterTasks().get());
        assertThat("timed out waiting for state", actionGet.isTimedOut(), equalTo(false));
    }
    if (state == ClusterHealthStatus.YELLOW) {
        assertThat(actionGet.getStatus(), Matchers.anyOf(equalTo(state), equalTo(ClusterHealthStatus.GREEN)));
    } else {
        assertThat(actionGet.getStatus(), equalTo(state));
    }
    return actionGet.getStatus();
}
 
Example 16
private AsyncAction(ActionListener<NodePrometheusMetricsResponse> listener) {
    this.listener = listener;

    // Note: when using ClusterHealthRequest in Java, it pulls data at the shards level, according to ES source
    // code comment this is "so it is backward compatible with the transport client behaviour".
    // hence we are explicit about ClusterHealthRequest level and do not rely on defaults.
    // https://www.elastic.co/guide/en/elasticsearch/reference/6.4/cluster-health.html#request-params
    this.healthRequest = Requests.clusterHealthRequest().local(true);
    this.healthRequest.level(ClusterHealthRequest.Level.SHARDS);

    this.nodesStatsRequest = Requests.nodesStatsRequest("_local").clear().all();

    // Indices stats request is not "node-specific", it does not support any "_local" notion
    // it is broad-casted to all cluster nodes.
    this.indicesStatsRequest = isPrometheusIndices ? new IndicesStatsRequest() : null;

    // Cluster settings are get via ClusterStateRequest (see elasticsearch RestClusterGetSettingsAction for details)
    // We prefer to send it to master node (hence local=false; it should be set by default but we want to be sure).
    this.clusterStateRequest = isPrometheusClusterSettings ? Requests.clusterStateRequest()
            .clear().metadata(true).local(false) : null;
}
 
Example 17
public void testBadValues() throws IOException {
    Map<String, Object> map = new HashMap<>();
    XContentBuilder builder = XContentBuilder.builder(Requests.INDEX_CONTENT_TYPE.xContent());
    BytesReference bytes = BytesReference.bytes(builder.map(map));
    assertThat(expectThrows(IllegalArgumentException.class,
            () -> IndexFeatureStore.parse(StoredFeature.class, StoredFeature.TYPE, bytes))
            .getMessage(), equalTo("No StorableElement found."));

    builder = XContentBuilder.builder(Requests.INDEX_CONTENT_TYPE.xContent());
    map.put("featureset", LtrTestUtils.randomFeatureSet());
    BytesReference bytes2 = BytesReference.bytes(builder.map(map));
    assertThat(expectThrows(IllegalArgumentException.class,
            () -> IndexFeatureStore.parse(StoredFeature.class, StoredFeature.TYPE, bytes2))
            .getMessage(), equalTo("Expected an element of type [" + StoredFeature.TYPE + "] but" +
            " got [" + StoredFeatureSet.TYPE + "]."));
}
 
Example 18
Source Project: streams   Source File: TwitterHistoryElasticsearchIT.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(TwitterHistoryElasticsearchConfiguration.class).detectCustomConfiguration();

  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getElasticsearch()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getElasticsearch().getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();

  if(indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getElasticsearch().getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  };
}
 
Example 19
Source Project: elasticshell   Source File: JLineRhinoCompleterTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCompleteNativeJavaObject() {
    completer.getScope().registerJavaObject("ir", Context.javaToJS(Requests.indexRequest("index_name"), completer.getScope().get()));
    List<CharSequence> candidates = new ArrayList<CharSequence>();
    String input = "ir.ty";
    int output = completer.complete(input, input.length(), candidates);
    Assert.assertEquals(candidates.size(), 1);
    Assert.assertEquals(candidates.get(0), "type()");
    Assert.assertEquals(output, 3);

    candidates.clear();
    input = "ir.type('type_name').id";
    output = completer.complete(input, input.length(), candidates);
    Assert.assertEquals(candidates.size(), 1);
    Assert.assertEquals(candidates.get(0), "id()");
    Assert.assertEquals(output, 21);

    candidates.clear();
    input = "ir.type('type_name').id(\"id\").so";
    output = completer.complete(input, input.length(), candidates);
    Assert.assertEquals(candidates.size(), 2);
    Assert.assertTrue(candidates.contains("source()"));
    Assert.assertTrue(candidates.contains("sourceAsMap()"));
    Assert.assertEquals(output, 30);
}
 
Example 20
Source Project: baleen   Source File: LocationElasticsearch.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Create an index in Elasticsearch. If necessary, this function should check whether a new index
 * is required.
 *
 * @return true if a new index has been created, false otherwise
 */
public boolean createIndex() {
  if (!esResource
      .getClient()
      .admin()
      .indices()
      .exists(Requests.indicesExistsRequest(index))
      .actionGet()
      .isExists()) {
    esResource
        .getClient()
        .admin()
        .indices()
        .create(Requests.createIndexRequest(index))
        .actionGet();

    return true;
  }

  return false;
}
 
Example 21
Source Project: mt-flume   Source File: TestElasticSearchSink.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void shouldIndexOneEvent() throws Exception {
  Configurables.configure(fixture, new Context(parameters));
  Channel channel = bindAndStartChannel(fixture);

  Transaction tx = channel.getTransaction();
  tx.begin();
  Event event = EventBuilder.withBody("event #1 or 1".getBytes());
  channel.put(event);
  tx.commit();
  tx.close();

  fixture.process();
  fixture.stop();
  client.admin().indices()
      .refresh(Requests.refreshRequest(timestampedIndexName)).actionGet();

  assertMatchAllQuery(1, event);
  assertBodyQuery(1, event);
}
 
Example 22
Source Project: logstash   Source File: ElasticsearchContainer.java    License: Apache License 2.0 6 votes vote down vote up
public Client createClient() {
    final AtomicReference<Client> elasticsearchClient = new AtomicReference<>();
    await().atMost(30, TimeUnit.SECONDS).pollDelay(1, TimeUnit.SECONDS).until(() -> {
        Client c = new TransportClient(ImmutableSettings.settingsBuilder().put("cluster.name", elasticsearchClusterName).build()).addTransportAddress(new InetSocketTransportAddress(getIpAddress(), 9300));
        try {
            c.admin().cluster().health(Requests.clusterHealthRequest("_all")).actionGet();
        } catch (ElasticsearchException e) {
            c.close();
            return false;
        }
        elasticsearchClient.set(c);
        return true;
    });
    assertEquals(elasticsearchClusterName, elasticsearchClient.get().admin().cluster().health(Requests.clusterHealthRequest("_all")).actionGet().getClusterName());
    return elasticsearchClient.get();
}
 
Example 23
Source Project: flink   Source File: Elasticsearch6SinkExample.java    License: Apache License 2.0 6 votes vote down vote up
private static IndexRequest createIndexRequest(String element, ParameterTool parameterTool) {
	Map<String, Object> json = new HashMap<>();
	json.put("data", element);

	String index;
	String type;

	if (element.startsWith("message #15")) {
		index = ":intentional invalid index:";
		type = ":intentional invalid type:";
	} else {
		index = parameterTool.getRequired("index");
		type = parameterTool.getRequired("type");
	}

	return Requests.indexRequest()
		.index(index)
		.type(type)
		.id(element)
		.source(json);
}
 
Example 24
Source Project: crate   Source File: ESIntegTestCase.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Waits for all relocating shards to become active and the cluster has reached the given health status
 * using the cluster health API.
 */
public ClusterHealthStatus waitForRelocation(ClusterHealthStatus status) {
    ClusterHealthRequest request = Requests.clusterHealthRequest().waitForNoRelocatingShards(true);
    if (status != null) {
        request.waitForStatus(status);
    }
    ClusterHealthResponse actionGet = client().admin().cluster()
        .health(request).actionGet();
    if (actionGet.isTimedOut()) {
        logger.info("waitForRelocation timed out (status={}), cluster state:\n{}\n{}", status,
            client().admin().cluster().prepareState().get().getState(), client().admin().cluster().preparePendingClusterTasks().get());
        assertThat("timed out waiting for relocation", actionGet.isTimedOut(), equalTo(false));
    }
    if (status != null) {
        assertThat(actionGet.getStatus(), equalTo(status));
    }
    return actionGet.getStatus();
}
 
Example 25
Source Project: SpringBootLearn   Source File: ElasticsearchUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 创建索引以及映射mapping,并给索引某些字段指定iK分词,以后向该索引中查询时,就会用ik分词。
 * @Author lihaodong
 * @Description
 * @Date 20:19 2018/12/21
 * @Param [indexName, esType]
 * @return boolean
 **/
public static boolean createIndex(String indexName, String esType) {
    if (!isIndexExist(indexName)) {
        log.info("Index is not exits!");
    }
    //创建映射
    XContentBuilder mapping = null;
    try {
        mapping = XContentFactory.jsonBuilder()
                .startObject()
                .startObject("properties") //      .startObject("m_id").field("type","keyword").endObject()
                // title:字段名,  type:文本类型       analyzer :分词器类型
                .startObject("id").field("type", "text").field("analyzer", "standard").endObject()   //该字段添加的内容,查询时将会使用ik_smart分词
                .startObject("name").field("type", "text").field("analyzer", "standard").endObject()  //ik_smart  ik_max_word  standard
                .startObject("message").field("type", "text").field("analyzer", "standard").endObject()
                .startObject("price").field("type", "float").endObject()
                .startObject("creatDate").field("type", "date").endObject()
                .endObject()
                .endObject();
    } catch (IOException e) {
        log.error("执行建立失败:{}",e.getMessage());
    }
    //index:索引名   type:类型名
    PutMappingRequest putmap = Requests.putMappingRequest(indexName).type(esType).source(mapping);
    //创建索引
    client.admin().indices().prepareCreate(indexName).execute().actionGet();
    //为索引添加映射
    PutMappingResponse indexresponse = client.admin().indices().putMapping(putmap).actionGet();
    log.info("执行建立成功?" + indexresponse.isAcknowledged());
    return indexresponse.isAcknowledged();
}
 
Example 26
Source Project: Flink-CEPplus   Source File: ElasticsearchSinkBaseTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void process(String element, RuntimeContext ctx, RequestIndexer indexer) {
	Map<java.lang.String, Object> json = new HashMap<>();
	json.put("data", element);

	indexer.add(
		Requests.indexRequest()
			.index("index")
			.type("type")
			.id("id")
			.source(json)
	);
}
 
Example 27
Source Project: Flink-CEPplus   Source File: Elasticsearch5SinkExample.java    License: Apache License 2.0 5 votes vote down vote up
private static IndexRequest createIndexRequest(String element, ParameterTool parameterTool) {
	Map<String, Object> json = new HashMap<>();
	json.put("data", element);

	return Requests.indexRequest()
		.index(parameterTool.getRequired("index"))
		.type(parameterTool.getRequired("type"))
		.id(element)
		.source(json);
}
 
Example 28
Source Project: streams   Source File: ElasticsearchPersistUpdaterIT.java    License: Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void prepareTestPersistUpdater() throws Exception {

  testConfiguration = new ComponentConfigurator<>(ElasticsearchWriterConfiguration.class).detectConfiguration("ElasticsearchPersistUpdaterIT");
  testClient = ElasticsearchClientManager.getInstance(testConfiguration).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertTrue(indicesExistsResponse.isExists());

}
 
Example 29
Source Project: Flink-CEPplus   Source File: Elasticsearch1SinkExample.java    License: Apache License 2.0 5 votes vote down vote up
private static IndexRequest createIndexRequest(String element, ParameterTool parameterTool) {
	Map<String, Object> json = new HashMap<>();
	json.put("data", element);

	return Requests.indexRequest()
		.index(parameterTool.getRequired("index"))
		.type(parameterTool.getRequired("type"))
		.id(element)
		.source(json);
}
 
Example 30
Source Project: flink   Source File: ElasticsearchSinkBaseTest.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void process(String element, RuntimeContext ctx, RequestIndexer indexer) {
	Map<java.lang.String, Object> json = new HashMap<>();
	json.put("data", element);

	indexer.add(
		Requests.indexRequest()
			.index("index")
			.type("type")
			.id("id")
			.source(json)
	);
}