Search in sources :

Example 1 with MapperException

use of org.opensearch.index.mapper.MapperException in project OpenSearch by opensearch-project.

the class ParentJoinFieldMapperTests method testMultipleLevels.

public void testMultipleLevels() throws Exception {
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("join_field").field("type", "join").startObject("relations").field("parent", "child").field("child", "grand_child").endObject().endObject().endObject().endObject());
    IndexService service = createIndex("test");
    DocumentMapper docMapper = service.mapperService().merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
    assertTrue(docMapper.mappers().getMapper("join_field") == ParentJoinFieldMapper.getMapper(service.mapperService()));
    // Doc without join
    ParsedDocument doc = docMapper.parse(new SourceToParse("test", "type", "0", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), XContentType.JSON));
    assertNull(doc.rootDoc().getBinaryValue("join_field"));
    // Doc parent
    doc = docMapper.parse(new SourceToParse("test", "type", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "parent").endObject()), XContentType.JSON));
    assertEquals("1", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("parent", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Doc child
    doc = docMapper.parse(new SourceToParse("test", "type", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "child").field("parent", "1").endObject().endObject()), XContentType.JSON, "1"));
    assertEquals("1", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("2", doc.rootDoc().getBinaryValue("join_field#child").utf8ToString());
    assertEquals("child", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Doc child missing parent
    MapperException exc = expectThrows(MapperParsingException.class, () -> docMapper.parse(new SourceToParse("test", "type", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "child").endObject()), XContentType.JSON, "1")));
    assertThat(exc.getRootCause().getMessage(), containsString("[parent] is missing for join field [join_field]"));
    // Doc child missing routing
    exc = expectThrows(MapperParsingException.class, () -> docMapper.parse(new SourceToParse("test", "type", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "child").field("parent", "1").endObject().endObject()), XContentType.JSON)));
    assertThat(exc.getRootCause().getMessage(), containsString("[routing] is missing for join field [join_field]"));
    // Doc grand_child
    doc = docMapper.parse(new SourceToParse("test", "type", "3", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "grand_child").field("parent", "2").endObject().endObject()), XContentType.JSON, "1"));
    assertEquals("2", doc.rootDoc().getBinaryValue("join_field#child").utf8ToString());
    assertEquals("grand_child", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Unknown join name
    exc = expectThrows(MapperParsingException.class, () -> docMapper.parse(new SourceToParse("test", "type", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "unknown").endObject()), XContentType.JSON)));
    assertThat(exc.getRootCause().getMessage(), containsString("unknown join name [unknown] for field [join_field]"));
}
Also used : MapperParsingException(org.opensearch.index.mapper.MapperParsingException) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IndexService(org.opensearch.index.IndexService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) MapperException(org.opensearch.index.mapper.MapperException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with MapperException

use of org.opensearch.index.mapper.MapperException in project OpenSearch by opensearch-project.

the class TransportShardBulkAction method dispatchedShardOperationOnPrimary.

@Override
protected void dispatchedShardOperationOnPrimary(BulkShardRequest request, IndexShard primary, ActionListener<PrimaryResult<BulkShardRequest, BulkShardResponse>> listener) {
    ClusterStateObserver observer = new ClusterStateObserver(clusterService, request.timeout(), logger, threadPool.getThreadContext());
    performOnPrimary(request, primary, updateHelper, threadPool::absoluteTimeInMillis, (update, shardId, mappingListener) -> {
        assert update != null;
        assert shardId != null;
        mappingUpdatedAction.updateMappingOnMaster(shardId.getIndex(), update, mappingListener);
    }, mappingUpdateListener -> observer.waitForNextChange(new ClusterStateObserver.Listener() {

        @Override
        public void onNewClusterState(ClusterState state) {
            mappingUpdateListener.onResponse(null);
        }

        @Override
        public void onClusterServiceClose() {
            mappingUpdateListener.onFailure(new NodeClosedException(clusterService.localNode()));
        }

        @Override
        public void onTimeout(TimeValue timeout) {
            mappingUpdateListener.onFailure(new MapperException("timed out while waiting for a dynamic mapping update"));
        }
    }), listener, threadPool, executor(primary));
}
Also used : ClusterState(org.opensearch.cluster.ClusterState) ClusterStateObserver(org.opensearch.cluster.ClusterStateObserver) ActionListener(org.opensearch.action.ActionListener) NodeClosedException(org.opensearch.node.NodeClosedException) MapperException(org.opensearch.index.mapper.MapperException) TimeValue(org.opensearch.common.unit.TimeValue)

Example 3 with MapperException

use of org.opensearch.index.mapper.MapperException in project OpenSearch by opensearch-project.

the class ParentJoinFieldMapperTests method testSingleLevel.

public void testSingleLevel() throws Exception {
    String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("join_field").field("type", "join").startObject("relations").field("parent", "child").endObject().endObject().endObject().endObject());
    IndexService service = createIndex("test");
    DocumentMapper docMapper = service.mapperService().merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
    assertTrue(docMapper.mappers().getMapper("join_field") == ParentJoinFieldMapper.getMapper(service.mapperService()));
    // Doc without join
    ParsedDocument doc = docMapper.parse(new SourceToParse("test", "type", "0", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().endObject()), XContentType.JSON));
    assertNull(doc.rootDoc().getBinaryValue("join_field"));
    // Doc parent
    doc = docMapper.parse(new SourceToParse("test", "type", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "parent").endObject()), XContentType.JSON));
    assertEquals("1", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("parent", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Doc child
    doc = docMapper.parse(new SourceToParse("test", "type", "2", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().startObject("join_field").field("name", "child").field("parent", "1").endObject().endObject()), XContentType.JSON, "1"));
    assertEquals("1", doc.rootDoc().getBinaryValue("join_field#parent").utf8ToString());
    assertEquals("child", doc.rootDoc().getBinaryValue("join_field").utf8ToString());
    // Unknown join name
    MapperException exc = expectThrows(MapperParsingException.class, () -> docMapper.parse(new SourceToParse("test", "type", "1", BytesReference.bytes(XContentFactory.jsonBuilder().startObject().field("join_field", "unknown").endObject()), XContentType.JSON)));
    assertThat(exc.getRootCause().getMessage(), containsString("unknown join name [unknown] for field [join_field]"));
}
Also used : ParsedDocument(org.opensearch.index.mapper.ParsedDocument) IndexService(org.opensearch.index.IndexService) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) CompressedXContent(org.opensearch.common.compress.CompressedXContent) SourceToParse(org.opensearch.index.mapper.SourceToParse) MapperException(org.opensearch.index.mapper.MapperException) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 4 with MapperException

use of org.opensearch.index.mapper.MapperException in project OpenSearch by opensearch-project.

the class NamedAnalyzerTests method testCheckAllowedInMode.

public void testCheckAllowedInMode() {
    try (NamedAnalyzer testAnalyzer = new NamedAnalyzer("my_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", AnalysisMode.INDEX_TIME), Integer.MIN_VALUE)) {
        testAnalyzer.checkAllowedInMode(AnalysisMode.INDEX_TIME);
        MapperException ex = expectThrows(MapperException.class, () -> testAnalyzer.checkAllowedInMode(AnalysisMode.SEARCH_TIME));
        assertEquals("analyzer [my_analyzer] contains filters [my_analyzer] that are not allowed to run in search time mode.", ex.getMessage());
        ex = expectThrows(MapperException.class, () -> testAnalyzer.checkAllowedInMode(AnalysisMode.ALL));
        assertEquals("analyzer [my_analyzer] contains filters [my_analyzer] that are not allowed to run in all mode.", ex.getMessage());
    }
    try (NamedAnalyzer testAnalyzer = new NamedAnalyzer("my_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", AnalysisMode.SEARCH_TIME), Integer.MIN_VALUE)) {
        testAnalyzer.checkAllowedInMode(AnalysisMode.SEARCH_TIME);
        MapperException ex = expectThrows(MapperException.class, () -> testAnalyzer.checkAllowedInMode(AnalysisMode.INDEX_TIME));
        assertEquals("analyzer [my_analyzer] contains filters [my_analyzer] that are not allowed to run in index time mode.", ex.getMessage());
        ex = expectThrows(MapperException.class, () -> testAnalyzer.checkAllowedInMode(AnalysisMode.ALL));
        assertEquals("analyzer [my_analyzer] contains filters [my_analyzer] that are not allowed to run in all mode.", ex.getMessage());
    }
    try (NamedAnalyzer testAnalyzer = new NamedAnalyzer("my_analyzer", AnalyzerScope.INDEX, createAnalyzerWithMode("my_analyzer", AnalysisMode.ALL), Integer.MIN_VALUE)) {
        testAnalyzer.checkAllowedInMode(AnalysisMode.ALL);
        testAnalyzer.checkAllowedInMode(AnalysisMode.INDEX_TIME);
        testAnalyzer.checkAllowedInMode(AnalysisMode.SEARCH_TIME);
    }
}
Also used : MapperException(org.opensearch.index.mapper.MapperException)

Example 5 with MapperException

use of org.opensearch.index.mapper.MapperException in project OpenSearch by opensearch-project.

the class AnalysisRegistryTests method testOverrideDefaultAnalyzerWithoutAnalysisModeAll.

public void testOverrideDefaultAnalyzerWithoutAnalysisModeAll() throws IOException {
    Version version = VersionUtils.randomVersion(random());
    Settings settings = Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, version).build();
    IndexSettings indexSettings = IndexSettingsModule.newIndexSettings("index", settings);
    TokenFilterFactory tokenFilter = new AbstractTokenFilterFactory(indexSettings, "my_filter", Settings.EMPTY) {

        @Override
        public AnalysisMode getAnalysisMode() {
            return randomFrom(AnalysisMode.SEARCH_TIME, AnalysisMode.INDEX_TIME);
        }

        @Override
        public TokenStream create(TokenStream tokenStream) {
            return tokenStream;
        }
    };
    TokenizerFactory tokenizer = new AbstractTokenizerFactory(indexSettings, Settings.EMPTY, "my_tokenizer") {

        @Override
        public Tokenizer create() {
            return new StandardTokenizer();
        }
    };
    Analyzer analyzer = new CustomAnalyzer(tokenizer, new CharFilterFactory[0], new TokenFilterFactory[] { tokenFilter });
    MapperException ex = expectThrows(MapperException.class, () -> emptyRegistry.build(IndexSettingsModule.newIndexSettings("index", settings), singletonMap("default", new PreBuiltAnalyzerProvider("default", AnalyzerScope.INDEX, analyzer)), emptyMap(), emptyMap(), emptyMap(), emptyMap()));
    assertEquals("analyzer [default] contains filters [my_filter] that are not allowed to run in all mode.", ex.getMessage());
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) IndexSettings(org.opensearch.index.IndexSettings) MapperException(org.opensearch.index.mapper.MapperException) EnglishAnalyzer(org.apache.lucene.analysis.en.EnglishAnalyzer) Analyzer(org.apache.lucene.analysis.Analyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Version(org.opensearch.Version) StandardTokenizer(org.apache.lucene.analysis.standard.StandardTokenizer) Settings(org.opensearch.common.settings.Settings) IndexSettings(org.opensearch.index.IndexSettings)

Aggregations

MapperException (org.opensearch.index.mapper.MapperException)5 Matchers.containsString (org.hamcrest.Matchers.containsString)2 CompressedXContent (org.opensearch.common.compress.CompressedXContent)2 IndexService (org.opensearch.index.IndexService)2 DocumentMapper (org.opensearch.index.mapper.DocumentMapper)2 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)2 SourceToParse (org.opensearch.index.mapper.SourceToParse)2 Analyzer (org.apache.lucene.analysis.Analyzer)1 TokenStream (org.apache.lucene.analysis.TokenStream)1 EnglishAnalyzer (org.apache.lucene.analysis.en.EnglishAnalyzer)1 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)1 StandardTokenizer (org.apache.lucene.analysis.standard.StandardTokenizer)1 Version (org.opensearch.Version)1 ActionListener (org.opensearch.action.ActionListener)1 ClusterState (org.opensearch.cluster.ClusterState)1 ClusterStateObserver (org.opensearch.cluster.ClusterStateObserver)1 Settings (org.opensearch.common.settings.Settings)1 TimeValue (org.opensearch.common.unit.TimeValue)1 IndexSettings (org.opensearch.index.IndexSettings)1 MapperParsingException (org.opensearch.index.mapper.MapperParsingException)1