Search in sources :

Example 16 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class InternalEngineTests method testOutOfOrderSequenceNumbersWithVersionConflict.

public void testOutOfOrderSequenceNumbersWithVersionConflict() throws IOException {
    final List<Engine.Operation> operations = new ArrayList<>();
    final int numberOfOperations = randomIntBetween(16, 32);
    final Document document = testDocumentWithTextField();
    final AtomicLong sequenceNumber = new AtomicLong();
    final Engine.Operation.Origin origin = randomFrom(LOCAL_TRANSLOG_RECOVERY, PEER_RECOVERY, PRIMARY, REPLICA);
    final LongSupplier sequenceNumberSupplier = origin == PRIMARY ? () -> SequenceNumbersService.UNASSIGNED_SEQ_NO : sequenceNumber::getAndIncrement;
    document.add(new Field(SourceFieldMapper.NAME, BytesReference.toBytes(B_1), SourceFieldMapper.Defaults.FIELD_TYPE));
    final ParsedDocument doc = testParsedDocument("1", "test", null, document, B_1, null);
    final Term uid = newUid(doc);
    for (int i = 0; i < numberOfOperations; i++) {
        if (randomBoolean()) {
            final Engine.Index index = new Engine.Index(uid, doc, sequenceNumberSupplier.getAsLong(), 1, i, VersionType.EXTERNAL, origin, System.nanoTime(), IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, false);
            operations.add(index);
        } else {
            final Engine.Delete delete = new Engine.Delete("test", "1", uid, sequenceNumberSupplier.getAsLong(), 1, i, VersionType.EXTERNAL, origin, System.nanoTime());
            operations.add(delete);
        }
    }
    final boolean exists = operations.get(operations.size() - 1) instanceof Engine.Index;
    Randomness.shuffle(operations);
    for (final Engine.Operation operation : operations) {
        if (operation instanceof Engine.Index) {
            engine.index((Engine.Index) operation);
        } else {
            engine.delete((Engine.Delete) operation);
        }
    }
    final long expectedLocalCheckpoint;
    if (origin == PRIMARY) {
        // we can only advance as far as the number of operations that did not conflict
        int count = 0;
        // each time the version increments as we walk the list, that counts as a successful operation
        long version = -1;
        for (int i = 0; i < numberOfOperations; i++) {
            if (operations.get(i).version() >= version) {
                count++;
                version = operations.get(i).version();
            }
        }
        // sequence numbers start at zero, so the expected local checkpoint is the number of successful operations minus one
        expectedLocalCheckpoint = count - 1;
    } else {
        expectedLocalCheckpoint = numberOfOperations - 1;
    }
    assertThat(engine.seqNoService().getLocalCheckpoint(), equalTo(expectedLocalCheckpoint));
    try (Engine.GetResult result = engine.get(new Engine.Get(true, uid))) {
        assertThat(result.exists(), equalTo(exists));
    }
}
Also used : ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) Term(org.apache.lucene.index.Term) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Document(org.elasticsearch.index.mapper.ParseContext.Document) LongPoint(org.apache.lucene.document.LongPoint) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) TextField(org.apache.lucene.document.TextField) IndexableField(org.apache.lucene.index.IndexableField) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) LongSupplier(java.util.function.LongSupplier)

Example 17 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class DynamicTemplatesTests method testSimpleWithXContentTraverse.

public void testSimpleWithXContentTraverse() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-mapping.json");
    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("person").setSource(mapping, XContentType.JSON).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("person");
    byte[] json = copyToBytesFromClasspath("/org/elasticsearch/index/mapper/dynamictemplate/simple/test-data.json");
    ParsedDocument parsedDoc = docMapper.parse("test", "person", "1", new BytesArray(json));
    client().admin().indices().preparePutMapping("test").setType("person").setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();
    docMapper = index.mapperService().documentMapper("person");
    Document doc = parsedDoc.rootDoc();
    IndexableField f = doc.getField("name");
    assertThat(f.name(), equalTo("name"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("some name")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    FieldMapper fieldMapper = docMapper.mappers().getMapper("name");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1");
    assertThat(f.name(), equalTo("multi1"));
    assertThat(f.stringValue(), equalTo("multi 1"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = docMapper.mappers().getMapper("multi1");
    assertNotNull(fieldMapper);
    f = doc.getField("multi1.org");
    assertThat(f.name(), equalTo("multi1.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 1")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = docMapper.mappers().getMapper("multi1.org");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2");
    assertThat(f.name(), equalTo("multi2"));
    assertThat(f.stringValue(), equalTo("multi 2"));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(true));
    fieldMapper = docMapper.mappers().getMapper("multi2");
    assertNotNull(fieldMapper);
    f = doc.getField("multi2.org");
    assertThat(f.name(), equalTo("multi2.org"));
    assertThat(f.binaryValue(), equalTo(new BytesRef("multi 2")));
    assertNotSame(IndexOptions.NONE, f.fieldType().indexOptions());
    assertThat(f.fieldType().tokenized(), equalTo(false));
    fieldMapper = docMapper.mappers().getMapper("multi2.org");
    assertNotNull(fieldMapper);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) Document(org.elasticsearch.index.mapper.ParseContext.Document) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) BytesRef(org.apache.lucene.util.BytesRef)

Example 18 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class DateFieldTypeTests method testIsFieldWithinQuery.

public void testIsFieldWithinQuery() throws IOException {
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    long instant1 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime("2015-10-12").getMillis();
    long instant2 = DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER.parser().parseDateTime("2016-04-03").getMillis();
    Document doc = new Document();
    LongPoint field = new LongPoint("my_date", instant1);
    doc.add(field);
    w.addDocument(doc);
    field.setLongValue(instant2);
    w.addDocument(doc);
    DirectoryReader reader = DirectoryReader.open(w);
    DateFieldType ft = new DateFieldType();
    ft.setName("my_date");
    DateMathParser alternateFormat = new DateMathParser(DateFieldMapper.DEFAULT_DATE_TIME_FORMATTER);
    doTestIsFieldWithinQuery(ft, reader, null, null);
    doTestIsFieldWithinQuery(ft, reader, null, alternateFormat);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, null);
    doTestIsFieldWithinQuery(ft, reader, DateTimeZone.UTC, alternateFormat);
    // Fields with no value indexed.
    DateFieldType ft2 = new DateFieldType();
    ft2.setName("my_date2");
    QueryRewriteContext context = new QueryRewriteContext(null, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    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.elasticsearch.index.mapper.DateFieldMapper.DateFieldType) QueryRewriteContext(org.elasticsearch.index.query.QueryRewriteContext) LongPoint(org.apache.lucene.document.LongPoint) DateMathParser(org.elasticsearch.common.joda.DateMathParser) Document(org.elasticsearch.index.mapper.ParseContext.Document) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 19 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class DocumentParserTests method testSimpleMapper.

public void testSimpleMapper() throws Exception {
    IndexService indexService = createIndex("test");
    DocumentMapper docMapper = new DocumentMapper.Builder(new RootObjectMapper.Builder("person").add(new ObjectMapper.Builder("name").add(new TextFieldMapper.Builder("first").store(true).index(false))), indexService.mapperService()).build(indexService.mapperService());
    BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1.json"));
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    assertThat(doc.get(docMapper.mappers().getMapper("name.first").fieldType().name()), equalTo("shay"));
    doc = docMapper.parse("test", "person", "1", json).rootDoc();
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) IndexService(org.elasticsearch.index.IndexService) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Example 20 with Document

use of org.elasticsearch.index.mapper.ParseContext.Document in project elasticsearch by elastic.

the class DocumentParserTests method testSimpleParserNoTypeNoId.

public void testSimpleParserNoTypeNoId() throws Exception {
    String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/simple/test-mapping.json");
    DocumentMapper docMapper = createIndex("test").mapperService().documentMapperParser().parse("person", new CompressedXContent(mapping));
    BytesReference json = new BytesArray(copyToBytesFromClasspath("/org/elasticsearch/index/mapper/simple/test1-notype-noid.json"));
    Document doc = docMapper.parse("test", "person", "1", json).rootDoc();
    assertThat(doc.get(docMapper.uidMapper().fieldType().name()), equalTo(Uid.createUid("person", "1")));
    assertThat(doc.get(docMapper.mappers().getMapper("name.first").fieldType().name()), equalTo("shay"));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BytesArray(org.elasticsearch.common.bytes.BytesArray) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) Document(org.elasticsearch.index.mapper.ParseContext.Document)

Aggregations

Document (org.elasticsearch.index.mapper.ParseContext.Document)25 IndexableField (org.apache.lucene.index.IndexableField)12 BytesArray (org.elasticsearch.common.bytes.BytesArray)12 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 BytesReference (org.elasticsearch.common.bytes.BytesReference)10 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)10 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)8 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)7 TextField (org.apache.lucene.document.TextField)7 IndexService (org.elasticsearch.index.IndexService)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Field (org.apache.lucene.document.Field)5 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)4 LongPoint (org.apache.lucene.document.LongPoint)3 StoredField (org.apache.lucene.document.StoredField)3 Term (org.apache.lucene.index.Term)3 BytesRef (org.apache.lucene.util.BytesRef)3 Index (org.elasticsearch.index.Index)3 ArrayList (java.util.ArrayList)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2