Search in sources :

Example 1 with MapperService

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

the class ExpressionNumberSortScriptTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    NumberFieldType fieldType = new NumberFieldType("field", NumberType.DOUBLE);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.fieldType("field")).thenReturn(fieldType);
    when(mapperService.fieldType("alias")).thenReturn(fieldType);
    SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
    when(doubleValues.advanceExact(anyInt())).thenReturn(true);
    when(doubleValues.nextValue()).thenReturn(2.718);
    LeafNumericFieldData atomicFieldData = mock(LeafNumericFieldData.class);
    when(atomicFieldData.getDoubleValues()).thenReturn(doubleValues);
    IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
    when(fieldData.getFieldName()).thenReturn("field");
    when(fieldData.load(any())).thenReturn(atomicFieldData);
    service = new ExpressionScriptEngine();
    lookup = new SearchLookup(mapperService, (ignored, lookup) -> fieldData);
}
Also used : NumberSortScript(org.opensearch.script.NumberSortScript) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) SearchLookup(org.opensearch.search.lookup.SearchLookup) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) ScriptException(org.opensearch.script.ScriptException) MapperService(org.opensearch.index.mapper.MapperService) NumberType(org.opensearch.index.mapper.NumberFieldMapper.NumberType) Mockito.anyInt(org.mockito.Mockito.anyInt) ParseException(java.text.ParseException) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) NumberFieldType(org.opensearch.index.mapper.NumberFieldMapper.NumberFieldType) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) MapperService(org.opensearch.index.mapper.MapperService) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 2 with MapperService

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

the class ExpressionFieldScriptTests method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    NumberFieldMapper.NumberFieldType fieldType = new NumberFieldMapper.NumberFieldType("field", NumberFieldMapper.NumberType.DOUBLE);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.fieldType("field")).thenReturn(fieldType);
    when(mapperService.fieldType("alias")).thenReturn(fieldType);
    SortedNumericDoubleValues doubleValues = mock(SortedNumericDoubleValues.class);
    when(doubleValues.advanceExact(anyInt())).thenReturn(true);
    when(doubleValues.nextValue()).thenReturn(2.718);
    LeafNumericFieldData atomicFieldData = mock(LeafNumericFieldData.class);
    when(atomicFieldData.getDoubleValues()).thenReturn(doubleValues);
    IndexNumericFieldData fieldData = mock(IndexNumericFieldData.class);
    when(fieldData.getFieldName()).thenReturn("field");
    when(fieldData.load(any())).thenReturn(atomicFieldData);
    service = new ExpressionScriptEngine();
    lookup = new SearchLookup(mapperService, (ignored, lookup) -> fieldData);
}
Also used : IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) FieldScript(org.opensearch.script.FieldScript) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) SearchLookup(org.opensearch.search.lookup.SearchLookup) ScriptException(org.opensearch.script.ScriptException) MapperService(org.opensearch.index.mapper.MapperService) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Mockito.anyInt(org.mockito.Mockito.anyInt) ParseException(java.text.ParseException) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) NumberFieldMapper(org.opensearch.index.mapper.NumberFieldMapper) LeafNumericFieldData(org.opensearch.index.fielddata.LeafNumericFieldData) IndexNumericFieldData(org.opensearch.index.fielddata.IndexNumericFieldData) SortedNumericDoubleValues(org.opensearch.index.fielddata.SortedNumericDoubleValues) MapperService(org.opensearch.index.mapper.MapperService) SearchLookup(org.opensearch.search.lookup.SearchLookup)

Example 3 with MapperService

use of org.opensearch.index.mapper.MapperService 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 4 with MapperService

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

the class AnnotatedTextFieldMapperTests method testToleranceForBadAnnotationMarkup.

public void testToleranceForBadAnnotationMarkup() throws IOException {
    MapperService mapperService = createMapperService(fieldMapping(this::minimalMapping));
    String annotatedText = "foo [bar](MissingEndBracket baz";
    ParsedDocument doc = mapperService.documentMapper().parse(source(b -> b.field("field", annotatedText)));
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    assertEquals(annotatedText, fields[0].stringValue());
    withLuceneIndex(mapperService, iw -> iw.addDocument(doc.rootDoc()), reader -> {
        LeafReader leaf = reader.leaves().get(0).reader();
        TermsEnum terms = leaf.terms("field").iterator();
        assertTrue(terms.seekExact(new BytesRef("foo")));
        PostingsEnum postings = terms.postings(null, PostingsEnum.POSITIONS);
        assertEquals(0, postings.nextDoc());
        assertEquals(0, postings.nextPosition());
        assertTrue(terms.seekExact(new BytesRef("bar")));
        postings = terms.postings(null, PostingsEnum.POSITIONS);
        assertEquals(0, postings.nextDoc());
        assertEquals(1, postings.nextPosition());
        assertFalse(terms.seekExact(new BytesRef("MissingEndBracket")));
        // Bad markup means value is treated as plain text and fed through tokenisation
        assertTrue(terms.seekExact(new BytesRef("missingendbracket")));
    });
}
Also used : Arrays(java.util.Arrays) IndexableField(org.apache.lucene.index.IndexableField) ToXContent(org.opensearch.common.xcontent.ToXContent) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) TokenFilterFactory(org.opensearch.index.analysis.TokenFilterFactory) HashMap(java.util.HashMap) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) MapperService(org.opensearch.index.mapper.MapperService) IndexableFieldType(org.apache.lucene.index.IndexableFieldType) TermsEnum(org.apache.lucene.index.TermsEnum) CharFilterFactory(org.opensearch.index.analysis.CharFilterFactory) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) StandardTokenizerFactory(org.opensearch.index.analysis.StandardTokenizerFactory) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) CustomAnalyzer(org.opensearch.index.analysis.CustomAnalyzer) EnglishAnalyzer(org.apache.lucene.analysis.en.EnglishAnalyzer) AnnotatedTextPlugin(org.opensearch.plugin.mapper.AnnotatedTextPlugin) StopFilter(org.apache.lucene.analysis.StopFilter) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) PostingsEnum(org.apache.lucene.index.PostingsEnum) Terms(org.apache.lucene.index.Terms) TokenStream(org.apache.lucene.analysis.TokenStream) BytesRef(org.apache.lucene.util.BytesRef) MapperTestCase(org.opensearch.index.mapper.MapperTestCase) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Plugin(org.opensearch.plugins.Plugin) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) DocValuesType(org.apache.lucene.index.DocValuesType) Matchers.equalTo(org.hamcrest.Matchers.equalTo) LeafReader(org.apache.lucene.index.LeafReader) IndexSettings(org.opensearch.index.IndexSettings) IndexOptions(org.apache.lucene.index.IndexOptions) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) LeafReader(org.apache.lucene.index.LeafReader) Matchers.containsString(org.hamcrest.Matchers.containsString) PostingsEnum(org.apache.lucene.index.PostingsEnum) MapperService(org.opensearch.index.mapper.MapperService) BytesRef(org.apache.lucene.util.BytesRef) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 5 with MapperService

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

the class AnnotatedTextFieldMapperTests method testAnnotationInjection.

public void testAnnotationInjection() throws IOException {
    MapperService mapperService = createMapperService(fieldMapping(this::minimalMapping));
    // Use example of typed and untyped annotations
    String annotatedText = "He paid [Stormy Daniels](Stephanie+Clifford&Payee) hush money";
    ParsedDocument doc = mapperService.documentMapper().parse(source(b -> b.field("field", annotatedText)));
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    assertEquals(annotatedText, fields[0].stringValue());
    withLuceneIndex(mapperService, iw -> iw.addDocument(doc.rootDoc()), reader -> {
        LeafReader leaf = reader.leaves().get(0).reader();
        TermsEnum terms = leaf.terms("field").iterator();
        assertTrue(terms.seekExact(new BytesRef("stormy")));
        PostingsEnum postings = terms.postings(null, PostingsEnum.POSITIONS);
        assertEquals(0, postings.nextDoc());
        assertEquals(2, postings.nextPosition());
        assertTrue(terms.seekExact(new BytesRef("Stephanie Clifford")));
        postings = terms.postings(null, PostingsEnum.POSITIONS);
        assertEquals(0, postings.nextDoc());
        assertEquals(2, postings.nextPosition());
        assertTrue(terms.seekExact(new BytesRef("Payee")));
        postings = terms.postings(null, PostingsEnum.POSITIONS);
        assertEquals(0, postings.nextDoc());
        assertEquals(2, postings.nextPosition());
        assertTrue(terms.seekExact(new BytesRef("hush")));
        postings = terms.postings(null, PostingsEnum.POSITIONS);
        assertEquals(0, postings.nextDoc());
        assertEquals(4, postings.nextPosition());
    });
}
Also used : Arrays(java.util.Arrays) IndexableField(org.apache.lucene.index.IndexableField) ToXContent(org.opensearch.common.xcontent.ToXContent) MapperParsingException(org.opensearch.index.mapper.MapperParsingException) TokenFilterFactory(org.opensearch.index.analysis.TokenFilterFactory) HashMap(java.util.HashMap) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) Strings(org.opensearch.common.Strings) HashSet(java.util.HashSet) MapperService(org.opensearch.index.mapper.MapperService) IndexableFieldType(org.apache.lucene.index.IndexableFieldType) TermsEnum(org.apache.lucene.index.TermsEnum) CharFilterFactory(org.opensearch.index.analysis.CharFilterFactory) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) StandardTokenizerFactory(org.opensearch.index.analysis.StandardTokenizerFactory) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) CustomAnalyzer(org.opensearch.index.analysis.CustomAnalyzer) EnglishAnalyzer(org.apache.lucene.analysis.en.EnglishAnalyzer) AnnotatedTextPlugin(org.opensearch.plugin.mapper.AnnotatedTextPlugin) StopFilter(org.apache.lucene.analysis.StopFilter) NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) PostingsEnum(org.apache.lucene.index.PostingsEnum) Terms(org.apache.lucene.index.Terms) TokenStream(org.apache.lucene.analysis.TokenStream) BytesRef(org.apache.lucene.util.BytesRef) MapperTestCase(org.opensearch.index.mapper.MapperTestCase) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) Plugin(org.opensearch.plugins.Plugin) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) TextFieldMapper(org.opensearch.index.mapper.TextFieldMapper) DocValuesType(org.apache.lucene.index.DocValuesType) Matchers.equalTo(org.hamcrest.Matchers.equalTo) LeafReader(org.apache.lucene.index.LeafReader) IndexSettings(org.opensearch.index.IndexSettings) IndexOptions(org.apache.lucene.index.IndexOptions) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) LeafReader(org.apache.lucene.index.LeafReader) Matchers.containsString(org.hamcrest.Matchers.containsString) PostingsEnum(org.apache.lucene.index.PostingsEnum) MapperService(org.opensearch.index.mapper.MapperService) BytesRef(org.apache.lucene.util.BytesRef) TermsEnum(org.apache.lucene.index.TermsEnum)

Aggregations

MapperService (org.opensearch.index.mapper.MapperService)86 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)36 Settings (org.opensearch.common.settings.Settings)27 IndexSettings (org.opensearch.index.IndexSettings)22 IOException (java.io.IOException)19 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)19 DocumentMapper (org.opensearch.index.mapper.DocumentMapper)18 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)18 DocumentField (org.opensearch.common.document.DocumentField)16 Collections (java.util.Collections)15 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)15 HashMap (java.util.HashMap)14 Map (java.util.Map)14 HashSet (java.util.HashSet)13 IndexService (org.opensearch.index.IndexService)13 Matchers.containsString (org.hamcrest.Matchers.containsString)12 Set (java.util.Set)11 IndexableField (org.apache.lucene.index.IndexableField)11 Store (org.opensearch.index.store.Store)11 AtomicLong (java.util.concurrent.atomic.AtomicLong)9