Search in sources :

Example 1 with DocumentMapper

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

the class PercolatorFieldMapperTests method testExtractTerms.

public void testExtractTerms() throws Exception {
    addQueryFieldMappings();
    BooleanQuery.Builder bq = new BooleanQuery.Builder();
    TermQuery termQuery1 = new TermQuery(new Term("field", "term1"));
    bq.add(termQuery1, Occur.SHOULD);
    TermQuery termQuery2 = new TermQuery(new Term("field", "term2"));
    bq.add(termQuery2, Occur.SHOULD);
    DocumentMapper documentMapper = mapperService.documentMapper("doc");
    PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
    IndexMetadata build = IndexMetadata.builder("").settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).build();
    IndexSettings settings = new IndexSettings(build, Settings.EMPTY);
    ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(settings, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(bq.build(), parseContext);
    ParseContext.Document document = parseContext.doc();
    PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType();
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE));
    List<IndexableField> fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name())));
    fields.sort(Comparator.comparing(IndexableField::binaryValue));
    assertThat(fields.size(), equalTo(2));
    assertThat(fields.get(0).binaryValue().utf8ToString(), equalTo("field\u0000term1"));
    assertThat(fields.get(1).binaryValue().utf8ToString(), equalTo("field\u0000term2"));
    fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.minimumShouldMatchField.name())));
    assertThat(fields.size(), equalTo(1));
    assertThat(fields.get(0).numericValue(), equalTo(1L));
    // Now test conjunction:
    bq = new BooleanQuery.Builder();
    bq.add(termQuery1, Occur.MUST);
    bq.add(termQuery2, Occur.MUST);
    parseContext = new ParseContext.InternalParseContext(settings, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(bq.build(), parseContext);
    document = parseContext.doc();
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_COMPLETE));
    fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.queryTermsField.name())));
    fields.sort(Comparator.comparing(IndexableField::binaryValue));
    assertThat(fields.size(), equalTo(2));
    assertThat(fields.get(0).binaryValue().utf8ToString(), equalTo("field\u0000term1"));
    assertThat(fields.get(1).binaryValue().utf8ToString(), equalTo("field\u0000term2"));
    fields = new ArrayList<>(Arrays.asList(document.getFields(fieldType.minimumShouldMatchField.name())));
    assertThat(fields.size(), equalTo(1));
    assertThat(fields.get(0).numericValue(), equalTo(2L));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) BoostingQueryBuilder(org.opensearch.index.query.BoostingQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) XContentFactory.jsonBuilder(org.opensearch.common.xcontent.XContentFactory.jsonBuilder) HasChildQueryBuilder(org.opensearch.join.query.HasChildQueryBuilder) ConstantScoreQueryBuilder(org.opensearch.index.query.ConstantScoreQueryBuilder) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) RandomScoreFunctionBuilder(org.opensearch.index.query.functionscore.RandomScoreFunctionBuilder) FunctionScoreQueryBuilder(org.opensearch.index.query.functionscore.FunctionScoreQueryBuilder) ScriptScoreFunctionBuilder(org.opensearch.index.query.functionscore.ScriptScoreFunctionBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) DisMaxQueryBuilder(org.opensearch.index.query.DisMaxQueryBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) HasParentQueryBuilder(org.opensearch.join.query.HasParentQueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexSettings(org.opensearch.index.IndexSettings) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) IndexableField(org.apache.lucene.index.IndexableField) ParseContext(org.opensearch.index.mapper.ParseContext) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata)

Example 2 with DocumentMapper

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

the class PercolatorFieldMapperTests method testExtractTermsAndRanges_failed.

public void testExtractTermsAndRanges_failed() throws Exception {
    addQueryFieldMappings();
    TermRangeQuery query = new TermRangeQuery("field1", new BytesRef("a"), new BytesRef("z"), true, true);
    DocumentMapper documentMapper = mapperService.documentMapper("doc");
    PercolatorFieldMapper fieldMapper = (PercolatorFieldMapper) documentMapper.mappers().getMapper(fieldName);
    IndexMetadata build = IndexMetadata.builder("").settings(Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT)).numberOfShards(1).numberOfReplicas(0).build();
    IndexSettings settings = new IndexSettings(build, Settings.EMPTY);
    ParseContext.InternalParseContext parseContext = new ParseContext.InternalParseContext(settings, mapperService.documentMapperParser(), documentMapper, null, null);
    fieldMapper.processQuery(query, parseContext);
    ParseContext.Document document = parseContext.doc();
    PercolatorFieldMapper.PercolatorFieldType fieldType = (PercolatorFieldMapper.PercolatorFieldType) fieldMapper.fieldType();
    assertThat(document.getFields().size(), equalTo(1));
    assertThat(document.getField(fieldType.extractionResultField.name()).stringValue(), equalTo(EXTRACTION_FAILED));
}
Also used : TermRangeQuery(org.apache.lucene.search.TermRangeQuery) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexSettings(org.opensearch.index.IndexSettings) ParseContext(org.opensearch.index.mapper.ParseContext) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) BytesRef(org.apache.lucene.util.BytesRef)

Example 3 with DocumentMapper

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

the class Murmur3FieldMapperTests method testDefaults.

public void testDefaults() throws Exception {
    DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
    ParsedDocument parsedDoc = mapper.parse(source(b -> b.field("field", "value")));
    IndexableField[] fields = parsedDoc.rootDoc().getFields("field");
    assertNotNull(fields);
    assertEquals(Arrays.toString(fields), 1, fields.length);
    IndexableField field = fields[0];
    assertEquals(IndexOptions.NONE, field.fieldType().indexOptions());
    assertEquals(DocValuesType.SORTED_NUMERIC, field.fieldType().docValuesType());
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) Arrays(java.util.Arrays) DocValuesType(org.apache.lucene.index.DocValuesType) IndexableField(org.apache.lucene.index.IndexableField) MapperTestCase(org.opensearch.index.mapper.MapperTestCase) Collection(java.util.Collection) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) MapperMurmur3Plugin(org.opensearch.plugin.mapper.MapperMurmur3Plugin) IOException(java.io.IOException) IndexOptions(org.apache.lucene.index.IndexOptions) Plugin(org.opensearch.plugins.Plugin) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) DocumentMapper(org.opensearch.index.mapper.DocumentMapper)

Example 4 with DocumentMapper

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

the class AnnotatedTextFieldMapperTests method testDefaults.

public void testDefaults() throws IOException {
    DocumentMapper mapper = createDocumentMapper(fieldMapping(this::minimalMapping));
    ParsedDocument doc = mapper.parse(source(b -> b.field("field", "1234")));
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    assertEquals("1234", fields[0].stringValue());
    IndexableFieldType fieldType = fields[0].fieldType();
    assertThat(fieldType.omitNorms(), equalTo(false));
    assertTrue(fieldType.tokenized());
    assertFalse(fieldType.stored());
    assertThat(fieldType.indexOptions(), equalTo(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS));
    assertThat(fieldType.storeTermVectors(), equalTo(false));
    assertThat(fieldType.storeTermVectorOffsets(), equalTo(false));
    assertThat(fieldType.storeTermVectorPositions(), equalTo(false));
    assertThat(fieldType.storeTermVectorPayloads(), equalTo(false));
    assertEquals(DocValuesType.NONE, fieldType.docValuesType());
}
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) DocumentMapper(org.opensearch.index.mapper.DocumentMapper) IndexableFieldType(org.apache.lucene.index.IndexableFieldType)

Example 5 with DocumentMapper

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

the class AnnotatedTextFieldMapperTests method testDisableNorms.

public void testDisableNorms() throws IOException {
    DocumentMapper mapper = createDocumentMapper(fieldMapping(b -> {
        b.field("type", "annotated_text");
        b.field("norms", false);
    }));
    ParsedDocument doc = mapper.parse(source(b -> b.field("field", "1234")));
    IndexableField[] fields = doc.rootDoc().getFields("field");
    assertEquals(1, fields.length);
    assertTrue(fields[0].fieldType().omitNorms());
}
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) DocumentMapper(org.opensearch.index.mapper.DocumentMapper)

Aggregations

DocumentMapper (org.opensearch.index.mapper.DocumentMapper)55 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)23 Matchers.containsString (org.hamcrest.Matchers.containsString)22 CompressedXContent (org.opensearch.common.compress.CompressedXContent)19 SourceToParse (org.opensearch.index.mapper.SourceToParse)19 IndexableField (org.apache.lucene.index.IndexableField)17 MapperService (org.opensearch.index.mapper.MapperService)17 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)14 HashMap (java.util.HashMap)13 IOException (java.io.IOException)12 TextFieldMapper (org.opensearch.index.mapper.TextFieldMapper)12 Map (java.util.Map)11 IndexService (org.opensearch.index.IndexService)11 IndexSettings (org.opensearch.index.IndexSettings)11 Mapper (org.opensearch.index.mapper.Mapper)11 Collections (java.util.Collections)8 BytesRef (org.apache.lucene.util.BytesRef)8 Arrays (java.util.Arrays)7 Collection (java.util.Collection)7 DocValuesType (org.apache.lucene.index.DocValuesType)7