Search in sources :

Example 1 with IndexService

use of org.opensearch.index.IndexService in project OpenSearch by opensearch-project.

the class NeedsScoreTests method testNeedsScores.

public void testNeedsScores() {
    IndexService index = createIndex("test", Settings.EMPTY, "type", "d", "type=double");
    Map<ScriptContext<?>, List<Whitelist>> contexts = new HashMap<>();
    contexts.put(NumberSortScript.CONTEXT, Whitelist.BASE_WHITELISTS);
    PainlessScriptEngine service = new PainlessScriptEngine(Settings.EMPTY, contexts);
    QueryShardContext shardContext = index.newQueryShardContext(0, null, () -> 0, null);
    NumberSortScript.Factory factory = service.compile(null, "1.2", NumberSortScript.CONTEXT, Collections.emptyMap());
    NumberSortScript.LeafFactory ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertFalse(ss.needs_score());
    factory = service.compile(null, "doc['d'].value", NumberSortScript.CONTEXT, Collections.emptyMap());
    ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertFalse(ss.needs_score());
    factory = service.compile(null, "1/_score", NumberSortScript.CONTEXT, Collections.emptyMap());
    ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertTrue(ss.needs_score());
    factory = service.compile(null, "doc['d'].value * _score", NumberSortScript.CONTEXT, Collections.emptyMap());
    ss = factory.newFactory(Collections.emptyMap(), shardContext.lookup());
    assertTrue(ss.needs_score());
}
Also used : IndexService(org.opensearch.index.IndexService) HashMap(java.util.HashMap) ScriptContext(org.opensearch.script.ScriptContext) QueryShardContext(org.opensearch.index.query.QueryShardContext) List(java.util.List) NumberSortScript(org.opensearch.script.NumberSortScript)

Example 2 with IndexService

use of org.opensearch.index.IndexService in project OpenSearch by opensearch-project.

the class PercolatorFieldMapperTests method testAllowNoAdditionalSettings.

public void testAllowNoAdditionalSettings() throws Exception {
    addQueryFieldMappings();
    IndexService indexService = createIndex("test1", Settings.EMPTY);
    MapperService mapperService = indexService.mapperService();
    String percolatorMapper = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("doc").startObject("properties").startObject(fieldName).field("type", "percolator").field("index", "no").endObject().endObject().endObject().endObject());
    MapperParsingException e = expectThrows(MapperParsingException.class, () -> mapperService.merge("doc", new CompressedXContent(percolatorMapper), MapperService.MergeReason.MAPPING_UPDATE));
    assertThat(e.getMessage(), containsString("Mapping definition for [" + fieldName + "] has unsupported parameters:  [index : no]"));
}
Also used : MapperParsingException(org.opensearch.index.mapper.MapperParsingException) IndexService(org.opensearch.index.IndexService) CompressedXContent(org.opensearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) MapperService(org.opensearch.index.mapper.MapperService)

Example 3 with IndexService

use of org.opensearch.index.IndexService in project OpenSearch by opensearch-project.

the class RareClusterStateIT method testDelayedMappingPropagationOnReplica.

public void testDelayedMappingPropagationOnReplica() throws Exception {
    // This is essentially the same thing as testDelayedMappingPropagationOnPrimary
    // but for replicas
    // Here we want to test that everything goes well if the mappings that
    // are needed for a document are not available on the replica at the
    // time of indexing it
    final List<String> nodeNames = internalCluster().startNodes(2);
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut());
    final String master = internalCluster().getMasterName();
    assertThat(nodeNames, hasItem(master));
    String otherNode = null;
    for (String node : nodeNames) {
        if (node.equals(master) == false) {
            otherNode = node;
            break;
        }
    }
    assertNotNull(otherNode);
    // Force allocation of the primary on the master node by first only allocating on the master
    // and then allowing all nodes so that the replica gets allocated on the other node
    prepareCreate("index").setSettings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 1).put("index.routing.allocation.include._name", master)).get();
    client().admin().indices().prepareUpdateSettings("index").setSettings(Settings.builder().put("index.routing.allocation.include._name", "")).get();
    ensureGreen();
    // Check routing tables
    ClusterState state = client().admin().cluster().prepareState().get().getState();
    assertEquals(master, state.nodes().getMasterNode().getName());
    List<ShardRouting> shards = state.routingTable().allShards("index");
    assertThat(shards, hasSize(2));
    for (ShardRouting shard : shards) {
        if (shard.primary()) {
            // primary must be on the master
            assertEquals(state.nodes().getMasterNodeId(), shard.currentNodeId());
        } else {
            assertTrue(shard.active());
        }
    }
    // Block cluster state processing on the replica
    BlockClusterStateProcessing disruption = new BlockClusterStateProcessing(otherNode, random());
    internalCluster().setDisruptionScheme(disruption);
    disruption.startDisrupting();
    final ActionFuture<AcknowledgedResponse> putMappingResponse = executeAndCancelCommittedPublication(client().admin().indices().preparePutMapping("index").setSource("field", "type=long"));
    final Index index = resolveIndex("index");
    // Wait for mappings to be available on master
    assertBusy(() -> {
        final IndicesService indicesService = internalCluster().getInstance(IndicesService.class, master);
        final IndexService indexService = indicesService.indexServiceSafe(index);
        assertNotNull(indexService);
        final MapperService mapperService = indexService.mapperService();
        DocumentMapper mapper = mapperService.documentMapper(MapperService.SINGLE_MAPPING_NAME);
        assertNotNull(mapper);
        assertNotNull(mapper.mappers().getMapper("field"));
    });
    final ActionFuture<IndexResponse> docIndexResponse = client().prepareIndex("index").setId("1").setSource("field", 42).execute();
    assertBusy(() -> assertTrue(client().prepareGet("index", "1").get().isExists()));
    // index another document, this time using dynamic mappings.
    // The ack timeout of 0 on dynamic mapping updates makes it possible for the document to be indexed on the primary, even
    // if the dynamic mapping update is not applied on the replica yet.
    // this request does not change the cluster state, because the mapping is dynamic,
    // we need to await and cancel committed publication
    ActionFuture<IndexResponse> dynamicMappingsFut = executeAndCancelCommittedPublication(client().prepareIndex("index").setId("2").setSource("field2", 42));
    // ...and wait for second mapping to be available on master
    assertBusy(() -> {
        final IndicesService indicesService = internalCluster().getInstance(IndicesService.class, master);
        final IndexService indexService = indicesService.indexServiceSafe(index);
        assertNotNull(indexService);
        final MapperService mapperService = indexService.mapperService();
        DocumentMapper mapper = mapperService.documentMapper(MapperService.SINGLE_MAPPING_NAME);
        assertNotNull(mapper);
        assertNotNull(mapper.mappers().getMapper("field2"));
    });
    assertBusy(() -> assertTrue(client().prepareGet("index", "2").get().isExists()));
    // The mappings have not been propagated to the replica yet as a consequence the document count not be indexed
    // We wait on purpose to make sure that the document is not indexed because the shard operation is stalled
    // and not just because it takes time to replicate the indexing request to the replica
    Thread.sleep(100);
    assertFalse(putMappingResponse.isDone());
    assertFalse(docIndexResponse.isDone());
    // Now make sure the indexing request finishes successfully
    disruption.stopDisrupting();
    assertTrue(putMappingResponse.get(10, TimeUnit.SECONDS).isAcknowledged());
    assertThat(docIndexResponse.get(10, TimeUnit.SECONDS), instanceOf(IndexResponse.class));
    // both shards should have succeeded
    assertEquals(2, docIndexResponse.get(10, TimeUnit.SECONDS).getShardInfo().getTotal());
    assertThat(dynamicMappingsFut.get(10, TimeUnit.SECONDS).getResult(), equalTo(CREATED));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) BlockClusterStateProcessing(org.opensearch.test.disruption.BlockClusterStateProcessing) IndexService(org.opensearch.index.IndexService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) IndicesService(org.opensearch.indices.IndicesService) Index(org.opensearch.index.Index) IndexResponse(org.opensearch.action.index.IndexResponse) ShardRouting(org.opensearch.cluster.routing.ShardRouting) MapperService(org.opensearch.index.mapper.MapperService)

Example 4 with IndexService

use of org.opensearch.index.IndexService in project OpenSearch by opensearch-project.

the class PainlessExecuteApiTests method testFilterExecutionContext.

public void testFilterExecutionContext() throws IOException {
    ScriptService scriptService = getInstanceFromNode(ScriptService.class);
    IndexService indexService = createIndex("index", Settings.EMPTY, "doc", "field", "type=long");
    Request.ContextSetup contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null);
    contextSetup.setXContentType(XContentType.JSON);
    Request request = new Request(new Script("doc['field'].value >= 3"), "filter", contextSetup);
    Response response = innerShardOperation(request, scriptService, indexService);
    assertThat(response.getResult(), equalTo(true));
    contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 3}"), null);
    contextSetup.setXContentType(XContentType.JSON);
    request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), "filter", contextSetup);
    response = innerShardOperation(request, scriptService, indexService);
    assertThat(response.getResult(), equalTo(true));
    contextSetup = new Request.ContextSetup("index", new BytesArray("{\"field\": 2}"), null);
    contextSetup.setXContentType(XContentType.JSON);
    request = new Request(new Script(ScriptType.INLINE, "painless", "doc['field'].value >= params.max", singletonMap("max", 3)), "filter", contextSetup);
    response = innerShardOperation(request, scriptService, indexService);
    assertThat(response.getResult(), equalTo(false));
}
Also used : ScriptService(org.opensearch.script.ScriptService) Response(org.opensearch.painless.action.PainlessExecuteAction.Response) Script(org.opensearch.script.Script) BytesArray(org.opensearch.common.bytes.BytesArray) IndexService(org.opensearch.index.IndexService) Request(org.opensearch.painless.action.PainlessExecuteAction.Request)

Example 5 with IndexService

use of org.opensearch.index.IndexService in project OpenSearch by opensearch-project.

the class SizeMappingTests method testSizeNotSet.

public void testSizeNotSet() throws Exception {
    IndexService service = createIndex("test", Settings.EMPTY, "type");
    DocumentMapper docMapper = service.mapperService().documentMapper("type");
    BytesReference source = BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("field", "value").endObject());
    ParsedDocument doc = docMapper.parse(new SourceToParse("test", "type", "1", source, XContentType.JSON));
    assertThat(doc.rootDoc().getField("_size"), nullValue());
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IndexService(org.opensearch.index.IndexService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) SourceToParse(org.opensearch.index.mapper.SourceToParse)

Aggregations

IndexService (org.opensearch.index.IndexService)194 IndexShard (org.opensearch.index.shard.IndexShard)75 IndicesService (org.opensearch.indices.IndicesService)72 Settings (org.opensearch.common.settings.Settings)42 IndexSettings (org.opensearch.index.IndexSettings)37 ShardId (org.opensearch.index.shard.ShardId)34 Matchers.containsString (org.hamcrest.Matchers.containsString)29 IOException (java.io.IOException)28 Index (org.opensearch.index.Index)28 Engine (org.opensearch.index.engine.Engine)28 CompressedXContent (org.opensearch.common.compress.CompressedXContent)27 QueryShardContext (org.opensearch.index.query.QueryShardContext)21 ClusterService (org.opensearch.cluster.service.ClusterService)20 ActionListener (org.opensearch.action.ActionListener)17 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)17 CountDownLatch (java.util.concurrent.CountDownLatch)14 TimeValue (org.opensearch.common.unit.TimeValue)14 DocumentMapper (org.opensearch.index.mapper.DocumentMapper)14 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)13 ShardRouting (org.opensearch.cluster.routing.ShardRouting)13