org.elasticsearch.index.mapper.Uid Java Examples

The following examples show how to use org.elasticsearch.index.mapper.Uid. 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: LuceneQueryBuilder.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Nullable
private Function rewriteAndValidateFields(Function function, Context context) {
    if (function.arguments().size() == 2) {
        Symbol left = function.arguments().get(0);
        Symbol right = function.arguments().get(1);
        if (left.symbolType() == SymbolType.REFERENCE && right.symbolType().isValueSymbol()) {
            Reference ref = (Reference) left;
            if (ref.info().ident().columnIdent().equals(DocSysColumns.ID)) {
                function.setArgument(0,
                        new Reference(DocSysColumns.forTable(ref.ident().tableIdent(), DocSysColumns.UID)));
                function.setArgument(1, Literal.newLiteral(Uid.createUid(Constants.DEFAULT_MAPPING_TYPE,
                        ValueSymbolVisitor.STRING.process(right))));
            } else {
                String unsupportedMessage = context.unsupportedMessage(ref.info().ident().columnIdent().name());
                if (unsupportedMessage != null) {
                    throw new UnsupportedFeatureException(unsupportedMessage);
                }
            }
        }
    }
    return function;
}
 
Example #2
Source File: EngineTestCase.java    From crate with Apache License 2.0 6 votes vote down vote up
protected static ParsedDocument testParsedDocument(
    String id, String routing, ParseContext.Document document, BytesReference source, Mapping mappingUpdate,
    boolean recoverySource) {
    Field uidField = new Field("_id", Uid.encodeId(id), IdFieldMapper.Defaults.FIELD_TYPE);
    Field versionField = new NumericDocValuesField("_version", 0);
    SeqNoFieldMapper.SequenceIDFields seqID = SeqNoFieldMapper.SequenceIDFields.emptySeqID();
    document.add(uidField);
    document.add(versionField);
    document.add(seqID.seqNo);
    document.add(seqID.seqNoDocValue);
    document.add(seqID.primaryTerm);
    BytesRef ref = source.toBytesRef();
    if (recoverySource) {
        document.add(new StoredField(SourceFieldMapper.RECOVERY_SOURCE_NAME, ref.bytes, ref.offset, ref.length));
        document.add(new NumericDocValuesField(SourceFieldMapper.RECOVERY_SOURCE_NAME, 1));
    } else {
        document.add(new StoredField(SourceFieldMapper.NAME, ref.bytes, ref.offset, ref.length));
    }
    return new ParsedDocument(versionField, seqID, id, routing, Arrays.asList(document), source, mappingUpdate);
}
 
Example #3
Source File: VersionFetchSubPhase.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    // it might make sense to cache the TermDocs on a shared fetch context and just skip here)
    // it is going to mean we work on the high level multi reader and not the lower level reader as is
    // the case below...
    long version;
    try {
        BytesRef uid = Uid.createUidAsBytes(hitContext.hit().type(), hitContext.hit().id());
        version = Versions.loadVersion(
                hitContext.readerContext().reader(),
                new Term(UidFieldMapper.NAME, uid)
        );
    } catch (IOException e) {
        throw new ElasticsearchException("Could not query index for _version", e);
    }

    if (version < 0) {
        version = -1;
    }
    hitContext.hit().version(version);
}
 
Example #4
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 #5
Source File: ParentChildFilteredTermsEnum.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected AcceptStatus accept(BytesRef term) throws IOException {
    if (parentTypes.isEmpty()) {
        return AcceptStatus.END;
    }

    BytesRef[] typeAndId = Uid.splitUidIntoTypeAndId(term);
    if (parentTypes.contains(typeAndId[0])) {
        type = typeAndId[0].utf8ToString();
        id = typeAndId[1];
        return AcceptStatus.YES;
    } else {
        BytesRef nextType = parentTypes.ceiling(typeAndId[0]);
        if (nextType == null) {
            return AcceptStatus.END;
        }
        seekTerm = nextType;
        return AcceptStatus.NO_AND_SEEK;
    }
}
 
Example #6
Source File: IndexShard.java    From crate with Apache License 2.0 6 votes vote down vote up
public static Engine.Index prepareIndex(DocumentMapper docMapper,
                                        SourceToParse source,
                                        long seqNo,
                                        long primaryTerm,
                                        long version,
                                        VersionType versionType,
                                        Engine.Operation.Origin origin,
                                        long autoGeneratedIdTimestamp,
                                        boolean isRetry,
                                        long ifSeqNo,
                                        long ifPrimaryTerm) {
    long startTime = System.nanoTime();
    ParsedDocument doc = docMapper.parse(source);
    Term uid = new Term(IdFieldMapper.NAME, Uid.encodeId(doc.id()));
    return new Engine.Index(uid, doc, seqNo, primaryTerm, version, versionType, origin, startTime, autoGeneratedIdTimestamp, isRetry,
                            ifSeqNo, ifPrimaryTerm);
}
 
Example #7
Source File: UidFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void parseCreateField(ParseContext context, List<Field> fields) throws IOException {
    Field uid = new Field(NAME, Uid.createUid(context.stringBuilder(), context.type(), context.id()), Defaults.FIELD_TYPE);
    context.uid(uid);
    fields.add(uid);
    if (fieldType().hasDocValues()) {
        fields.add(new BinaryDocValuesField(NAME, new BytesRef(uid.stringValue())));
    }
}
 
Example #8
Source File: PKLookupOperation.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
public static Doc lookupDoc(IndexShard shard, String id, long version, VersionType versionType, long seqNo, long primaryTerm) {
    Term uidTerm = new Term(IdFieldMapper.NAME, Uid.encodeId(id));
    Engine.Get get = new Engine.Get(id, uidTerm)
        .version(version)
        .versionType(versionType)
        .setIfSeqNo(seqNo)
        .setIfPrimaryTerm(primaryTerm);

    try (Engine.GetResult getResult = shard.get(get)) {
        var docIdAndVersion = getResult.docIdAndVersion();
        if (docIdAndVersion == null) {
            return null;
        }
        SourceFieldVisitor visitor = new SourceFieldVisitor();
        try {
            docIdAndVersion.reader.document(docIdAndVersion.docId, visitor);
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        return new Doc(
            docIdAndVersion.docId,
            shard.shardId().getIndexName(),
            id,
            docIdAndVersion.version,
            docIdAndVersion.seqNo,
            docIdAndVersion.primaryTerm,
            XContentHelper.toMap(visitor.source(), XContentType.JSON),
            () -> visitor.source().utf8ToString()
        );
    }
}
 
Example #9
Source File: FieldsVisitor.java    From crate with Apache License 2.0 5 votes vote down vote up
public Uid uid() {
    if (id == null) {
        return null;
    } else if (type == null) {
        throw new IllegalStateException("Call postProcess before getting the uid");
    }
    return new Uid(type, id);
}
 
Example #10
Source File: FieldsVisitor.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException {
    if (sourceFieldName.equals(fieldInfo.name)) {
        source = new BytesArray(value);
    } else if (IdFieldMapper.NAME.equals(fieldInfo.name)) {
        id = Uid.decodeId(value);
    } else {
        addValue(fieldInfo.name, new BytesRef(value));
    }
}
 
Example #11
Source File: ShardSplittingQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void binaryField(FieldInfo fieldInfo, byte[] value) throws IOException {
    switch (fieldInfo.name) {
        case IdFieldMapper.NAME:
            id = Uid.decodeId(value);
            break;
        default:
            throw new IllegalStateException("Unexpected field: " + fieldInfo.name);
    }
}
 
Example #12
Source File: FieldsVisitor.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void stringField(FieldInfo fieldInfo, byte[] bytes) throws IOException {
    final String value = new String(bytes, StandardCharsets.UTF_8);
    if (UidFieldMapper.NAME.equals(fieldInfo.name)) {
        uid = Uid.createUid(value);
    } else {
        addValue(fieldInfo.name, value);
    }
}
 
Example #13
Source File: DMLProjector.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean setNextRow(Row row) {
    // resolve the Uid
    collectUidExpression.setNextRow(row);
    Uid uid = Uid.createUid(((BytesRef)collectUidExpression.value()).utf8ToString());
    // routing is already resolved
    bulkShardProcessor.addForExistingShard(shardId, createItem(uid.id()), null);
    return true;
}
 
Example #14
Source File: TypeFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    if (indexOptions() == IndexOptions.NONE) {
        return new ConstantScoreQuery(new PrefixQuery(new Term(UidFieldMapper.NAME, Uid.typePrefixAsBytes(BytesRefs.toBytesRef(value)))));
    }
    return new ConstantScoreQuery(new TermQuery(createTerm(value)));
}
 
Example #15
Source File: ParentFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Object valueForSearch(Object value) {
    if (value == null) {
        return null;
    }
    String sValue = value.toString();
    if (sValue == null) {
        return null;
    }
    int index = sValue.indexOf(Uid.DELIMITER);
    if (index == -1) {
        return sValue;
    }
    return sValue.substring(index + 1);
}
 
Example #16
Source File: IdFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query termsQuery(List values, @Nullable QueryParseContext context) {
    if (indexOptions() != IndexOptions.NONE || context == null) {
        return super.termsQuery(values, context);
    }
    return new TermsQuery(UidFieldMapper.NAME, Uid.createUidsForTypesAndIds(context.queryTypes(), values));
}
 
Example #17
Source File: IdFieldMapper.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Query termQuery(Object value, @Nullable QueryParseContext context) {
    if (indexOptions() != IndexOptions.NONE || context == null) {
        return super.termQuery(value, context);
    }
    final BytesRef[] uids = Uid.createUidsForTypesAndId(context.queryTypes(), value);
    return new TermsQuery(UidFieldMapper.NAME, uids);
}
 
Example #18
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public Engine.Delete prepareDeleteOnPrimary(String type, String id, long version, VersionType versionType) {
    if (shardRouting.primary() == false) {
        throw new IllegalIndexShardStateException(shardId, state, "shard is not a primary");
    }
    final DocumentMapper documentMapper = docMapper(type).getDocumentMapper();
    return prepareDelete(type, id, documentMapper.uidMapper().term(Uid.createUid(type, id)), version, versionType, Engine.Operation
            .Origin.PRIMARY);
}
 
Example #19
Source File: IndicesTTLService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void collect(int doc) {
    try {
        FieldsVisitor fieldsVisitor = new FieldsVisitor(false);
        context.reader().document(doc, fieldsVisitor);
        Uid uid = fieldsVisitor.uid();
        final long version = Versions.loadVersion(context.reader(), new Term(UidFieldMapper.NAME, uid.toBytesRef()));
        docsToPurge.add(new DocToPurge(uid.type(), uid.id(), version, fieldsVisitor.routing()));
    } catch (Exception e) {
        logger.trace("failed to collect doc", e);
    }
}
 
Example #20
Source File: FieldsVisitor.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Uid uid() {
    return uid;
}
 
Example #21
Source File: TransportExplainAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
protected ExplainResponse shardOperation(ExplainRequest request, ShardId shardId) {
    IndexService indexService = indicesService.indexServiceSafe(shardId.getIndex());
    IndexShard indexShard = indexService.shardSafe(shardId.id());
    Term uidTerm = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(request.type(), request.id()));
    Engine.GetResult result = indexShard.get(new Engine.Get(false, uidTerm));
    if (!result.exists()) {
        return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), false);
    }

    SearchContext context = new DefaultSearchContext(
            0, new ShardSearchLocalRequest(new String[]{request.type()}, request.nowInMillis, request.filteringAlias()),
            null, result.searcher(), indexService, indexShard,
            scriptService, pageCacheRecycler,
            bigArrays, threadPool.estimatedTimeInMillisCounter(), parseFieldMatcher,
            SearchService.NO_TIMEOUT
    );
    SearchContext.setCurrent(context);

    try {
        context.parsedQuery(indexService.queryParserService().parseQuery(request.source()));
        context.preProcess();
        int topLevelDocId = result.docIdAndVersion().docId + result.docIdAndVersion().context.docBase;
        Explanation explanation = context.searcher().explain(context.query(), topLevelDocId);
        for (RescoreSearchContext ctx : context.rescore()) {
            Rescorer rescorer = ctx.rescorer();
            explanation = rescorer.explain(topLevelDocId, context, ctx, explanation);
        }
        if (request.fields() != null || (request.fetchSourceContext() != null && request.fetchSourceContext().fetchSource())) {
            // Advantage is that we're not opening a second searcher to retrieve the _source. Also
            // because we are working in the same searcher in engineGetResult we can be sure that a
            // doc isn't deleted between the initial get and this call.
            GetResult getResult = indexShard.getService().get(result, request.id(), request.type(), request.fields(), request.fetchSourceContext(), false);
            return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), true, explanation, getResult);
        } else {
            return new ExplainResponse(shardId.getIndex(), request.type(), request.id(), true, explanation);
        }
    } catch (IOException e) {
        throw new ElasticsearchException("Could not explain", e);
    } finally {
        context.close();
        SearchContext.removeCurrent();
    }
}
 
Example #22
Source File: IndexShard.java    From crate with Apache License 2.0 4 votes vote down vote up
private Term extractUidForDelete(String type, String id) {
    // This is only correct because we create types dynamically on delete operations
    // otherwise this could match the same _id from a different type
    BytesRef idBytes = Uid.encodeId(id);
    return new Term(IdFieldMapper.NAME, idBytes);
}
 
Example #23
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public Engine.Delete prepareDeleteOnReplica(String type, String id, long version, VersionType versionType) {
    final DocumentMapper documentMapper = docMapper(type).getDocumentMapper();
    return prepareDelete(type, id, documentMapper.uidMapper().term(Uid.createUid(type, id)), version, versionType, Engine.Operation
            .Origin.REPLICA);
}
 
Example #24
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public TopDocs topDocs(SearchContext context, FetchSubPhase.HitContext hitContext) throws IOException {
    final String field;
    final String term;
    if (isParentHit(hitContext.hit())) {
        field = ParentFieldMapper.NAME;
        term = Uid.createUid(hitContext.hit().type(), hitContext.hit().id());
    } else if (isChildHit(hitContext.hit())) {
        DocumentMapper hitDocumentMapper = mapperService.documentMapper(hitContext.hit().type());
        final String parentType = hitDocumentMapper.parentFieldMapper().type();
        field = UidFieldMapper.NAME;
        SearchHitField parentField = hitContext.hit().field(ParentFieldMapper.NAME);
        if (parentField == null) {
            throw new IllegalStateException("All children must have a _parent");
        }
        term = Uid.createUid(parentType, (String) parentField.getValue());
    } else {
        return Lucene.EMPTY_TOP_DOCS;
    }

    BooleanQuery q = new BooleanQuery.Builder()
        .add(query.query(), Occur.MUST)
        // Only include docs that have the current hit as parent
        .add(new TermQuery(new Term(field, term)), Occur.MUST)
        // Only include docs that have this inner hits type
        .add(documentMapper.typeFilter(), Occur.MUST)
        .build();
    if (size() == 0) {
        final int count = context.searcher().count(q);
        return new TopDocs(count, Lucene.EMPTY_SCORE_DOCS, 0);
    } else {
        int topN = Math.min(from() + size(), context.searcher().getIndexReader().maxDoc());
        TopDocsCollector topDocsCollector;
        if (sort() != null) {
            topDocsCollector = TopFieldCollector.create(sort(), topN, true, trackScores(), trackScores());
        } else {
            topDocsCollector = TopScoreDocCollector.create(topN);
        }
        try {
            context.searcher().search(q, topDocsCollector);
        } finally {
            clearReleasables(Lifetime.COLLECTION);
        }
        return topDocsCollector.topDocs(from(), size());
    }
}
 
Example #25
Source File: EngineTestCase.java    From crate with Apache License 2.0 4 votes vote down vote up
public static Term newUid(String id) {
    return new Term("_id", Uid.encodeId(id));
}