Search in sources :

Example 21 with Document

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

the class TranslogTests method testTranslogOpSerialization.

public void testTranslogOpSerialization() throws Exception {
    BytesReference B_1 = new BytesArray(new byte[] { 1 });
    SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID();
    long randomSeqNum = randomNonNegativeLong();
    long randomPrimaryTerm = randomBoolean() ? 0 : randomNonNegativeLong();
    seqID.seqNo.setLongValue(randomSeqNum);
    seqID.seqNoDocValue.setLongValue(randomSeqNum);
    seqID.primaryTerm.setLongValue(randomPrimaryTerm);
    Field idField = new Field("_id", Uid.encodeId("1"), IdFieldMapper.Defaults.FIELD_TYPE);
    Field versionField = new NumericDocValuesField("_version", 1);
    Document document = new Document();
    document.add(new TextField("value", "test", Field.Store.YES));
    document.add(idField);
    document.add(versionField);
    document.add(seqID.seqNo);
    document.add(seqID.seqNoDocValue);
    document.add(seqID.primaryTerm);
    ParsedDocument doc = new ParsedDocument(versionField, seqID, "1", "type", null, Arrays.asList(document), B_1, XContentType.JSON, null);
    Engine.Index eIndex = new Engine.Index(newUid(doc), doc, randomSeqNum, randomPrimaryTerm, 1, VersionType.INTERNAL, Origin.PRIMARY, 0, 0, false, SequenceNumbers.UNASSIGNED_SEQ_NO, 0);
    Engine.IndexResult eIndexResult = new Engine.IndexResult(1, randomPrimaryTerm, randomSeqNum, true);
    Translog.Index index = new Translog.Index(eIndex, eIndexResult);
    Version wireVersion = VersionUtils.randomVersionBetween(random(), Version.CURRENT.minimumCompatibilityVersion(), Version.CURRENT);
    BytesStreamOutput out = new BytesStreamOutput();
    out.setVersion(wireVersion);
    Translog.Operation.writeOperation(out, index);
    StreamInput in = out.bytes().streamInput();
    in.setVersion(wireVersion);
    Translog.Index serializedIndex = (Translog.Index) Translog.Operation.readOperation(in);
    assertEquals(index, serializedIndex);
    Engine.Delete eDelete = new Engine.Delete(doc.type(), doc.id(), newUid(doc), randomSeqNum, randomPrimaryTerm, 2, VersionType.INTERNAL, Origin.PRIMARY, 0, SequenceNumbers.UNASSIGNED_SEQ_NO, 0);
    Engine.DeleteResult eDeleteResult = new Engine.DeleteResult(2, randomPrimaryTerm, randomSeqNum, true);
    Translog.Delete delete = new Translog.Delete(eDelete, eDeleteResult);
    out = new BytesStreamOutput();
    out.setVersion(wireVersion);
    Translog.Operation.writeOperation(out, delete);
    in = out.bytes().streamInput();
    in.setVersion(wireVersion);
    Translog.Delete serializedDelete = (Translog.Delete) Translog.Operation.readOperation(in);
    assertEquals(delete, serializedDelete);
}
Also used : Document(org.opensearch.index.mapper.ParseContext.Document) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) TextField(org.apache.lucene.document.TextField) Field(org.apache.lucene.document.Field) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) ParsedDocument(org.opensearch.index.mapper.ParsedDocument) Version(org.opensearch.Version) TextField(org.apache.lucene.document.TextField) Engine(org.opensearch.index.engine.Engine) ReleasableBytesReference(org.opensearch.common.bytes.ReleasableBytesReference) BytesReference(org.opensearch.common.bytes.BytesReference) SeqNoFieldMapper(org.opensearch.index.mapper.SeqNoFieldMapper) BytesArray(org.opensearch.common.bytes.BytesArray) StreamInput(org.opensearch.common.io.stream.StreamInput)

Example 22 with Document

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

the class SeqNoFieldMapper method postParse.

@Override
public void postParse(ParseContext context) throws IOException {
    // In the case of nested docs, let's fill nested docs with the original
    // so that Lucene doesn't write a Bitset for documents that
    // don't have the field. This is consistent with the default value
    // for efficiency.
    // we share the parent docs fields to ensure good compression
    SequenceIDFields seqID = context.seqID();
    assert seqID != null;
    for (Document doc : context.nonRootDocuments()) {
        doc.add(seqID.seqNo);
        doc.add(seqID.seqNoDocValue);
    }
}
Also used : Document(org.opensearch.index.mapper.ParseContext.Document)

Example 23 with Document

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

the class DataStreamFieldMapper method postParse.

@Override
public void postParse(ParseContext context) throws IOException {
    // If _data_stream_timestamp metadata mapping is disabled, then skip all the remaining checks.
    if (enabled == false) {
        return;
    }
    // It is expected that the timestamp field will be parsed by the DateFieldMapper during the parseCreateField step.
    // The parsed field will be added to the document as:
    // 1. LongPoint (indexed = true; an indexed long field to allow fast range filters on the timestamp field value)
    // 2. SortedNumericDocValuesField (hasDocValues = true; allows sorting, aggregations and access to the timestamp field value)
    Document document = context.doc();
    IndexableField[] fields = document.getFields(timestampField.getName());
    // Documents must contain exactly one value for the timestamp field.
    long numTimestampValues = Arrays.stream(fields).filter(field -> field.fieldType().docValuesType() == DocValuesType.SORTED_NUMERIC).count();
    if (numTimestampValues != 1) {
        throw new IllegalArgumentException("documents must contain a single-valued timestamp field '" + timestampField.getName() + "' of date type");
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) Query(org.apache.lucene.search.Query) Arrays(java.util.Arrays) List(java.util.List) DocValuesType(org.apache.lucene.index.DocValuesType) IndexableField(org.apache.lucene.index.IndexableField) Map(java.util.Map) QueryShardContext(org.opensearch.index.query.QueryShardContext) IOException(java.io.IOException) Document(org.opensearch.index.mapper.ParseContext.Document) Collections(java.util.Collections) SearchLookup(org.opensearch.search.lookup.SearchLookup) TimestampField(org.opensearch.cluster.metadata.DataStream.TimestampField) Document(org.opensearch.index.mapper.ParseContext.Document)

Example 24 with Document

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

the class BooleanFieldMapperTests method testDocValues.

public void testDocValues() throws Exception {
    DocumentMapper defaultMapper = createDocumentMapper(mapping(b -> {
        b.startObject("bool1").field("type", "boolean").endObject();
        b.startObject("bool2");
        {
            b.field("type", "boolean");
            b.field("index", false);
        }
        b.endObject();
        b.startObject("bool3");
        {
            b.field("type", "boolean");
            b.field("index", true);
        }
        b.endObject();
    }));
    ParsedDocument parsedDoc = defaultMapper.parse(source(b -> {
        b.field("bool1", true);
        b.field("bool2", true);
        b.field("bool3", true);
    }));
    Document doc = parsedDoc.rootDoc();
    IndexableField[] fields = doc.getFields("bool1");
    assertEquals(2, fields.length);
    assertEquals(DocValuesType.NONE, fields[0].fieldType().docValuesType());
    assertEquals(DocValuesType.SORTED_NUMERIC, fields[1].fieldType().docValuesType());
    fields = doc.getFields("bool2");
    assertEquals(1, fields.length);
    assertEquals(DocValuesType.SORTED_NUMERIC, fields[0].fieldType().docValuesType());
    fields = doc.getFields("bool3");
    assertEquals(DocValuesType.NONE, fields[0].fieldType().docValuesType());
    assertEquals(DocValuesType.SORTED_NUMERIC, fields[1].fieldType().docValuesType());
}
Also used : BytesRef(org.apache.lucene.util.BytesRef) IndexableField(org.apache.lucene.index.IndexableField) ToXContent(org.opensearch.common.xcontent.ToXContent) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Document(org.opensearch.index.mapper.ParseContext.Document) SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) Strings(org.opensearch.common.Strings) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) TermQuery(org.apache.lucene.search.TermQuery) DocValuesType(org.apache.lucene.index.DocValuesType) BoostQuery(org.apache.lucene.search.BoostQuery) LeafReader(org.apache.lucene.index.LeafReader) XContentFactory(org.opensearch.common.xcontent.XContentFactory) IndexableField(org.apache.lucene.index.IndexableField) Document(org.opensearch.index.mapper.ParseContext.Document)

Example 25 with Document

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

the class DateFieldTypeTests method isFieldWithinRangeTestCase.

public void isFieldWithinRangeTestCase(DateFieldType ft) throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    Document doc = new Document();
    LongPoint field = new LongPoint("my_date", ft.parse("2015-10-12"));
    doc.add(field);
    w.addDocument(doc);
    field.setLongValue(ft.parse("2016-04-03"));
    w.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(w);
    DateMathParser alternateFormat = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.toDateMathParser();
    doTestIsFieldWithinQuery(ft, reader, null, null);
    doTestIsFieldWithinQuery(ft, reader, null, alternateFormat);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat);
    QueryRewriteContext context = new QueryRewriteContext(xContentRegistry(), writableRegistry(), null, () -> nowInMillis);
    // Fields with no value indexed.
    DateFieldType ft2 = new DateFieldType("my_date2");
    assertEquals(Relation.DISJOINT, ft2.isFieldWithinQuery(reader, "2015-10-09", "2016-01-02", false, false, null, null, context));
    IOUtils.close(reader, w, dir);
}
Also used : IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) DateFieldType(org.opensearch.index.mapper.DateFieldMapper.DateFieldType) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) LongPoint(org.apache.lucene.document.LongPoint) DateMathParser(org.opensearch.common.time.DateMathParser) Document(org.opensearch.index.mapper.ParseContext.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

Document (org.opensearch.index.mapper.ParseContext.Document)32 IndexableField (org.apache.lucene.index.IndexableField)22 BytesArray (org.opensearch.common.bytes.BytesArray)16 BytesReference (org.opensearch.common.bytes.BytesReference)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 IOException (java.io.IOException)11 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)11 TextField (org.apache.lucene.document.TextField)11 BytesRef (org.apache.lucene.util.BytesRef)11 Field (org.apache.lucene.document.Field)10 ParsedDocument (org.opensearch.index.mapper.ParsedDocument)10 List (java.util.List)9 Version (org.opensearch.Version)9 IndexSettings (org.opensearch.index.IndexSettings)9 ArrayList (java.util.ArrayList)8 Collections (java.util.Collections)8 StoredField (org.apache.lucene.document.StoredField)8 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)8 Matchers.equalTo (org.hamcrest.Matchers.equalTo)8 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)8