Skip to content

Commit

Permalink
Add indexing and search index settings (#667)
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.

Signed-off-by: Russ Cam <russcam@canva.com>

* add changelog entry

Signed-off-by: Russ Cam <russcam@canva.com>

* Add documentation and sample

Signed-off-by: Russ Cam <russcam@canva.com>

* Update IndexTemplates

Signed-off-by: Russ Cam <russcam@canva.com>

---------

Signed-off-by: Russ Cam <russcam@canva.com>
Signed-off-by: Andriy Redko <andriy.redko@aiven.io>
  • Loading branch information
russcam authored and reta committed Oct 27, 2023
1 parent de8c2a9 commit 4e78f11
Show file tree
Hide file tree
Showing 16 changed files with 1,858 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
## [Unreleased 2.x]

### Added
- Added support for indexing and search index settings ([#667](https://github.com/opensearch-project/opensearch-java/pull/667))

### Dependencies

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=591855b517fc635b9e04de1d05d5e76ada3f89f5fc76f87978d1b245b4f69225
distributionSha256Sum=3e1af3ae886920c3ac87f7a91f816c0c7c436f276a6eefdb3da152100fef72ae
14 changes: 7 additions & 7 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +202,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
79 changes: 79 additions & 0 deletions guides/index_templates.md
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).
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,12 @@ public class IndexSettings implements JsonpSerializable {
@Nullable
private final IndexSettingsMapping mapping;

@Nullable
private final IndexSettingsIndexing indexing;

@Nullable
private final IndexSettingsSearch search;

@Nullable
private final Boolean knn;

Expand Down Expand Up @@ -280,6 +286,8 @@ private IndexSettings(Builder builder) {
this.analysis = builder.analysis;
this.settings = builder.settings;
this.mapping = builder.mapping;
this.indexing = builder.indexing;
this.search = builder.search;
this.knn = builder.knn;
this.knnAlgoParamEfSearch = builder.knnAlgoParamEfSearch;

Expand Down Expand Up @@ -748,6 +756,22 @@ public final IndexSettingsMapping mapping() {
return this.mapping;
}

/**
* API name: {@code indexing}
*/
@Nullable
public final IndexSettingsIndexing indexing() {
return this.indexing;
}

/**
* API name: {@code search}
*/
@Nullable
public final IndexSettingsSearch search() {
return this.search;
}

/**
* API name: {@code knn}
*/
Expand Down Expand Up @@ -1054,6 +1078,16 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.writeKey("mapping");
this.mapping.serialize(generator, mapper);

}
if (this.indexing != null) {
generator.writeKey("indexing");
this.indexing.serialize(generator, mapper);

}
if (this.search != null) {
generator.writeKey("search");
this.search.serialize(generator, mapper);

}
if (this.knn != null) {
generator.writeKey("knn");
Expand Down Expand Up @@ -1240,6 +1274,12 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<I
@Nullable
private IndexSettingsMapping mapping;

@Nullable
private IndexSettingsIndexing indexing;

@Nullable
private IndexSettingsSearch search;

@Nullable
private Boolean knn;

Expand Down Expand Up @@ -1821,6 +1861,36 @@ public final Builder mapping(Function<IndexSettingsMapping.Builder, ObjectBuilde
return this.mapping(fn.apply(new IndexSettingsMapping.Builder()).build());
}

/**
* API name: {@code indexing}
*/
public final Builder indexing(@Nullable IndexSettingsIndexing value) {
this.indexing = value;
return this;
}

/**
* API name: {@code indexing}
*/
public final Builder indexing(Function<IndexSettingsIndexing.Builder, ObjectBuilder<IndexSettingsIndexing>> fn) {
return this.indexing(fn.apply(new IndexSettingsIndexing.Builder()).build());
}

/**
* API name: {@code search}
*/
public final Builder search(@Nullable IndexSettingsSearch value) {
this.search = value;
return this;
}

/**
* API name: {@code search}
*/
public final Builder search(Function<IndexSettingsSearch.Builder, ObjectBuilder<IndexSettingsSearch>> fn) {
return this.search(fn.apply(new IndexSettingsSearch.Builder()).build());
}

/**
* Builds a {@link IndexSettings}.
*
Expand Down Expand Up @@ -1995,6 +2065,8 @@ protected static void setupIndexSettingsDeserializer(ObjectDeserializer<IndexSet
op.add(Builder::analysis, IndexSettingsAnalysis._DESERIALIZER, "analysis", "index.analysis");
op.add(Builder::settings, IndexSettings._DESERIALIZER, "settings");
op.add(Builder::mapping, IndexSettingsMapping._DESERIALIZER, "mapping");
op.add(Builder::indexing, IndexSettingsIndexing._DESERIALIZER, "indexing");
op.add(Builder::search, IndexSettingsSearch._DESERIALIZER, "search");
op.add(Builder::knn, JsonpDeserializer.booleanDeserializer(), "knn", "index.knn");
op.add(
Builder::knnAlgoParamEfSearch,
Expand Down
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");

}

}
Loading

0 comments on commit 4e78f11

Please sign in to comment.