org.elasticsearch.client.Requests Java Examples

The following examples show how to use org.elasticsearch.client.Requests. 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: SQLTransportExecutor.java    From crate with 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 #2
Source File: Elasticsearch6SinkExample.java    From flink with 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 #3
Source File: Elasticsearch7SinkExample.java    From flink with 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 #4
Source File: Sink2ES6Main.java    From flink-learning with 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 #5
Source File: ElasticsearchPersistWriterIT.java    From streams with 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 File: ElasticsearchParentChildUpdaterIT.java    From streams with 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 #7
Source File: Elasticsearch6SinkExample.java    From Flink-CEPplus with 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 #8
Source File: Elasticsearch6SinkExample.java    From Flink-CEPplus with 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 #9
Source File: ESIntegTestCase.java    From crate with 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 #10
Source File: AbstractElasticSearchTest.java    From camunda-bpm-elasticsearch with 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().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 #11
Source File: Elasticsearch6SinkExample.java    From flink with 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 #12
Source File: ElasticsearchContainer.java    From logstash with 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 #13
Source File: IndexController.java    From blog-sample with 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 #14
Source File: FlumeESSinkServiceTest.java    From searchanalytics-bigdata with 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 #15
Source File: TransportNodePrometheusMetricsAction.java    From elasticsearch-prometheus-exporter with Apache License 2.0 6 votes vote down vote up
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 #16
Source File: ElasticsearchReindexIT.java    From streams with 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 #17
Source File: TwitterUserstreamElasticsearchIT.java    From streams with 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 #18
Source File: IndexFeatureStoreTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 6 votes vote down vote up
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 #19
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 #20
Source File: TwitterHistoryElasticsearchIT.java    From streams with 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 #21
Source File: JLineRhinoCompleterTest.java    From elasticshell with 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 #22
Source File: LocationElasticsearch.java    From baleen with 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 #23
Source File: ShellScope.java    From elasticshell with 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 #24
Source File: TestElasticSearchSink.java    From mt-flume with 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 #25
Source File: ElasticSearch.java    From javabase with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
    Map<String, String> map = new HashMap();
    //基础名称
    map.put("cluster.name", "my-application-A");
    Settings.Builder settings = Settings.builder().put(map);
    try {
        transportClient = TransportClient.builder().settings(settings).build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));

        IndicesAdminClient indicesAdminClient = transportClient.admin().indices();
        //查看索引是否存在,不存在就创建索引
        if(!checkExistsIndex(indicesAdminClient,INDEX_NAME)){
            indicesAdminClient.prepareCreate(INDEX_NAME).setSettings().execute().actionGet();
        }
        //查询mapping是否存在,已存在就不创建了
        GetMappingsResponse getMappingsResponse = indicesAdminClient.getMappings(new GetMappingsRequest().indices(INDEX_NAME)).actionGet();
        ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> indexToMappings = getMappingsResponse.getMappings();
       if(indexToMappings.get(INDEX_NAME).get(TIEABA_CONTENT_TYPE)==null) {
           //创建zk分词mapping
           PutMappingRequest mapping = Requests.putMappingRequest(INDEX_NAME).type(TIEABA_CONTENT_TYPE).source(createIKMapping(TIEABA_CONTENT_TYPE, TIEABA_CONTENT_FIELD).string());
           mapping.updateAllTypes(true);
           indicesAdminClient.putMapping(mapping).actionGet();
       }
    } catch (Exception e) {
       log.error("初始化 elasticsearch cliet error"+e.getLocalizedMessage());
    }
}
 
Example #26
Source File: IndexFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
/**
 * Generate the source doc ready to be indexed in the store
 *
 * @param elt the storable element to build the source document for
 * @return the source-doc to be indexed by the store
 * @throws IOException in case of failures
 */
public static XContentBuilder toSource(StorableElement elt) throws IOException {
    XContentBuilder source = XContentFactory.contentBuilder(Requests.INDEX_CONTENT_TYPE);
    source.startObject();
    source.field("name", elt.name());
    source.field("type", elt.type());
    source.field(elt.type(), elt);
    source.endObject();
    return source;
}
 
Example #27
Source File: IngestRequestTest.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Test(expected = ActionRequestValidationException.class)
public void testIngest1() {
    Client client = client("1");
    IngestRequestBuilder builder = new IngestRequestBuilder(client, IngestAction.INSTANCE)
            .add(Requests.indexRequest());
    client.execute(IngestAction.INSTANCE, builder.request()).actionGet();
}
 
Example #28
Source File: ElasticsearchSinkBaseTest.java    From flink with 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 #29
Source File: PravegaAnomalyDetectionProcessor.java    From pravega-samples with Apache License 2.0 5 votes vote down vote up
private IndexRequest createIndexRequest(Result element) {
	Gson gson = new Gson();
	String resultAsJson = gson.toJson(element);
	return Requests.indexRequest()
			.index(index)
			.type(type)
			.id(element.getNetworkId())
			.source(resultAsJson);
}
 
Example #30
Source File: SuggestRequestBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected SuggestRequest beforeExecute(SuggestRequest request) {
    try {
        XContentBuilder builder = XContentFactory.contentBuilder(Requests.CONTENT_TYPE);
        suggest.toXContent(builder, ToXContent.EMPTY_PARAMS);
        request.suggest(builder.bytes());
    } catch (IOException e) {
        throw new ElasticsearchException("Unable to build suggestion request", e);
    }
    return request;
}