Java Code Examples for org.elasticsearch.common.lucene.uid.Versions#MATCH_ANY

The following examples show how to use org.elasticsearch.common.lucene.uid.Versions#MATCH_ANY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecoverySourceHandlerTests.java    From crate with Apache License 2.0 6 votes vote down vote up
private Engine.Index getIndex(final String id) {
    final String type = "test";
    final ParseContext.Document document = new ParseContext.Document();
    document.add(new TextField("test", "test", Field.Store.YES));
    final Field idField = new Field("_id", Uid.encodeId(id), IdFieldMapper.Defaults.FIELD_TYPE);
    final Field versionField = new NumericDocValuesField("_version", Versions.MATCH_ANY);
    final SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID();
    document.add(idField);
    document.add(versionField);
    document.add(seqID.seqNo);
    document.add(seqID.seqNoDocValue);
    document.add(seqID.primaryTerm);
    final BytesReference source = new BytesArray(new byte[] { 1 });
    final ParsedDocument doc =
        new ParsedDocument(versionField, seqID, id, type, List.of(document), source, null);
    return new Engine.Index(
        new Term("_id", Uid.encodeId(doc.id())), doc, UNASSIGNED_SEQ_NO, 0,
        Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), -1, false, UNASSIGNED_SEQ_NO, 0);

}
 
Example 2
Source File: TransportShardUpsertAction.java    From crate with Apache License 2.0 6 votes vote down vote up
private static Doc getDocument(IndexShard indexShard, String id, long version, long seqNo, long primaryTerm) {
    // when sequence versioning is used, this lookup will throw VersionConflictEngineException
    Doc doc = PKLookupOperation.lookupDoc(indexShard, id, Versions.MATCH_ANY, VersionType.INTERNAL, seqNo, primaryTerm);
    if (doc == null) {
        throw new DocumentMissingException(indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, id);
    }
    if (doc.getSource() == null) {
        throw new DocumentSourceMissingException(indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, id);
    }
    if (version != Versions.MATCH_ANY && version != doc.getVersion()) {
        throw new VersionConflictEngineException(
            indexShard.shardId(),
            id,
            "Requested version: " + version + " but got version: " + doc.getVersion());
    }
    return doc;
}
 
Example 3
Source File: MoreLikeThisQueryBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    if (this.index != null) {
        builder.field(Field.INDEX.getPreferredName(), this.index);
    }
    if (this.type != null) {
        builder.field(Field.TYPE.getPreferredName(), this.type);
    }
    if (this.id != null && this.doc == null) {
        builder.field(Field.ID.getPreferredName(), this.id);
    }
    if (this.doc != null) {
        XContentType contentType = XContentFactory.xContentType(this.doc);
        if (contentType == builder.contentType()) {
            builder.rawField(Field.DOC.getPreferredName(), this.doc);
        } else {
            XContentParser parser = XContentFactory.xContent(contentType).createParser(this.doc);
            parser.nextToken();
            builder.field(Field.DOC.getPreferredName());
            builder.copyCurrentStructure(parser);
        }
    }
    if (this.fields != null) {
        builder.array(Field.FIELDS.getPreferredName(), this.fields);
    }
    if (this.perFieldAnalyzer != null) {
        builder.field(Field.PER_FIELD_ANALYZER.getPreferredName(), this.perFieldAnalyzer);
    }
    if (this.routing != null) {
        builder.field(Field.ROUTING.getPreferredName(), this.routing);
    }
    if (this.version != Versions.MATCH_ANY) {
        builder.field(Field.VERSION.getPreferredName(), this.version);
    }
    if (this.versionType != VersionType.INTERNAL) {
        builder.field(Field.VERSION_TYPE.getPreferredName(), this.versionType.toString().toLowerCase(Locale.ROOT));
    }
    return builder.endObject();
}
 
Example 4
Source File: RestActions.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public static long parseVersion(RestRequest request) {
    if (request.hasParam("version")) {
        return request.paramAsLong("version", Versions.MATCH_ANY);
    }
    String ifMatch = request.header("If-Match");
    if (ifMatch != null) {
        return Long.parseLong(ifMatch);
    }
    return Versions.MATCH_ANY;
}
 
Example 5
Source File: MappingMetaData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public ParseContext createParseContext(@Nullable String id, @Nullable String routing, @Nullable String timestamp, long version) {
    // We parse the routing even if there is already a routing key in the request in order to make sure that
    // they are the same
    return new ParseContext(
            id == null && id().hasPath(),
            routing().hasPath(),
            timestamp == null && timestamp().hasPath(),
            version == Versions.MATCH_ANY && version().hasPath()
    );
}
 
Example 6
Source File: UpdateRequest.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public ActionRequestValidationException validate() {
    ActionRequestValidationException validationException = super.validate();
    if (type == null) {
        validationException = addValidationError("type is missing", validationException);
    }
    if (id == null) {
        validationException = addValidationError("id is missing", validationException);
    }

    if (!(versionType == VersionType.INTERNAL || versionType == VersionType.FORCE)) {
        validationException = addValidationError("version type [" + versionType + "] is not supported by the update API", validationException);
    } else {

        if (version != Versions.MATCH_ANY && retryOnConflict > 0) {
            validationException = addValidationError("can't provide both retry_on_conflict and a specific version", validationException);
        }

        if (!versionType.validateVersionForWrites(version)) {
            validationException = addValidationError("illegal version value [" + version + "] for version type [" + versionType.name() + "]", validationException);
        }
    }

    if (script == null && doc == null) {
        validationException = addValidationError("script or doc is missing", validationException);
    }
    if (script != null && doc != null) {
        validationException = addValidationError("can't provide both script and doc", validationException);
    }
    if (doc == null && docAsUpsert) {
        validationException = addValidationError("doc must be specified if doc_as_upsert is enabled", validationException);
    }
    return validationException;
}
 
Example 7
Source File: ElasticsearchDocument.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public ElasticsearchDocument(String id, String type, String index, String resourceId, String context,
		Function<? super String, ? extends SpatialContext> geoContextMapper) {
	this(id, type, index, Versions.MATCH_ANY, new HashMap<>(), geoContextMapper);
	fields.put(SearchFields.URI_FIELD_NAME, resourceId);
	if (context != null) {
		fields.put(SearchFields.CONTEXT_FIELD_NAME, context);
	}
}
 
Example 8
Source File: EngineTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
public static List<Engine.Operation> generateSingleDocHistory(
    final boolean forReplica,
    final VersionType versionType,
    final boolean partialOldPrimary,
    final long primaryTerm,
    final int minOpCount,
    final int maxOpCount,
    final String docId) {
    final int numOfOps = randomIntBetween(minOpCount, maxOpCount);
    final List<Engine.Operation> ops = new ArrayList<>();
    final Term id = newUid(docId);
    final int startWithSeqNo;
    if (partialOldPrimary) {
        startWithSeqNo = randomBoolean() ? numOfOps - 1 : randomIntBetween(0, numOfOps - 1);
    } else {
        startWithSeqNo = 0;
    }
    final String valuePrefix = (forReplica ? "r_" : "p_") + docId + "_";
    final boolean incrementTermWhenIntroducingSeqNo = randomBoolean();
    for (int i = 0; i < numOfOps; i++) {
        final Engine.Operation op;
        final long version;
        switch (versionType) {
            case INTERNAL:
                version = forReplica ? i : Versions.MATCH_ANY;
                break;
            case EXTERNAL:
                version = i;
                break;
            case EXTERNAL_GTE:
                version = randomBoolean() ? Math.max(i - 1, 0) : i;
                break;
            case FORCE:
                version = randomNonNegativeLong();
                break;
            default:
                throw new UnsupportedOperationException("unknown version type: " + versionType);
        }
        if (randomBoolean()) {
            op = new Engine.Index(id, testParsedDocument(docId, null, testDocumentWithTextField(valuePrefix + i), SOURCE, null),
                                  forReplica && i >= startWithSeqNo ? i * 2 : UNASSIGNED_SEQ_NO,
                                  forReplica && i >= startWithSeqNo && incrementTermWhenIntroducingSeqNo ? primaryTerm + 1 : primaryTerm,
                                  version,
                                  forReplica ? null : versionType,
                                  forReplica ? REPLICA : PRIMARY,
                                  System.currentTimeMillis(), -1, false,
                                  UNASSIGNED_SEQ_NO, 0);
        } else {
            op = new Engine.Delete("test", docId, id,
                                   forReplica && i >= startWithSeqNo ? i * 2 : UNASSIGNED_SEQ_NO,
                                   forReplica && i >= startWithSeqNo && incrementTermWhenIntroducingSeqNo ? primaryTerm + 1 : primaryTerm,
                                   version,
                                   forReplica ? null : versionType,
                                   forReplica ? REPLICA : PRIMARY,
                                   System.currentTimeMillis(), UNASSIGNED_SEQ_NO, 0);
        }
        ops.add(op);
    }
    return ops;
}
 
Example 9
Source File: DLBasedEngine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private void innerCreateNoLock(Create create, long currentVersion, VersionValue versionValue) throws IOException {
    
        long updatedVersion;
        long expectedVersion = create.version();
        if (create.versionType().isVersionConflictForWrites(currentVersion, expectedVersion)) {
            // recovery means reindex or replay from dl
            // replay from dl: it never happens, because primary will check it
            // reindex: means user insert a new record, but reindex never go here
            if (create.origin() == Operation.Origin.RECOVERY) {
                logger.info("create origin recovery but meet create conflicts, should not happern {} {} {}", create.versionType(), currentVersion, expectedVersion);
                return;
            } else {
                throw new VersionConflictEngineException(shardId, create.type(), create.id(), currentVersion, expectedVersion);
            }
        }
        
        updatedVersion = create.versionType().updateVersion(currentVersion, expectedVersion);
        boolean doUpdate = false;
        if (create.origin() == Operation.Origin.RECOVERY) {
            updatedVersion = create.version();
            if (versionValue != null || currentVersion != Versions.NOT_FOUND) {
                doUpdate = true;
            }
        } else if (create.origin() == Operation.Origin.PRIMARY) {
            if ((versionValue != null && versionValue.delete() == false) || (versionValue == null && currentVersion != Versions.NOT_FOUND)) {
                if (create.autoGeneratedId() && create.canHaveDuplicates() && currentVersion == 1 && create.version() == Versions.MATCH_ANY) {
                    /**
                     * If bulk index request fails due to a disconnect, unavailable shard etc. then the request is
                     * retried before it actually fails. However, the documents might already be indexed.
                     * For autogenerated ids this means that a version conflict will be reported in the bulk request
                     * although the document was indexed properly.
                     * To avoid this we have to make sure that the index request is treated as an update and set updatedVersion to 1.
                     * See also discussion on https://github.com/elasticsearch/elasticsearch/pull/9125
                     */
                    doUpdate = true;
                    updatedVersion = 1;
                } else {
                    throw new DocumentAlreadyExistsException(shardId, create.type(), create.id());
                }
            }
        }

        create.updateVersion(updatedVersion);

        // If it is a write request from primary, then should write the log first
        WriteDistributedLogCallBack callBack = null;
        if (create.origin() == Operation.Origin.PRIMARY) {
            callBack = new WriteDistributedLogCallBack(new Translog.Create(create));
        }
        if (doUpdate) {
            if (create.docs().size() > 1) {
                indexWriter.updateDocuments(create.uid(), create.docs(), callBack);
            } else {
                indexWriter.updateDocument(create.uid(), create.docs().get(0), callBack);
            }
        } else {
            if (create.docs().size() > 1) {
                indexWriter.addDocuments(create.docs(), callBack);
            } else {
                indexWriter.addDocument(create.docs().get(0), callBack);
            }
        }
        if (callBack != null) {
            versionMap.putUnderLock(create.uid().bytes(), new VersionValue(updatedVersion, callBack.location));
            create.setTranslogLocation(callBack.location);
        } else {
            versionMap.putUnderLock(create.uid().bytes(), new VersionValue(updatedVersion, null));
        }
        indexingService.postCreateUnderLock(create);
}
 
Example 10
Source File: EngineTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
protected Engine.Index indexForDoc(ParsedDocument doc) {
    return new Engine.Index(
        newUid(doc), doc, UNASSIGNED_SEQ_NO, primaryTerm.get(),
        Versions.MATCH_ANY, VersionType.INTERNAL, Engine.Operation.Origin.PRIMARY,
        System.nanoTime(), -1, false, UNASSIGNED_SEQ_NO, 0);
}
 
Example 11
Source File: ShardUpsertRequest.java    From crate with Apache License 2.0 4 votes vote down vote up
boolean retryOnConflict() {
    return seqNo == SequenceNumbers.UNASSIGNED_SEQ_NO && version == Versions.MATCH_ANY;
}
 
Example 12
Source File: Translog.java    From crate with Apache License 2.0 4 votes vote down vote up
/** utility for testing */
public Delete(String type, String id, long seqNo, long primaryTerm, Term uid) {
    this(type, id, uid, seqNo, primaryTerm, Versions.MATCH_ANY);
}
 
Example 13
Source File: Translog.java    From crate with Apache License 2.0 4 votes vote down vote up
public Index(String type, String id, long seqNo, long primaryTerm, byte[] source) {
    this(type, id, seqNo, primaryTerm, Versions.MATCH_ANY, source, null, -1);
}
 
Example 14
Source File: RestActions.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public static long parseVersion(RestRequest request, long defaultVersion) {
    long version = parseVersion(request);
    return (version == Versions.MATCH_ANY) ? defaultVersion : version;
}
 
Example 15
Source File: Engine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Delete(String type, String id, Term uid) {
    this(type, id, uid, Versions.MATCH_ANY, VersionType.INTERNAL, Origin.PRIMARY, System.nanoTime(), false);
}
 
Example 16
Source File: Engine.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public IndexingOperation(Term uid, ParsedDocument doc) {
    this(uid, doc, Versions.MATCH_ANY, VersionType.INTERNAL, Origin.PRIMARY, System.nanoTime(), true);
}
 
Example 17
Source File: TransportShardUpsertAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Prepares an update request by converting it into an index request.
 * <p/>
 * TODO: detect a NOOP and return an update response if true
 */
@SuppressWarnings("unchecked")
private SourceAndVersion prepareUpdate(DocTableInfo tableInfo,
                                       ShardUpsertRequest request,
                                       ShardUpsertRequest.Item item,
                                       IndexShard indexShard) throws ElasticsearchException {
    final GetResult getResult = indexShard.getService().get(request.type(), item.id(),
            new String[]{RoutingFieldMapper.NAME, ParentFieldMapper.NAME, TTLFieldMapper.NAME},
            true, Versions.MATCH_ANY, VersionType.INTERNAL, FetchSourceContext.FETCH_SOURCE, false);

    if (!getResult.isExists()) {
        throw new DocumentMissingException(new ShardId(request.index(), request.shardId().id()), request.type(), item.id());
    }

    if (getResult.internalSourceRef() == null) {
        // no source, we can't do nothing, through a failure...
        throw new DocumentSourceMissingException(new ShardId(request.index(), request.shardId().id()), request.type(), item.id());
    }

    if (item.version() != Versions.MATCH_ANY && item.version() != getResult.getVersion()) {
        throw new VersionConflictEngineException(
                indexShard.shardId(), Constants.DEFAULT_MAPPING_TYPE, item.id(), getResult.getVersion(), item.version());
    }

    Tuple<XContentType, Map<String, Object>> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef(), true);
    final Map<String, Object> updatedSourceAsMap;
    final XContentType updateSourceContentType = sourceAndContent.v1();

    updatedSourceAsMap = sourceAndContent.v2();

    SymbolToFieldExtractorContext ctx = new SymbolToFieldExtractorContext(functions, item.insertValues());

    Map<String, Object> pathsToUpdate = new LinkedHashMap<>();
    Map<String, Object> updatedGeneratedColumns = new LinkedHashMap<>();
    for (int i = 0; i < request.updateColumns().length; i++) {
        /**
         * NOTE: mapping isn't applied. So if an Insert was done using the ES Rest Endpoint
         * the data might be returned in the wrong format (date as string instead of long)
         */
        String columnPath = request.updateColumns()[i];
        Object value = SYMBOL_TO_FIELD_EXTRACTOR.convert(item.updateAssignments()[i], ctx).apply(getResult);
        ReferenceInfo referenceInfo = tableInfo.getReferenceInfo(ColumnIdent.fromPath(columnPath));
        if (referenceInfo instanceof GeneratedReferenceInfo) {
            updatedGeneratedColumns.put(columnPath, value);

        } else {
            pathsToUpdate.put(columnPath, value);
        }
    }

    processGeneratedColumns(tableInfo, pathsToUpdate, updatedGeneratedColumns, request.validateGeneratedColumns(), getResult);

    updateSourceByPaths(updatedSourceAsMap, pathsToUpdate);

    try {
        XContentBuilder builder = XContentFactory.contentBuilder(updateSourceContentType);
        builder.map(updatedSourceAsMap);
        return new SourceAndVersion(builder.bytes(), getResult.getVersion());
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to generate [" + updatedSourceAsMap + "]", e);
    }
}
 
Example 18
Source File: ShardUpsertRequest.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public boolean retryOnConflict() {
    return version == Versions.MATCH_ANY;
}