use of org.elasticsearch.action.index.IndexRequest in project elasticsearch-jdbc by jprante.
the class StandardSink method index.
@Override
public void index(IndexableObject object, boolean create) throws IOException {
if (clientAPI == null) {
return;
}
if (Strings.hasLength(object.index())) {
setIndex(object.index());
}
if (Strings.hasLength(object.type())) {
setType(object.type());
}
if (Strings.hasLength(object.id())) {
setId(object.id());
}
IndexRequest request = Requests.indexRequest(this.index).type(this.type).id(getId()).source(object.build());
if (object.meta(ControlKeys._version.name()) != null) {
request.versionType(VersionType.EXTERNAL).version(Long.parseLong(object.meta(ControlKeys._version.name())));
}
if (object.meta(ControlKeys._routing.name()) != null) {
request.routing(object.meta(ControlKeys._routing.name()));
}
if (object.meta(ControlKeys._parent.name()) != null) {
request.parent(object.meta(ControlKeys._parent.name()));
}
if (object.meta(ControlKeys._timestamp.name()) != null) {
request.timestamp(object.meta(ControlKeys._timestamp.name()));
}
if (object.meta(ControlKeys._ttl.name()) != null) {
request.ttl(Long.parseLong(object.meta(ControlKeys._ttl.name())));
}
if (logger.isTraceEnabled()) {
logger.trace("adding bulk index action {}", request.source().toUtf8());
}
clientAPI.bulkIndex(request);
}
use of org.elasticsearch.action.index.IndexRequest in project spring-boot by spring-projects.
the class ElasticsearchRestClientAutoConfigurationIntegrationTests method restClientCanQueryElasticsearchNode.
@Test
@SuppressWarnings("deprecation")
void restClientCanQueryElasticsearchNode() {
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=" + elasticsearch.getHttpHostAddress(), "spring.elasticsearch.connection-timeout=120s", "spring.elasticsearch.socket-timeout=120s").run((context) -> {
org.elasticsearch.client.RestHighLevelClient client = context.getBean(org.elasticsearch.client.RestHighLevelClient.class);
Map<String, String> source = new HashMap<>();
source.put("a", "alpha");
source.put("b", "bravo");
IndexRequest index = new IndexRequest("test").id("1").source(source);
client.index(index, RequestOptions.DEFAULT);
GetRequest getRequest = new GetRequest("test").id("1");
assertThat(client.get(getRequest, RequestOptions.DEFAULT).isExists()).isTrue();
});
}
use of org.elasticsearch.action.index.IndexRequest in project spring-boot by spring-projects.
the class ReactiveElasticsearchRestClientAutoConfigurationIntegrationTests method restClientCanQueryElasticsearchNode.
@Test
void restClientCanQueryElasticsearchNode() {
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=" + elasticsearch.getHttpHostAddress(), "spring.elasticsearch.connection-timeout=120s", "spring.elasticsearch.socket-timeout=120s").run((context) -> {
ReactiveElasticsearchClient client = context.getBean(ReactiveElasticsearchClient.class);
Map<String, String> source = new HashMap<>();
source.put("a", "alpha");
source.put("b", "bravo");
IndexRequest indexRequest = new IndexRequest("foo").id("1").source(source);
GetRequest getRequest = new GetRequest("foo").id("1");
GetResult getResult = client.index(indexRequest).then(client.get(getRequest)).block();
assertThat(getResult).isNotNull();
assertThat(getResult.isExists()).isTrue();
});
}
use of org.elasticsearch.action.index.IndexRequest in project graylog2-server by Graylog2.
the class FixtureImporterES7 method importNode.
private void importNode(JsonNode root) throws IOException {
/* This supports the nosqlunit DataSet structure:
*
* {
* "documents": [
* {
* "document": [
* {
* "index": {
* "indexName": "graylog_0",
* "indexId": "0"
* }
* },
* {
* "data": {
* "source": "example.org",
* "message": "Hi",
* "timestamp": "2015-01-01 01:00:00.000"
* }
* }
* ]
* }
* ]
* }
*/
final BulkRequest bulkRequest = new BulkRequest();
final Set<String> targetIndices = new HashSet<>();
for (final JsonNode document : root.path("documents")) {
final List<JsonNode> indexes = new ArrayList<>();
Map<String, Object> data = new HashMap<>();
for (JsonNode entry : document.path("document")) {
if (entry.hasNonNull("index")) {
indexes.add(entry.path("index"));
} else if (entry.hasNonNull("data")) {
data = OBJECT_MAPPER.convertValue(entry.path("data"), TypeReferences.MAP_STRING_OBJECT);
}
}
for (final JsonNode index : indexes) {
final IndexRequest indexRequest = new IndexRequest().source(data);
final String indexName = index.path("indexName").asText(null);
if (indexName == null) {
throw new IllegalArgumentException("Missing indexName in " + index);
}
targetIndices.add(indexName);
indexRequest.index(indexName);
if (index.hasNonNull("indexId")) {
indexRequest.id(index.path("indexId").asText());
}
bulkRequest.add(indexRequest);
}
}
for (String indexName : targetIndices) {
if (!indexExists(indexName)) {
createIndex(indexName);
}
}
final BulkResponse result = client.execute((c, requestOptions) -> c.bulk(bulkRequest, requestOptions), "Unable to import fixtures.");
if (result.hasFailures()) {
throw new IllegalStateException("Error while bulk indexing documents: " + result.buildFailureMessage());
}
}
use of org.elasticsearch.action.index.IndexRequest in project graylog2-server by Graylog2.
the class MessagesES7IT method indexMessage.
@Override
protected boolean indexMessage(String index, Map<String, Object> source, String id) {
final IndexRequest indexRequest = new IndexRequest(index).source(source).id(id);
final IndexResponse result = this.elasticsearch.elasticsearchClient().execute((c, requestOptions) -> c.index(indexRequest, requestOptions));
return result.status().equals(RestStatus.CREATED);
}
Aggregations