Search in sources :

Example 6 with Settings.builder

use of org.opensearch.common.settings.Settings.builder in project OpenSearch by opensearch-project.

the class QueryRescorerIT method testMoreDocs.

public void testMoreDocs() throws Exception {
    Builder builder = Settings.builder();
    XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("field1").field("type", "text").field("analyzer", "whitespace").endObject().endObject().endObject();
    assertAcked(client().admin().indices().prepareCreate("test").setMapping(mapping).setSettings(builder.put("index.number_of_shards", 1)));
    client().prepareIndex("test").setId("1").setSource("field1", "massachusetts avenue boston massachusetts").get();
    client().prepareIndex("test").setId("2").setSource("field1", "lexington avenue boston massachusetts").get();
    client().prepareIndex("test").setId("3").setSource("field1", "boston avenue lexington massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("4").setSource("field1", "boston road lexington massachusetts").get();
    client().prepareIndex("test").setId("5").setSource("field1", "lexington street lexington massachusetts").get();
    client().prepareIndex("test").setId("6").setSource("field1", "massachusetts avenue lexington massachusetts").get();
    client().prepareIndex("test").setId("7").setSource("field1", "bosten street san franciso california").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("8").setSource("field1", "hollywood boulevard los angeles california").get();
    client().prepareIndex("test").setId("9").setSource("field1", "1st street boston massachussetts").get();
    client().prepareIndex("test").setId("10").setSource("field1", "1st street boston massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    client().prepareIndex("test").setId("11").setSource("field1", "2st street boston massachusetts").get();
    client().prepareIndex("test").setId("12").setSource("field1", "3st street boston massachusetts").get();
    client().admin().indices().prepareRefresh("test").get();
    SearchResponse searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertFirstHit(searchResponse, hasId("2"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("3"));
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(0).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertThat(searchResponse.getHits().getMaxScore(), equalTo(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("2"));
    assertSecondHit(searchResponse, hasId("6"));
    assertThirdHit(searchResponse, hasId("3"));
    // Make sure non-zero from works:
    searchResponse = client().prepareSearch().setQuery(QueryBuilders.matchQuery("field1", "lexington avenue massachusetts").operator(Operator.OR)).setFrom(2).setSize(5).setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setRescorer(new QueryRescorerBuilder(matchPhraseQuery("field1", "lexington avenue massachusetts").slop(3)).setQueryWeight(0.6f).setRescoreQueryWeight(2.0f), 20).get();
    assertThat(searchResponse.getHits().getHits().length, equalTo(5));
    assertHitCount(searchResponse, 9);
    assertThat(searchResponse.getHits().getMaxScore(), greaterThan(searchResponse.getHits().getHits()[0].getScore()));
    assertFirstHit(searchResponse, hasId("3"));
}
Also used : QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) IndexRequestBuilder(org.opensearch.action.index.IndexRequestBuilder) SearchRequestBuilder(org.opensearch.action.search.SearchRequestBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) Builder(org.opensearch.common.settings.Settings.Builder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) QueryRescorerBuilder(org.opensearch.search.rescore.QueryRescorerBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with Settings.builder

use of org.opensearch.common.settings.Settings.builder in project OpenSearch by opensearch-project.

the class StopTokenFilterTests method testCorrectPositionIncrementSetting.

public void testCorrectPositionIncrementSetting() throws IOException {
    Builder builder = Settings.builder().put("index.analysis.filter.my_stop.type", "stop");
    if (random().nextBoolean()) {
        builder.put("index.analysis.filter.my_stop.version", Version.LATEST);
    } else {
    // don't specify
    }
    builder.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString());
    OpenSearchTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(builder.build());
    TokenFilterFactory tokenFilter = analysis.tokenFilter.get("my_stop");
    assertThat(tokenFilter, instanceOf(StopTokenFilterFactory.class));
    Tokenizer tokenizer = new WhitespaceTokenizer();
    tokenizer.setReader(new StringReader("foo bar"));
    TokenStream create = tokenFilter.create(tokenizer);
    assertThat(create, instanceOf(StopFilter.class));
}
Also used : WhitespaceTokenizer(org.apache.lucene.analysis.core.WhitespaceTokenizer) TokenStream(org.apache.lucene.analysis.TokenStream) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) StopFilter(org.apache.lucene.analysis.StopFilter) SuggestStopFilter(org.apache.lucene.search.suggest.analyzing.SuggestStopFilter) Builder(org.opensearch.common.settings.Settings.Builder) StringReader(java.io.StringReader) WhitespaceTokenizer(org.apache.lucene.analysis.core.WhitespaceTokenizer) Tokenizer(org.apache.lucene.analysis.Tokenizer)

Example 8 with Settings.builder

use of org.opensearch.common.settings.Settings.builder in project OpenSearch by opensearch-project.

the class InternalTestCluster method reset.

private synchronized void reset(boolean wipeData) throws IOException {
    // clear all rules for mock transport services
    for (NodeAndClient nodeAndClient : nodes.values()) {
        TransportService transportService = nodeAndClient.node.injector().getInstance(TransportService.class);
        if (transportService instanceof MockTransportService) {
            final MockTransportService mockTransportService = (MockTransportService) transportService;
            mockTransportService.clearAllRules();
        }
    }
    randomlyResetClients();
    final int newSize = sharedNodesSeeds.length;
    if (nextNodeId.get() == newSize && nodes.size() == newSize) {
        if (wipeData) {
            wipePendingDataDirectories();
        }
        logger.debug("Cluster hasn't changed - moving out - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
        return;
    }
    logger.debug("Cluster is NOT consistent - restarting shared nodes - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
    // trash all nodes with id >= sharedNodesSeeds.length - they are non shared
    final List<NodeAndClient> toClose = new ArrayList<>();
    for (NodeAndClient nodeAndClient : nodes.values()) {
        if (nodeAndClient.nodeAndClientId() >= sharedNodesSeeds.length) {
            logger.debug("Close Node [{}] not shared", nodeAndClient.name);
            toClose.add(nodeAndClient);
        }
    }
    stopNodesAndClients(toClose);
    // clean up what the nodes left that is unused
    if (wipeData) {
        wipePendingDataDirectories();
    }
    assertTrue("expected at least one cluster-manager-eligible node left in " + nodes, nodes.isEmpty() || nodes.values().stream().anyMatch(NodeAndClient::isMasterEligible));
    final int prevNodeCount = nodes.size();
    // start any missing node
    assert newSize == numSharedDedicatedClusterManagerNodes + numSharedDataNodes + numSharedCoordOnlyNodes;
    final int numberOfClusterManagerNodes = numSharedDedicatedClusterManagerNodes > 0 ? numSharedDedicatedClusterManagerNodes : numSharedDataNodes;
    final int defaultMinClusterManagerNodes = (numberOfClusterManagerNodes / 2) + 1;
    // we want to start nodes in one go
    final List<NodeAndClient> toStartAndPublish = new ArrayList<>();
    final Runnable onTransportServiceStarted = () -> rebuildUnicastHostFiles(toStartAndPublish);
    final List<Settings> settings = new ArrayList<>();
    for (int i = 0; i < numSharedDedicatedClusterManagerNodes; i++) {
        final Settings nodeSettings = getNodeSettings(i, sharedNodesSeeds[i], Settings.EMPTY, defaultMinClusterManagerNodes);
        settings.add(removeRoles(nodeSettings, Collections.singleton(DiscoveryNodeRole.DATA_ROLE)));
    }
    for (int i = numSharedDedicatedClusterManagerNodes; i < numSharedDedicatedClusterManagerNodes + numSharedDataNodes; i++) {
        final Settings nodeSettings = getNodeSettings(i, sharedNodesSeeds[i], Settings.EMPTY, defaultMinClusterManagerNodes);
        if (numSharedDedicatedClusterManagerNodes > 0) {
            settings.add(removeRoles(nodeSettings, Collections.singleton(DiscoveryNodeRole.MASTER_ROLE)));
        } else {
            // if we don't have dedicated cluster-manager nodes, keep things default
            settings.add(nodeSettings);
        }
    }
    for (int i = numSharedDedicatedClusterManagerNodes + numSharedDataNodes; i < numSharedDedicatedClusterManagerNodes + numSharedDataNodes + numSharedCoordOnlyNodes; i++) {
        final Builder extraSettings = Settings.builder().put(noRoles());
        settings.add(getNodeSettings(i, sharedNodesSeeds[i], extraSettings.build(), defaultMinClusterManagerNodes));
    }
    int autoBootstrapClusterManagerNodeIndex = -1;
    final List<String> clusterManagerNodeNames = settings.stream().filter(DiscoveryNode::isMasterNode).map(Node.NODE_NAME_SETTING::get).collect(Collectors.toList());
    if (prevNodeCount == 0 && autoManageClusterManagerNodes) {
        if (numSharedDedicatedClusterManagerNodes > 0) {
            autoBootstrapClusterManagerNodeIndex = RandomNumbers.randomIntBetween(random, 0, numSharedDedicatedClusterManagerNodes - 1);
        } else if (numSharedDataNodes > 0) {
            autoBootstrapClusterManagerNodeIndex = RandomNumbers.randomIntBetween(random, 0, numSharedDataNodes - 1);
        }
    }
    final List<Settings> updatedSettings = bootstrapClusterManagerNodeWithSpecifiedIndex(settings);
    for (int i = 0; i < numSharedDedicatedClusterManagerNodes + numSharedDataNodes + numSharedCoordOnlyNodes; i++) {
        Settings nodeSettings = updatedSettings.get(i);
        if (i == autoBootstrapClusterManagerNodeIndex) {
            nodeSettings = Settings.builder().putList(INITIAL_CLUSTER_MANAGER_NODES_SETTING.getKey(), clusterManagerNodeNames).put(nodeSettings).build();
        }
        final NodeAndClient nodeAndClient = buildNode(i, nodeSettings, true, onTransportServiceStarted);
        toStartAndPublish.add(nodeAndClient);
    }
    startAndPublishNodesAndClients(toStartAndPublish);
    nextNodeId.set(newSize);
    assert size() == newSize;
    if (autoManageClusterManagerNodes && newSize > 0) {
        validateClusterFormed();
    }
    logger.debug("Cluster is consistent again - nodes: [{}] nextNodeId: [{}] numSharedNodes: [{}]", nodes.keySet(), nextNodeId.get(), newSize);
}
Also used : DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockTransportService(org.opensearch.test.transport.MockTransportService) Builder(org.opensearch.common.settings.Settings.Builder) NodeRoles.dataOnlyNode(org.opensearch.test.NodeRoles.dataOnlyNode) Node(org.opensearch.node.Node) DiscoveryNode(org.opensearch.cluster.node.DiscoveryNode) MockNode(org.opensearch.node.MockNode) ArrayList(java.util.ArrayList) TransportService(org.opensearch.transport.TransportService) MockTransportService(org.opensearch.test.transport.MockTransportService) MockSecureSettings(org.opensearch.common.settings.MockSecureSettings) Settings(org.opensearch.common.settings.Settings) SecureSettings(org.opensearch.common.settings.SecureSettings) TransportSettings(org.opensearch.transport.TransportSettings) RecoverySettings(org.opensearch.indices.recovery.RecoverySettings) DiskThresholdSettings(org.opensearch.cluster.routing.allocation.DiskThresholdSettings) DiscoverySettings(org.opensearch.node.Node.DiscoverySettings)

Example 9 with Settings.builder

use of org.opensearch.common.settings.Settings.builder in project OpenSearch by opensearch-project.

the class InternalTestCluster method getRandomNodeSettings.

private static Settings getRandomNodeSettings(long seed) {
    Random random = new Random(seed);
    Builder builder = Settings.builder();
    builder.put(TransportSettings.TRANSPORT_COMPRESS.getKey(), rarely(random));
    if (random.nextBoolean()) {
        builder.put("cache.recycler.page.type", RandomPicks.randomFrom(random, PageCacheRecycler.Type.values()));
    }
    if (random.nextInt(10) == 0) {
        // 10% of the nodes have a very frequent check interval
        builder.put(SearchService.KEEPALIVE_INTERVAL_SETTING.getKey(), timeValueMillis(10 + random.nextInt(2000)).getStringRep());
    } else if (random.nextInt(10) != 0) {
        // 90% of the time - 10% of the time we don't set anything
        builder.put(SearchService.KEEPALIVE_INTERVAL_SETTING.getKey(), timeValueSeconds(10 + random.nextInt(5 * 60)).getStringRep());
    }
    if (random.nextBoolean()) {
        // sometimes set a
        builder.put(SearchService.DEFAULT_KEEPALIVE_SETTING.getKey(), timeValueSeconds(100 + random.nextInt(5 * 60)).getStringRep());
    }
    builder.put(OpenSearchExecutors.NODE_PROCESSORS_SETTING.getKey(), 1 + random.nextInt(Math.min(4, Runtime.getRuntime().availableProcessors())));
    if (random.nextBoolean()) {
        if (random.nextBoolean()) {
            builder.put("indices.fielddata.cache.size", 1 + random.nextInt(1000), ByteSizeUnit.MB);
        }
    }
    // randomize tcp settings
    if (random.nextBoolean()) {
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_RECOVERY.getKey(), random.nextInt(2) + 1);
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_BULK.getKey(), random.nextInt(3) + 1);
        builder.put(TransportSettings.CONNECTIONS_PER_NODE_REG.getKey(), random.nextInt(6) + 1);
    }
    if (random.nextBoolean()) {
        builder.put(MappingUpdatedAction.INDICES_MAPPING_DYNAMIC_TIMEOUT_SETTING.getKey(), timeValueSeconds(RandomNumbers.randomIntBetween(random, 10, 30)).getStringRep());
        builder.put(MappingUpdatedAction.INDICES_MAX_IN_FLIGHT_UPDATES_SETTING.getKey(), RandomNumbers.randomIntBetween(random, 1, 10));
    }
    // turning on the real memory circuit breaker leads to spurious test failures. As have no full control over heap usage, we
    // turn it off for these tests.
    builder.put(HierarchyCircuitBreakerService.USE_REAL_MEMORY_USAGE_SETTING.getKey(), false);
    if (random.nextInt(10) == 0) {
        builder.put(HierarchyCircuitBreakerService.REQUEST_CIRCUIT_BREAKER_TYPE_SETTING.getKey(), "noop");
        builder.put(HierarchyCircuitBreakerService.FIELDDATA_CIRCUIT_BREAKER_TYPE_SETTING.getKey(), "noop");
    }
    if (random.nextBoolean()) {
        if (random.nextInt(10) == 0) {
            // do something crazy slow here
            builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(), new ByteSizeValue(RandomNumbers.randomIntBetween(random, 1, 10), ByteSizeUnit.MB));
        } else {
            builder.put(RecoverySettings.INDICES_RECOVERY_MAX_BYTES_PER_SEC_SETTING.getKey(), new ByteSizeValue(RandomNumbers.randomIntBetween(random, 10, 200), ByteSizeUnit.MB));
        }
    }
    if (random.nextBoolean()) {
        builder.put(TransportSettings.PING_SCHEDULE.getKey(), RandomNumbers.randomIntBetween(random, 100, 2000) + "ms");
    }
    if (random.nextBoolean()) {
        String ctx = randomFrom(random, ScriptModule.CORE_CONTEXTS.keySet());
        builder.put(ScriptService.SCRIPT_CACHE_SIZE_SETTING.getConcreteSettingForNamespace(ctx).getKey(), RandomNumbers.randomIntBetween(random, 0, 2000));
    }
    if (random.nextBoolean()) {
        String ctx = randomFrom(random, ScriptModule.CORE_CONTEXTS.keySet());
        builder.put(ScriptService.SCRIPT_CACHE_EXPIRE_SETTING.getConcreteSettingForNamespace(ctx).getKey(), timeValueMillis(RandomNumbers.randomIntBetween(random, 750, 10000000)).getStringRep());
    }
    if (random.nextBoolean()) {
        int initialMillisBound = RandomNumbers.randomIntBetween(random, 10, 100);
        builder.put(TransportReplicationAction.REPLICATION_INITIAL_RETRY_BACKOFF_BOUND.getKey(), timeValueMillis(initialMillisBound));
        int retryTimeoutSeconds = RandomNumbers.randomIntBetween(random, 0, 60);
        builder.put(TransportReplicationAction.REPLICATION_RETRY_TIMEOUT.getKey(), timeValueSeconds(retryTimeoutSeconds));
    }
    return builder.build();
}
Also used : Random(java.util.Random) Builder(org.opensearch.common.settings.Settings.Builder) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue)

Example 10 with Settings.builder

use of org.opensearch.common.settings.Settings.builder in project OpenSearch by opensearch-project.

the class CoordinatorTests method testAppliesNoClusterManagerBlock.

private void testAppliesNoClusterManagerBlock(String noClusterManagerBlockSetting, ClusterBlock expectedBlock) {
    try (Cluster cluster = new Cluster(3)) {
        cluster.runRandomly();
        cluster.stabilise();
        final ClusterNode leader = cluster.getAnyLeader();
        leader.submitUpdateTask("update NO_CLUSTER_MANAGER_BLOCK_SETTING", cs -> {
            final Builder settingsBuilder = Settings.builder().put(cs.metadata().persistentSettings());
            settingsBuilder.put(NO_CLUSTER_MANAGER_BLOCK_SETTING.getKey(), noClusterManagerBlockSetting);
            return ClusterState.builder(cs).metadata(Metadata.builder(cs.metadata()).persistentSettings(settingsBuilder.build())).build();
        }, (source, e) -> {
        });
        cluster.runFor(DEFAULT_CLUSTER_STATE_UPDATE_DELAY, "committing setting update");
        final ClusterNode removedNode = cluster.getAnyNode();
        removedNode.disconnect();
        cluster.runFor(Math.max(defaultMillis(FOLLOWER_CHECK_TIMEOUT_SETTING) + defaultMillis(FOLLOWER_CHECK_INTERVAL_SETTING), defaultMillis(LEADER_CHECK_TIMEOUT_SETTING) + defaultMillis(LEADER_CHECK_INTERVAL_SETTING)) + DEFAULT_CLUSTER_STATE_UPDATE_DELAY, "detecting disconnection");
        assertThat(removedNode.getLastAppliedClusterState().blocks().global(), hasItem(expectedBlock));
        removedNode.close();
        final ClusterNode restartedNode = removedNode.restartedNode();
        cluster.clusterNodes.replaceAll(cn -> cn == removedNode ? restartedNode : cn);
        restartedNode.disconnect();
        cluster.stabilise();
        assertThat(restartedNode.getLastAppliedClusterState().blocks().global(), hasItem(expectedBlock));
    }
}
Also used : ClusterNode(org.opensearch.cluster.coordination.AbstractCoordinatorTestCase.Cluster.ClusterNode) Builder(org.opensearch.common.settings.Settings.Builder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

Builder (org.opensearch.common.settings.Settings.Builder)35 Settings (org.opensearch.common.settings.Settings)11 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 SearchRequestBuilder (org.opensearch.action.search.SearchRequestBuilder)7 QueryBuilder (org.opensearch.index.query.QueryBuilder)7 IndexRequestBuilder (org.opensearch.action.index.IndexRequestBuilder)6 SearchResponse (org.opensearch.action.search.SearchResponse)6 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)6 XContentFactory.jsonBuilder (org.opensearch.common.xcontent.XContentFactory.jsonBuilder)6 QueryRescorerBuilder (org.opensearch.search.rescore.QueryRescorerBuilder)5 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 OpenSearchException (org.opensearch.OpenSearchException)4 HashSet (java.util.HashSet)3 List (java.util.List)3 Map (java.util.Map)3 Set (java.util.Set)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3