Search in sources :

Example 6 with VersionType

use of org.elasticsearch.index.VersionType in project elasticsearch by elastic.

the class RequestTests method setRandomVersionType.

private static void setRandomVersionType(DocWriteRequest<?> request, Map<String, String> expectedParams) {
    if (randomBoolean()) {
        VersionType versionType = randomFrom(VersionType.values());
        request.versionType(versionType);
        if (versionType != VersionType.INTERNAL) {
            expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
        }
    }
}
Also used : VersionType(org.elasticsearch.index.VersionType)

Example 7 with VersionType

use of org.elasticsearch.index.VersionType in project elasticsearch by elastic.

the class MultiGetRequest method parseDocuments.

public static void parseDocuments(XContentParser parser, List<Item> items, @Nullable String defaultIndex, @Nullable String defaultType, @Nullable String[] defaultFields, @Nullable FetchSourceContext defaultFetchSource, @Nullable String defaultRouting, boolean allowExplicitIndex) throws IOException {
    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
        if (token != XContentParser.Token.START_OBJECT) {
            throw new IllegalArgumentException("docs array element should include an object");
        }
        String index = defaultIndex;
        String type = defaultType;
        String id = null;
        String routing = defaultRouting;
        String parent = null;
        List<String> storedFields = null;
        long version = Versions.MATCH_ANY;
        VersionType versionType = VersionType.INTERNAL;
        FetchSourceContext fetchSourceContext = FetchSourceContext.FETCH_SOURCE;
        while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = parser.currentName();
            } else if (token.isValue()) {
                if ("_index".equals(currentFieldName)) {
                    if (!allowExplicitIndex) {
                        throw new IllegalArgumentException("explicit index in multi get is not allowed");
                    }
                    index = parser.text();
                } else if ("_type".equals(currentFieldName)) {
                    type = parser.text();
                } else if ("_id".equals(currentFieldName)) {
                    id = parser.text();
                } else if ("_routing".equals(currentFieldName) || "routing".equals(currentFieldName)) {
                    routing = parser.text();
                } else if ("_parent".equals(currentFieldName) || "parent".equals(currentFieldName)) {
                    parent = parser.text();
                } else if ("fields".equals(currentFieldName)) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if ("stored_fields".equals(currentFieldName)) {
                    storedFields = new ArrayList<>();
                    storedFields.add(parser.text());
                } else if ("_version".equals(currentFieldName) || "version".equals(currentFieldName)) {
                    version = parser.longValue();
                } else if ("_version_type".equals(currentFieldName) || "_versionType".equals(currentFieldName) || "version_type".equals(currentFieldName) || "versionType".equals(currentFieldName)) {
                    versionType = VersionType.fromString(parser.text());
                } else if ("_source".equals(currentFieldName)) {
                    // check lenient to avoid interpreting the value as string but parse strict in order to provoke an error early on.
                    if (parser.isBooleanValueLenient()) {
                        fetchSourceContext = new FetchSourceContext(parser.booleanValue(), fetchSourceContext.includes(), fetchSourceContext.excludes());
                    } else if (token == XContentParser.Token.VALUE_STRING) {
                        fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), new String[] { parser.text() }, fetchSourceContext.excludes());
                    } else {
                        throw new ElasticsearchParseException("illegal type for _source: [{}]", token);
                    }
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                if ("fields".equals(currentFieldName)) {
                    throw new ParsingException(parser.getTokenLocation(), "Unsupported field [fields] used, expected [stored_fields] instead");
                } else if ("stored_fields".equals(currentFieldName)) {
                    storedFields = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        storedFields.add(parser.text());
                    }
                } else if ("_source".equals(currentFieldName)) {
                    ArrayList<String> includes = new ArrayList<>();
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        includes.add(parser.text());
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes.toArray(Strings.EMPTY_ARRAY), fetchSourceContext.excludes());
                }
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("_source".equals(currentFieldName)) {
                    List<String> currentList = null, includes = null, excludes = null;
                    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                        if (token == XContentParser.Token.FIELD_NAME) {
                            currentFieldName = parser.currentName();
                            if ("includes".equals(currentFieldName) || "include".equals(currentFieldName)) {
                                currentList = includes != null ? includes : (includes = new ArrayList<>(2));
                            } else if ("excludes".equals(currentFieldName) || "exclude".equals(currentFieldName)) {
                                currentList = excludes != null ? excludes : (excludes = new ArrayList<>(2));
                            } else {
                                throw new ElasticsearchParseException("source definition may not contain [{}]", parser.text());
                            }
                        } else if (token == XContentParser.Token.START_ARRAY) {
                            while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                                currentList.add(parser.text());
                            }
                        } else if (token.isValue()) {
                            currentList.add(parser.text());
                        } else {
                            throw new ElasticsearchParseException("unexpected token while parsing source settings");
                        }
                    }
                    fetchSourceContext = new FetchSourceContext(fetchSourceContext.fetchSource(), includes == null ? Strings.EMPTY_ARRAY : includes.toArray(new String[includes.size()]), excludes == null ? Strings.EMPTY_ARRAY : excludes.toArray(new String[excludes.size()]));
                }
            }
        }
        String[] aFields;
        if (storedFields != null) {
            aFields = storedFields.toArray(new String[storedFields.size()]);
        } else {
            aFields = defaultFields;
        }
        items.add(new Item(index, type, id).routing(routing).storedFields(aFields).parent(parent).version(version).versionType(versionType).fetchSourceContext(fetchSourceContext == FetchSourceContext.FETCH_SOURCE ? defaultFetchSource : fetchSourceContext));
    }
}
Also used : ArrayList(java.util.ArrayList) VersionType(org.elasticsearch.index.VersionType) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) ParsingException(org.elasticsearch.common.ParsingException) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) ArrayList(java.util.ArrayList) List(java.util.List) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 8 with VersionType

use of org.elasticsearch.index.VersionType in project elasticsearch by elastic.

the class InternalEngineTests method testSequenceNumberAdvancesToMaxSeqNoOnEngineOpenOnReplica.

public void testSequenceNumberAdvancesToMaxSeqNoOnEngineOpenOnReplica() throws IOException {
    final long v = Versions.MATCH_ANY;
    final VersionType t = VersionType.EXTERNAL;
    final long ts = IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP;
    final int docs = randomIntBetween(1, 32);
    InternalEngine initialEngine = null;
    try {
        initialEngine = engine;
        for (int i = 0; i < docs; i++) {
            final String id = Integer.toString(i);
            final ParsedDocument doc = testParsedDocument(id, "test", null, testDocumentWithTextField(), SOURCE, null);
            final Term uid = newUid(doc);
            // create a gap at sequence number 3 * i + 1
            initialEngine.index(new Engine.Index(uid, doc, 3 * i, 1, v, t, REPLICA, System.nanoTime(), ts, false));
            initialEngine.delete(new Engine.Delete("type", id, uid, 3 * i + 2, 1, v, t, REPLICA, System.nanoTime()));
        }
        // bake the commit with the local checkpoint stuck at 0 and gaps all along the way up to the max sequence number
        assertThat(initialEngine.seqNoService().getLocalCheckpoint(), equalTo((long) 0));
        assertThat(initialEngine.seqNoService().getMaxSeqNo(), equalTo((long) (3 * (docs - 1) + 2)));
        initialEngine.flush(true, true);
        for (int i = 0; i < docs; i++) {
            final String id = Integer.toString(i);
            final ParsedDocument doc = testParsedDocument(id, "test", null, testDocumentWithTextField(), SOURCE, null);
            final Term uid = newUid(doc);
            initialEngine.index(new Engine.Index(uid, doc, 3 * i + 1, 1, v, t, REPLICA, System.nanoTime(), ts, false));
        }
    } finally {
        IOUtils.close(initialEngine);
    }
    try (Engine recoveringEngine = new InternalEngine(copy(initialEngine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG))) {
        assertThat(recoveringEngine.seqNoService().getLocalCheckpoint(), greaterThanOrEqualTo((long) (3 * (docs - 1) + 2 - 1)));
    }
}
Also used : ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) VersionType(org.elasticsearch.index.VersionType) LongPoint(org.apache.lucene.document.LongPoint)

Example 9 with VersionType

use of org.elasticsearch.index.VersionType in project elasticsearch by elastic.

the class RequestTests method getAndExistsTest.

private static void getAndExistsTest(Function<GetRequest, Request> requestConverter, String method) {
    String index = randomAsciiOfLengthBetween(3, 10);
    String type = randomAsciiOfLengthBetween(3, 10);
    String id = randomAsciiOfLengthBetween(3, 10);
    GetRequest getRequest = new GetRequest(index, type, id);
    Map<String, String> expectedParams = new HashMap<>();
    if (randomBoolean()) {
        if (randomBoolean()) {
            String preference = randomAsciiOfLengthBetween(3, 10);
            getRequest.preference(preference);
            expectedParams.put("preference", preference);
        }
        if (randomBoolean()) {
            String routing = randomAsciiOfLengthBetween(3, 10);
            getRequest.routing(routing);
            expectedParams.put("routing", routing);
        }
        if (randomBoolean()) {
            boolean realtime = randomBoolean();
            getRequest.realtime(realtime);
            if (realtime == false) {
                expectedParams.put("realtime", "false");
            }
        }
        if (randomBoolean()) {
            boolean refresh = randomBoolean();
            getRequest.refresh(refresh);
            if (refresh) {
                expectedParams.put("refresh", "true");
            }
        }
        if (randomBoolean()) {
            long version = randomLong();
            getRequest.version(version);
            if (version != Versions.MATCH_ANY) {
                expectedParams.put("version", Long.toString(version));
            }
        }
        if (randomBoolean()) {
            VersionType versionType = randomFrom(VersionType.values());
            getRequest.versionType(versionType);
            if (versionType != VersionType.INTERNAL) {
                expectedParams.put("version_type", versionType.name().toLowerCase(Locale.ROOT));
            }
        }
        if (randomBoolean()) {
            int numStoredFields = randomIntBetween(1, 10);
            String[] storedFields = new String[numStoredFields];
            StringBuilder storedFieldsParam = new StringBuilder();
            for (int i = 0; i < numStoredFields; i++) {
                String storedField = randomAsciiOfLengthBetween(3, 10);
                storedFields[i] = storedField;
                storedFieldsParam.append(storedField);
                if (i < numStoredFields - 1) {
                    storedFieldsParam.append(",");
                }
            }
            getRequest.storedFields(storedFields);
            expectedParams.put("stored_fields", storedFieldsParam.toString());
        }
        if (randomBoolean()) {
            randomizeFetchSourceContextParams(getRequest::fetchSourceContext, expectedParams);
        }
    }
    Request request = requestConverter.apply(getRequest);
    assertEquals("/" + index + "/" + type + "/" + id, request.endpoint);
    assertEquals(expectedParams, request.params);
    assertNull(request.entity);
    assertEquals(method, request.method);
}
Also used : HashMap(java.util.HashMap) GetRequest(org.elasticsearch.action.get.GetRequest) DeleteRequest(org.elasticsearch.action.delete.DeleteRequest) WriteRequest(org.elasticsearch.action.support.WriteRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) BulkShardRequest(org.elasticsearch.action.bulk.BulkShardRequest) GetRequest(org.elasticsearch.action.get.GetRequest) ReplicatedWriteRequest(org.elasticsearch.action.support.replication.ReplicatedWriteRequest) UpdateRequest(org.elasticsearch.action.update.UpdateRequest) DocWriteRequest(org.elasticsearch.action.DocWriteRequest) ReplicationRequest(org.elasticsearch.action.support.replication.ReplicationRequest) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) VersionType(org.elasticsearch.index.VersionType)

Example 10 with VersionType

use of org.elasticsearch.index.VersionType in project crate by crate.

the class IndexShard method applyTranslogOperation.

private Engine.Result applyTranslogOperation(Engine engine, Translog.Operation operation, Engine.Operation.Origin origin) throws IOException {
    // If a translog op is replayed on the primary (eg. ccr), we need to use external instead of null for its version type.
    final VersionType versionType = (origin == Engine.Operation.Origin.PRIMARY) ? VersionType.EXTERNAL : null;
    final Engine.Result result;
    switch(operation.opType()) {
        case INDEX:
            final Translog.Index index = (Translog.Index) operation;
            // we set canHaveDuplicates to true all the time such that we de-optimze the translog case and ensure that all
            // autoGeneratedID docs that are coming from the primary are updated correctly.
            result = applyIndexOperation(engine, index.seqNo(), index.primaryTerm(), index.version(), versionType, UNASSIGNED_SEQ_NO, 0, index.getAutoGeneratedIdTimestamp(), true, origin, new SourceToParse(shardId.getIndexName(), index.id(), index.source(), XContentHelper.xContentType(index.source()), index.routing()));
            break;
        case DELETE:
            final Translog.Delete delete = (Translog.Delete) operation;
            result = applyDeleteOperation(engine, delete.seqNo(), delete.primaryTerm(), delete.version(), delete.id(), versionType, UNASSIGNED_SEQ_NO, 0, origin);
            break;
        case NO_OP:
            final Translog.NoOp noOp = (Translog.NoOp) operation;
            result = markSeqNoAsNoop(engine, noOp.seqNo(), noOp.primaryTerm(), noOp.reason(), origin);
            break;
        default:
            throw new IllegalStateException("No operation defined for [" + operation + "]");
    }
    return result;
}
Also used : SourceToParse(org.elasticsearch.index.mapper.SourceToParse) CheckIndex(org.apache.lucene.index.CheckIndex) Index(org.elasticsearch.index.Index) VersionType(org.elasticsearch.index.VersionType) ReadOnlyEngine(org.elasticsearch.index.engine.ReadOnlyEngine) Engine(org.elasticsearch.index.engine.Engine) Translog(org.elasticsearch.index.translog.Translog)

Aggregations

VersionType (org.elasticsearch.index.VersionType)11 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)4 IndexRequest (org.elasticsearch.action.index.IndexRequest)4 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)4 XContentParser (org.elasticsearch.common.xcontent.XContentParser)4 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)3 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)3 GetRequest (org.elasticsearch.action.get.GetRequest)3 WriteRequest (org.elasticsearch.action.support.WriteRequest)3 Engine (org.elasticsearch.index.engine.Engine)3 HashMap (java.util.HashMap)2 HttpEntity (org.apache.http.HttpEntity)2 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)2 Term (org.apache.lucene.index.Term)2 BulkShardRequest (org.elasticsearch.action.bulk.BulkShardRequest)2 ReplicatedWriteRequest (org.elasticsearch.action.support.replication.ReplicatedWriteRequest)2 ReplicationRequest (org.elasticsearch.action.support.replication.ReplicationRequest)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 XContentType (org.elasticsearch.common.xcontent.XContentType)2 SourceToParse (org.elasticsearch.index.mapper.SourceToParse)2