Java Code Examples for org.elasticsearch.index.mapper.DocumentMapper#parentFieldMapper()

The following examples show how to use org.elasticsearch.index.mapper.DocumentMapper#parentFieldMapper() . 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: ParentChildIndexFieldData.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public ParentChildIndexFieldData(Index index, Settings indexSettings, MappedFieldType.Names fieldNames,
                                 FieldDataType fieldDataType, IndexFieldDataCache cache, MapperService mapperService,
                                 CircuitBreakerService breakerService) {
    super(index, indexSettings, fieldNames, fieldDataType, cache);
    this.breakerService = breakerService;
    if (Version.indexCreated(indexSettings).before(Version.V_2_0_0_beta1)) {
        parentTypes = new TreeSet<>();
        for (DocumentMapper documentMapper : mapperService.docMappers(false)) {
            beforeCreate(documentMapper);
        }
        mapperService.addTypeListener(this);
    } else {
        ImmutableSortedSet.Builder<String> builder = ImmutableSortedSet.naturalOrder();
        for (DocumentMapper mapper : mapperService.docMappers(false)) {
            ParentFieldMapper parentFieldMapper = mapper.parentFieldMapper();
            if (parentFieldMapper.active()) {
                builder.add(parentFieldMapper.type());
            }
        }
        parentTypes = builder.build();
    }
}
 
Example 2
Source File: ParentChildIndexFieldData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeCreate(DocumentMapper mapper) {
    // Remove in 3.0
    synchronized (lock) {
        ParentFieldMapper parentFieldMapper = mapper.parentFieldMapper();
        if (parentFieldMapper.active()) {
            // A _parent field can never be added to an existing mapping, so a _parent field either exists on
            // a new created or doesn't exists. This is why we can update the known parent types via DocumentTypeListener
            if (parentTypes.add(parentFieldMapper.type())) {
                clear();
            }
        }
    }
}
 
Example 3
Source File: ChildrenParser.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException {
    String childType = null;

    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("type".equals(currentFieldName)) {
                childType = parser.text();
            } else {
                throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: ["
                        + currentFieldName + "].", parser.getTokenLocation());
            }
        } else {
            throw new SearchParseException(context, "Unexpected token " + token + " in [" + aggregationName + "].",
                    parser.getTokenLocation());
        }
    }

    if (childType == null) {
        throw new SearchParseException(context, "Missing [child_type] field for children aggregation [" + aggregationName + "]",
                parser.getTokenLocation());
    }

    ValuesSourceConfig<ValuesSource.Bytes.WithOrdinals.ParentChild> config = new ValuesSourceConfig<>(ValuesSource.Bytes.WithOrdinals.ParentChild.class);
    DocumentMapper childDocMapper = context.mapperService().documentMapper(childType);

    String parentType = null;
    Query parentFilter = null;
    Query childFilter = null;
    if (childDocMapper != null) {
        ParentFieldMapper parentFieldMapper = childDocMapper.parentFieldMapper();
        if (!parentFieldMapper.active()) {
            throw new SearchParseException(context, "[children] no [_parent] field not configured that points to a parent type", parser.getTokenLocation());
        }
        parentType = parentFieldMapper.type();
        DocumentMapper parentDocMapper = context.mapperService().documentMapper(parentType);
        if (parentDocMapper != null) {
            // TODO: use the query API
            parentFilter = parentDocMapper.typeFilter();
            childFilter = childDocMapper.typeFilter();
            ParentChildIndexFieldData parentChildIndexFieldData = context.fieldData().getForField(parentFieldMapper.fieldType());
            config.fieldContext(new FieldContext(parentFieldMapper.fieldType().names().indexName(), parentChildIndexFieldData, parentFieldMapper.fieldType()));
        } else {
            config.unmapped(true);
        }
    } else {
        config.unmapped(true);
    }
    return new ParentToChildrenAggregator.Factory(aggregationName, config, parentType, parentFilter, childFilter);
}