use of org.opensearch.index.IndexSettings in project OpenSearch by opensearch-project.
the class PatternCaptureTokenFilterTests method testPatternCaptureTokenFilter.
public void testPatternCaptureTokenFilter() throws Exception {
String json = "/org/opensearch/analysis/common/pattern_capture.json";
Settings settings = Settings.builder().put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()).loadFromStream(json, getClass().getResourceAsStream(json), false).put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT).build();
IndexSettings idxSettings = IndexSettingsModule.newIndexSettings("index", settings);
IndexAnalyzers indexAnalyzers = createTestAnalysis(idxSettings, settings, new CommonAnalysisPlugin()).indexAnalyzers;
NamedAnalyzer analyzer1 = indexAnalyzers.get("single");
assertTokenStreamContents(analyzer1.tokenStream("test", "foobarbaz"), new String[] { "foobarbaz", "foobar", "foo" });
NamedAnalyzer analyzer2 = indexAnalyzers.get("multi");
assertTokenStreamContents(analyzer2.tokenStream("test", "abc123def"), new String[] { "abc123def", "abc", "123", "def" });
NamedAnalyzer analyzer3 = indexAnalyzers.get("preserve");
assertTokenStreamContents(analyzer3.tokenStream("test", "foobarbaz"), new String[] { "foobar", "foo" });
}
use of org.opensearch.index.IndexSettings in project OpenSearch by opensearch-project.
the class CharGroupTokenizerFactoryTests method testMaxTokenLength.
public void testMaxTokenLength() throws IOException {
final Index index = new Index("test", "_na_");
final Settings indexSettings = newAnalysisSettingsBuilder().build();
IndexSettings indexProperties = IndexSettingsModule.newIndexSettings(index, indexSettings);
final String name = "cg";
String[] conf = new String[] { "-" };
final Settings defaultLengthSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf).build();
CharTokenizer tokenizer = (CharTokenizer) new CharGroupTokenizerFactory(indexProperties, null, name, defaultLengthSettings).create();
String textWithVeryLongToken = RandomStrings.randomAsciiAlphanumOfLength(random(), 256).concat("-trailing");
try (Reader reader = new StringReader(textWithVeryLongToken)) {
tokenizer.setReader(reader);
assertTokenStreamContents(tokenizer, new String[] { textWithVeryLongToken.substring(0, 255), textWithVeryLongToken.substring(255, 256), "trailing" });
}
final Settings analysisSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf).put("max_token_length", 2).build();
tokenizer = (CharTokenizer) new CharGroupTokenizerFactory(indexProperties, null, name, analysisSettings).create();
try (Reader reader = new StringReader("one-two-three")) {
tokenizer.setReader(reader);
assertTokenStreamContents(tokenizer, new String[] { "on", "e", "tw", "o", "th", "re", "e" });
}
final Settings tooLongLengthSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf).put("max_token_length", 1024 * 1024 + 1).build();
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new CharGroupTokenizerFactory(indexProperties, null, name, tooLongLengthSettings).create());
assertEquals("maxTokenLen must be greater than 0 and less than 1048576 passed: 1048577", e.getMessage());
final Settings negativeLengthSettings = newAnalysisSettingsBuilder().putList("tokenize_on_chars", conf).put("max_token_length", -1).build();
e = expectThrows(IllegalArgumentException.class, () -> new CharGroupTokenizerFactory(indexProperties, null, name, negativeLengthSettings).create());
assertEquals("maxTokenLen must be greater than 0 and less than 1048576 passed: -1", e.getMessage());
}
use of org.opensearch.index.IndexSettings in project k-NN by opensearch-project.
the class KNNVectorFieldMapperTests method buildParserContext.
public Mapper.TypeParser.ParserContext buildParserContext(String indexName, Settings settings) {
IndexSettings indexSettings = new IndexSettings(buildIndexMetaData(indexName, settings), Settings.EMPTY, new IndexScopedSettings(Settings.EMPTY, new HashSet<>(IndexScopedSettings.BUILT_IN_INDEX_SETTINGS)));
MapperService mapperService = mock(MapperService.class);
when(mapperService.getIndexSettings()).thenReturn(indexSettings);
// Setup blank
ModelDao mockModelDao = mock(ModelDao.class);
return new Mapper.TypeParser.ParserContext(null, mapperService, type -> new KNNVectorFieldMapper.TypeParser(() -> mockModelDao), CURRENT, null, null, null);
}
use of org.opensearch.index.IndexSettings in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetIndex.
public void testGetIndex() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Settings settings = Settings.builder().put("number_of_shards", 3).build();
String mappings = "{\"properties\":{\"field-1\":{\"type\":\"integer\"}}}";
CreateIndexRequest createIndexRequest = new CreateIndexRequest("index").settings(settings).mapping(mappings, XContentType.JSON);
CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
// tag::get-index-request
// <1>
GetIndexRequest request = new GetIndexRequest("index");
// end::get-index-request
// tag::get-index-request-indicesOptions
// <1>
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// end::get-index-request-indicesOptions
// tag::get-index-request-includeDefaults
// <1>
request.includeDefaults(true);
// end::get-index-request-includeDefaults
// tag::get-index-execute
GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT);
// end::get-index-execute
// tag::get-index-response
// <1>
MappingMetadata indexMappings = getIndexResponse.getMappings().get("index");
// <2>
Map<String, Object> indexTypeMappings = indexMappings.getSourceAsMap();
// <3>
List<AliasMetadata> indexAliases = getIndexResponse.getAliases().get("index");
// <4>
String numberOfShardsString = getIndexResponse.getSetting("index", "index.number_of_shards");
// <5>
Settings indexSettings = getIndexResponse.getSettings().get("index");
// <6>
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
TimeValue time = getIndexResponse.getDefaultSettings().get("index").getAsTime("index.refresh_interval", // <7>
null);
// end::get-index-response
assertEquals(Collections.singletonMap("properties", Collections.singletonMap("field-1", Collections.singletonMap("type", "integer"))), indexTypeMappings);
assertTrue(indexAliases.isEmpty());
assertEquals(IndexSettings.DEFAULT_REFRESH_INTERVAL, time);
assertEquals("3", numberOfShardsString);
assertEquals(Integer.valueOf(3), numberOfShards);
// tag::get-index-execute-listener
ActionListener<GetIndexResponse> listener = new ActionListener<GetIndexResponse>() {
@Override
public void onResponse(GetIndexResponse getIndexResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::get-index-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::get-index-execute-async
// <1>
client.indices().getAsync(request, RequestOptions.DEFAULT, listener);
// end::get-index-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
use of org.opensearch.index.IndexSettings in project OpenSearch by opensearch-project.
the class IndicesClientDocumentationIT method testGetSettingsWithDefaults.
public void testGetSettingsWithDefaults() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Settings settings = Settings.builder().put("number_of_shards", 3).build();
CreateIndexResponse createIndexResponse = client.indices().create(new CreateIndexRequest("index").settings(settings), RequestOptions.DEFAULT);
assertTrue(createIndexResponse.isAcknowledged());
}
GetSettingsRequest request = new GetSettingsRequest().indices("index");
request.indicesOptions(IndicesOptions.lenientExpandOpen());
// tag::get-settings-request-include-defaults
// <1>
request.includeDefaults(true);
// end::get-settings-request-include-defaults
GetSettingsResponse getSettingsResponse = client.indices().getSettings(request, RequestOptions.DEFAULT);
String numberOfShardsString = getSettingsResponse.getSetting("index", "index.number_of_shards");
Settings indexSettings = getSettingsResponse.getIndexToSettings().get("index");
Integer numberOfShards = indexSettings.getAsInt("index.number_of_shards", null);
// tag::get-settings-defaults-response
// <1>
String refreshInterval = getSettingsResponse.getSetting("index", "index.refresh_interval");
// <2>
Settings indexDefaultSettings = getSettingsResponse.getIndexToDefaultSettings().get("index");
// end::get-settings-defaults-response
assertEquals("3", numberOfShardsString);
assertEquals(Integer.valueOf(3), numberOfShards);
assertNotNull("with defaults enabled we should get a value for refresh_interval!", refreshInterval);
assertEquals(refreshInterval, indexDefaultSettings.get("index.refresh_interval"));
ActionListener<GetSettingsResponse> listener = new ActionListener<GetSettingsResponse>() {
@Override
public void onResponse(GetSettingsResponse GetSettingsResponse) {
}
@Override
public void onFailure(Exception e) {
}
};
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
client.indices().getSettingsAsync(request, RequestOptions.DEFAULT, listener);
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Aggregations