Search in sources :

Example 91 with IndexSettings

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" });
}
Also used : NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) IndexSettings(org.opensearch.index.IndexSettings) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexSettings(org.opensearch.index.IndexSettings) Settings(org.opensearch.common.settings.Settings)

Example 92 with IndexSettings

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());
}
Also used : IndexSettings(org.opensearch.index.IndexSettings) StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) Index(org.opensearch.index.Index) CharTokenizer(org.apache.lucene.analysis.util.CharTokenizer) IndexSettings(org.opensearch.index.IndexSettings) Settings(org.opensearch.common.settings.Settings)

Example 93 with IndexSettings

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);
}
Also used : Mapper(org.opensearch.index.mapper.Mapper) IndexScopedSettings(org.opensearch.common.settings.IndexScopedSettings) IndexSettings(org.opensearch.index.IndexSettings) ModelDao(org.opensearch.knn.indices.ModelDao) MapperService(org.opensearch.index.mapper.MapperService) HashSet(java.util.HashSet)

Example 94 with IndexSettings

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));
}
Also used : AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) GetIndexResponse(org.opensearch.client.indices.GetIndexResponse) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) MappingMetadata(org.opensearch.cluster.metadata.MappingMetadata) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings) TimeValue(org.opensearch.common.unit.TimeValue)

Example 95 with IndexSettings

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));
}
Also used : GetSettingsResponse(org.opensearch.action.admin.indices.settings.get.GetSettingsResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

IndexSettings (org.opensearch.index.IndexSettings)195 Settings (org.opensearch.common.settings.Settings)137 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)72 IOException (java.io.IOException)39 Index (org.opensearch.index.Index)32 Matchers.containsString (org.hamcrest.Matchers.containsString)25 Version (org.opensearch.Version)23 Store (org.opensearch.index.store.Store)23 Map (java.util.Map)22 QueryShardContext (org.opensearch.index.query.QueryShardContext)22 OpenSearchException (org.opensearch.OpenSearchException)21 MapperService (org.opensearch.index.mapper.MapperService)20 ShardId (org.opensearch.index.shard.ShardId)19 HashMap (java.util.HashMap)18 ArrayList (java.util.ArrayList)17 List (java.util.List)17 IndexService (org.opensearch.index.IndexService)17 IndexShard (org.opensearch.index.shard.IndexShard)17 HashSet (java.util.HashSet)16 Query (org.apache.lucene.search.Query)16