if (mapper.type().equals(mapper.parentFieldMapper().type())) { throw new IllegalArgumentException("The [_parent.type] option can't point to the same type"); if (oldMapper == null && newMapper.parentFieldMapper().active()) { if (parentTypes == this.parentTypes) { parentTypes.add(mapper.parentFieldMapper().type());
@Override public ParentFieldMapper build(BuilderContext context) { if (context.indexCreatedVersion().onOrAfter(Version.V_6_5_0)) { throw new MapperParsingException("[_parent] field is disabled on indices with a single type. " + "Use a [join] field instead."); } if (parentType == null) { throw new MapperParsingException("[_parent] field mapping must contain the [type] option"); } name = joinField(parentType); setupFieldType(context); return new ParentFieldMapper(createParentJoinFieldMapper(documentType, context), fieldType, parentType, context.indexSettings()); } }
@Override public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { if (!active()) { return builder; } boolean includeDefaults = params.paramAsBoolean("include_defaults", false); builder.startObject(CONTENT_TYPE); builder.field("type", parentType); if (includeDefaults || fieldType().eagerGlobalOrdinals() == false) { builder.field("eager_global_ordinals", fieldType().eagerGlobalOrdinals()); } builder.endObject(); return builder; }
@Override public MetadataFieldMapper getDefault(MappedFieldType fieldType, ParserContext context) { final Settings indexSettings = context.mapperService().getIndexSettings().getSettings(); final String typeName = context.type(); KeywordFieldMapper parentJoinField = createParentJoinFieldMapper(typeName, new BuilderContext(indexSettings, new ContentPath(0))); MappedFieldType childJoinFieldType = new ParentFieldType(Defaults.FIELD_TYPE, typeName); childJoinFieldType.setName(ParentFieldMapper.NAME); return new ParentFieldMapper(parentJoinField, childJoinFieldType, null, indexSettings); } }
for (DocumentMapper documentMapper : context.getMapperService().docMappers(false)) { ParentFieldMapper parentFieldMapper = documentMapper.parentFieldMapper(); if (parentFieldMapper.active() && type.equals(parentFieldMapper.type())) { childTypes.add(documentMapper.type()); final MappedFieldType parentType = parentDocMapper.parentFieldMapper().getParentJoinFieldType(); final SortedSetDVOrdinalsIndexFieldData fieldData = context.getForField(parentType); return new HasChildQueryBuilder.LateParsingQuery(childrenQuery, HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, ParentFieldMapper.joinField(type), score ? ScoreMode.Max : ScoreMode.None, fieldData,
@Override protected Query doToQuery(QueryShardContext context) throws IOException { DocumentMapper childDocMapper = context.getMapperService().documentMapper(type); if (childDocMapper == null) { if (ignoreUnmapped) { return new MatchNoDocsQuery(); } else { throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]"); } } ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper(); if (parentFieldMapper.active() == false) { throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured"); } String fieldName = ParentFieldMapper.joinField(parentFieldMapper.type()); BooleanQuery.Builder query = new BooleanQuery.Builder(); query.add(new DocValuesTermsQuery(fieldName, id), BooleanClause.Occur.MUST); // Need to take child type into account, otherwise a child doc of different type with the same id could match query.add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.FILTER); return query.build(); }
@Override protected void doMerge(Mapper mergeWith, boolean updateAllTypes) { ParentFieldMapper fieldMergeWith = (ParentFieldMapper) mergeWith; if (fieldMergeWith.parentType != null && Objects.equals(parentType, fieldMergeWith.parentType) == false) { throw new IllegalArgumentException("The _parent field's type option can't be changed: [" + parentType + "]->[" + fieldMergeWith.parentType + "]"); } // If fieldMergeWith is not active it means the user provided a mapping // update that does not explicitly configure the _parent field, so we // ignore it. if (fieldMergeWith.active()) { super.doMerge(mergeWith, updateAllTypes); } }
private void parentFieldResolveConfig(SearchContext context, ValuesSourceConfig<WithOrdinals> config) { DocumentMapper childDocMapper = context.mapperService().documentMapper(childType); if (childDocMapper != null) { ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper(); if (!parentFieldMapper.active()) { throw new IllegalArgumentException("[children] no [_parent] field not configured that points to a parent type"); } String parentType = parentFieldMapper.type(); DocumentMapper parentDocMapper = context.mapperService().documentMapper(parentType); if (parentDocMapper != null) { parentFilter = parentDocMapper.typeFilter(context.getQueryShardContext()); childFilter = childDocMapper.typeFilter(context.getQueryShardContext()); MappedFieldType parentFieldType = parentDocMapper.parentFieldMapper().getParentJoinFieldType(); final SortedSetDVOrdinalsIndexFieldData fieldData = context.getForField(parentFieldType); config.fieldContext(new FieldContext(parentFieldType.name(), fieldData, parentFieldType)); } else { config.unmapped(true); } } else { config.unmapped(true); } }
final Query hitQuery; if (isParentHit(hit)) { String field = ParentFieldMapper.joinField(hit.getType()); hitQuery = new DocValuesTermsQuery(field, hit.getId()); } else if (isChildHit(hit)) { DocumentMapper hitDocumentMapper = mapperService.documentMapper(hit.getType()); final String parentType = hitDocumentMapper.parentFieldMapper().type(); DocumentField parentField = hit.field(ParentFieldMapper.NAME); if (parentField == null) {
for (SearchHit hit : hits) { ParentFieldMapper parentFieldMapper = mapperService.documentMapper(hit.getType()).parentFieldMapper(); if (parentFieldMapper.active()) { parentFields.add(parentFieldMapper.name()); for (SearchHit hit : hits) { ParentFieldMapper parentFieldMapper = mapperService.documentMapper(hit.getType()).parentFieldMapper(); if (parentFieldMapper.active() == false) { continue; SortedDocValues values = docValuesMap.get(parentFieldMapper.name()); if (values != null && values.advanceExact(docId)) { BytesRef binaryValue = values.binaryValue();
private boolean isParentHit(SearchHit hit) { return hit.getType().equals(documentMapper.parentFieldMapper().type()); }
static KeywordFieldMapper createParentJoinFieldMapper(String docType, BuilderContext context) { KeywordFieldMapper.Builder parentJoinField = new KeywordFieldMapper.Builder(joinField(docType)); parentJoinField.indexOptions(IndexOptions.NONE); parentJoinField.docValues(true); parentJoinField.fieldType().setDocValuesType(DocValuesType.SORTED); return parentJoinField.build(context); }
if (parentMapper.active() && fieldsToIdx.getOrDefault(ParentFieldMapper.NAME, -1) == -1) { String parent = null; if (parentMapper.pkColumns() != null) { String[] cols = parentMapper.pkColumns().split(","); if (cols.length == 1) { parent = (String) values[fieldsToIdx.get(cols[0])]; parentMapper.createField(context, parent); context.parent(parent); if (!parentMapper.active()) { parentMapper.createField(context, null);
case ParentFieldMapper.NAME: ParentFieldMapper parentMapper = docMapper.parentFieldMapper(); if (parentMapper.active()) { query.append(query.length() > 7 ? ',':' '); if (parentMapper.pkColumns() == null) { query.append( (parentMapper.pkColumns().indexOf(',') > 0) ? "toJsonArray(" : "toString(") .append(parentMapper.pkColumns()) .append(") as \"_parent\"");
@Override public void postParse(ParseContext context) throws IOException { parse(context); }
public static String getParentId(ParentFieldMapper fieldMapper, LeafReader reader, int docId) { try { SortedDocValues docValues = reader.getSortedDocValues(fieldMapper.name()); if (docValues == null || docValues.advanceExact(docId) == false) { // hit has no _parent field. return null; } BytesRef parentId = docValues.binaryValue(); return parentId.length > 0 ? parentId.utf8ToString() : null; } catch (IOException e) { throw ExceptionsHelper.convertToElastic(e); } } }
for (DocumentMapper documentMapper : context.getMapperService().docMappers(false)) { ParentFieldMapper parentFieldMapper = documentMapper.parentFieldMapper(); if (parentFieldMapper.active() && type.equals(parentFieldMapper.type())) { childTypes.add(documentMapper.type()); final MappedFieldType parentType = parentDocMapper.parentFieldMapper().getParentJoinFieldType(); final SortedSetDVOrdinalsIndexFieldData fieldData = context.getForField(parentType); return new HasChildQueryBuilder.LateParsingQuery(childrenQuery, HasChildQueryBuilder.DEFAULT_MIN_CHILDREN, HasChildQueryBuilder.DEFAULT_MAX_CHILDREN, ParentFieldMapper.joinField(type), score ? ScoreMode.Max : ScoreMode.None, fieldData,
/** * Creates parent_id query from a {@link ParentFieldMapper} * Only used for BWC with multi-types indices */ private Query doToQueryBWC(QueryShardContext context) throws IOException { DocumentMapper childDocMapper = context.getMapperService().documentMapper(type); if (childDocMapper == null) { if (ignoreUnmapped) { return new MatchNoDocsQuery(); } else { throw new QueryShardException(context, "[" + NAME + "] no mapping found for type [" + type + "]"); } } ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper(); if (parentFieldMapper.active() == false) { throw new QueryShardException(context, "[" + NAME + "] _parent field has no parent type configured"); } String fieldName = ParentFieldMapper.joinField(parentFieldMapper.type()); return new BooleanQuery.Builder() .add(new DocValuesTermsQuery(fieldName, id), BooleanClause.Occur.MUST) // Need to take child type into account, otherwise a child doc of different type with the same id could match .add(new TermQuery(new Term(TypeFieldMapper.NAME, type)), BooleanClause.Occur.FILTER) .build(); }
public MappingMetaData(DocumentMapper docMapper) { this.type = docMapper.type(); this.source = docMapper.mappingSource(); this.routing = new Routing(docMapper.routingFieldMapper().required()); this.hasParentField = docMapper.parentFieldMapper().active(); }
private void parentFieldResolveConfig(SearchContext context, ValuesSourceConfig<WithOrdinals> config) { DocumentMapper childDocMapper = context.mapperService().documentMapper(childType); if (childDocMapper != null) { ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper(); if (!parentFieldMapper.active()) { throw new IllegalArgumentException("[children] no [_parent] field not configured that points to a parent type"); } String parentType = parentFieldMapper.type(); DocumentMapper parentDocMapper = context.mapperService().documentMapper(parentType); if (parentDocMapper != null) { parentFilter = parentDocMapper.typeFilter(context.getQueryShardContext()); childFilter = childDocMapper.typeFilter(context.getQueryShardContext()); MappedFieldType parentFieldType = parentDocMapper.parentFieldMapper().getParentJoinFieldType(); final SortedSetDVOrdinalsIndexFieldData fieldData = context.getForField(parentFieldType); config.fieldContext(new FieldContext(parentFieldType.name(), fieldData, parentFieldType)); } else { config.unmapped(true); } } else { config.unmapped(true); } }