-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Add indexing and search index settings This commit adds indexing and search settings to index settings. These settings allow configuring of slow logs for indexing and search, as well as setting search idle after. Search idle after duplicates the individual IndexSettings::searchIdleAfter property, but this pattern is already present e.g. IndexSettings::blocks and IndexSettings::blocksReadOnly, IndexSettings::blocksRead, etc. * add changelog entry * Add documentation and sample * Update IndexTemplates --------- (cherry picked from commit 9408cc2) Signed-off-by: Russ Cam <russcam@canva.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
97c2768
commit ff3916c
Showing
14 changed files
with
1,849 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
- [Index templates](#index-templates) | ||
- [Setup](#setup) | ||
|
||
|
||
# Index templates | ||
|
||
Index templates let you initialize new indexes with predefined mappings and settings. | ||
For example, if you continuously index log data, you can define an index template so that all of these indexes have | ||
the same number of shards and replicas. | ||
|
||
## Setup | ||
|
||
To get started, first create a client, create an index template. In this example, an index template is created, | ||
composed of two component templates: | ||
|
||
- `index-settings` which specifies index settings such as shard configuration and slowlog settings | ||
- `index-mappings` which specifies the field mappings | ||
|
||
```java | ||
import org.apache.hc.core5.http.HttpHost; | ||
|
||
final HttpHost[] hosts = new HttpHost[] { | ||
new HttpHost("http", "localhost", 9200) | ||
}; | ||
|
||
final OpenSearchTransport transport = ApacheHttpClient5TransportBuilder | ||
.builder(hosts) | ||
.setMapper(new JacksonJsonpMapper()) | ||
.build(); | ||
OpenSearchClient client = new OpenSearchClient(transport); | ||
|
||
final var indexSettingsComponentTemplate = "index-settings"; | ||
PutComponentTemplateRequest putComponentTemplateRequest = PutComponentTemplateRequest.of( | ||
c -> c.name(indexSettingsComponentTemplate) | ||
.settings( | ||
s -> s.numberOfShards("2") | ||
.numberOfReplicas("1") | ||
.indexing( | ||
i -> i.slowlog( | ||
sl -> sl.level("info") | ||
.reformat(true) | ||
.threshold(th -> th.index(ith -> ith.warn(Time.of(t -> t.time("2s"))))) | ||
) | ||
) | ||
.search( | ||
se -> se.slowlog(sl -> sl.level("info").threshold(th -> th.query(q -> q.warn(Time.of(t -> t.time("2s")))))) | ||
) | ||
) | ||
); | ||
client.cluster().putComponentTemplate(putComponentTemplateRequest); | ||
|
||
final var indexMappingsComponentTemplate = "index-mappings"; | ||
putComponentTemplateRequest = PutComponentTemplateRequest.of( | ||
c -> c.name(indexMappingsComponentTemplate).mappings(m -> m.properties("age", p -> p.integer(i -> i))) | ||
); | ||
client.cluster().putComponentTemplate(putComponentTemplateRequest); | ||
|
||
final var indexTemplateName = "my-index-template"; | ||
PutIndexTemplateRequest putIndexTemplateRequest = PutIndexTemplateRequest.of( | ||
it -> it.name(indexTemplateName) | ||
.indexPatterns("my-index-*") | ||
.composedOf(List.of(indexSettingsComponentTemplate, indexMappingsComponentTemplate)) | ||
); | ||
|
||
client.indices().putIndexTemplate(putIndexTemplateRequest); | ||
``` | ||
|
||
## Usage | ||
|
||
Create an index with a name that matches the index template's `indexPatterns` | ||
|
||
```java | ||
String indexName = "my-index-1"; | ||
client.indices().create(CreateIndexRequest.of(c -> c.index(indexName))); | ||
``` | ||
|
||
The index will be created with the index settings and mappings defined by the `my-index-template` index template. | ||
|
||
You can find a working sample of the above code in [IndexTemplates.java](../samples/src/main/java/org/opensearch/client/samples/IndexTemplates.java). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...-client/src/main/java/org/opensearch/client/opensearch/indices/IndexSettingsIndexing.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
*/ | ||
|
||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.client.opensearch.indices; | ||
|
||
import jakarta.json.stream.JsonGenerator; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
import org.opensearch.client.json.*; | ||
import org.opensearch.client.util.ObjectBuilder; | ||
import org.opensearch.client.util.ObjectBuilderBase; | ||
|
||
@JsonpDeserializable | ||
public class IndexSettingsIndexing implements JsonpSerializable { | ||
@Nullable | ||
private final IndexingSlowlog slowlog; | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
|
||
private IndexSettingsIndexing(Builder builder) { | ||
|
||
this.slowlog = builder.slowlog; | ||
|
||
} | ||
|
||
public static IndexSettingsIndexing of(Function<Builder, ObjectBuilder<IndexSettingsIndexing>> fn) { | ||
return fn.apply(new Builder()).build(); | ||
} | ||
|
||
/** | ||
* API name: {@code slowlog} | ||
*/ | ||
@Nullable | ||
public final IndexingSlowlog slowlog() { | ||
return this.slowlog; | ||
} | ||
|
||
/** | ||
* Serialize this object to JSON. | ||
*/ | ||
public void serialize(JsonGenerator generator, JsonpMapper mapper) { | ||
generator.writeStartObject(); | ||
serializeInternal(generator, mapper); | ||
generator.writeEnd(); | ||
} | ||
|
||
protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { | ||
|
||
if (this.slowlog != null) { | ||
generator.writeKey("slowlog"); | ||
this.slowlog.serialize(generator, mapper); | ||
|
||
} | ||
|
||
} | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* Builder for {@link IndexSettingsIndexing}. | ||
*/ | ||
|
||
public static class Builder extends ObjectBuilderBase implements ObjectBuilder<IndexSettingsIndexing> { | ||
@Nullable | ||
private IndexingSlowlog slowlog; | ||
|
||
/** | ||
* API name: {@code slowlog} | ||
*/ | ||
public final Builder slowlog(@Nullable IndexingSlowlog value) { | ||
this.slowlog = value; | ||
return this; | ||
} | ||
|
||
/** | ||
* API name: {@code slowlog} | ||
*/ | ||
public final Builder slowlog(Function<IndexingSlowlog.Builder, ObjectBuilder<IndexingSlowlog>> fn) { | ||
return this.slowlog(fn.apply(new IndexingSlowlog.Builder()).build()); | ||
} | ||
|
||
/** | ||
* Builds a {@link IndexSettingsIndexing}. | ||
* | ||
* @throws NullPointerException | ||
* if some of the required fields are null. | ||
*/ | ||
public IndexSettingsIndexing build() { | ||
_checkSingleUse(); | ||
|
||
return new IndexSettingsIndexing(this); | ||
} | ||
} | ||
|
||
// --------------------------------------------------------------------------------------------- | ||
|
||
/** | ||
* Json deserializer for {@link IndexSettingsIndexing} | ||
*/ | ||
public static final JsonpDeserializer<IndexSettingsIndexing> _DESERIALIZER = ObjectBuilderDeserializer.lazy( | ||
Builder::new, | ||
IndexSettingsIndexing::setupSettingsSearchDeserializer | ||
); | ||
|
||
protected static void setupSettingsSearchDeserializer(ObjectDeserializer<Builder> op) { | ||
|
||
op.add(Builder::slowlog, IndexingSlowlog._DESERIALIZER, "slowlog"); | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.