Skip to content

Commit

Permalink
Add indexing and search index settings (#667) (#689)
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
Show file tree
Hide file tree
Showing 14 changed files with 1,849 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
## [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
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 ff3916c

Please sign in to comment.