Refine search
public Builder(RootObjectMapper.Builder builder, MapperService mapperService) { final Settings indexSettings = mapperService.getIndexSettings().getSettings(); this.builderContext = new Mapper.BuilderContext(indexSettings, new ContentPath(1)); this.rootObjectMapper = builder.build(builderContext); final String type = rootObjectMapper.name(); final DocumentMapper existingMapper = mapperService.documentMapper(type); final Map<String, TypeParser> metadataMapperParsers = mapperService.mapperRegistry.getMetadataMapperParsers(); for (Map.Entry<String, MetadataFieldMapper.TypeParser> entry : metadataMapperParsers.entrySet()) { final String name = entry.getKey(); final MetadataFieldMapper existingMetadataMapper = existingMapper == null ? null : (MetadataFieldMapper) existingMapper.mappers().getMapper(name); final MetadataFieldMapper metadataMapper; if (existingMetadataMapper == null) { final TypeParser parser = entry.getValue(); metadataMapper = parser.getDefault(mapperService.fullName(name), mapperService.documentMapperParser().parserContext(builder.name())); } else { metadataMapper = existingMetadataMapper; } metadataMappers.put(metadataMapper.getClass(), metadataMapper); } }
public static Engine.Index prepareIndex(DocumentMapperForType docMapper, Version indexCreatedVersion, 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.getDocumentMapper().parse(source); if (docMapper.getMapping() != null) { doc.addDynamicMappingsUpdate(docMapper.getMapping()); } Term uid; if (indexCreatedVersion.onOrAfter(Version.V_6_0_0_beta1)) { uid = new Term(IdFieldMapper.NAME, Uid.encodeId(doc.id())); } else if (docMapper.getDocumentMapper().idFieldMapper().fieldType().indexOptions() != IndexOptions.NONE) { uid = new Term(IdFieldMapper.NAME, doc.id()); } else { uid = new Term(UidFieldMapper.NAME, Uid.createUidAsBytes(doc.type(), doc.id())); } return new Engine.Index(uid, doc, seqNo, primaryTerm, version, versionType, origin, startTime, autoGeneratedIdTimestamp, isRetry, ifSeqNo, ifPrimaryTerm); }
assert defaultMapper.type().equals(DEFAULT_MAPPING); mappers.put(DEFAULT_MAPPING, defaultMapper); results.put(DEFAULT_MAPPING, defaultMapper); documentMappers.forEach(mapper -> actualTypes.add(mapper.type())); actualTypes.remove(DEFAULT_MAPPING); if (actualTypes.size() > 1) { throw new IllegalArgumentException( "Rejecting mapping update to [" + index().getName() + "] as the final mapping would have more than 1 type: " + actualTypes); validateTypeName(mapper.type()); if (mapper.type().equals(mapper.parentFieldMapper().type())) { throw new IllegalArgumentException("The [_parent.type] option can't point to the same type"); DocumentMapper oldMapper = mappers.get(mapper.type()); DocumentMapper newMapper; if (oldMapper != null) { newMapper = oldMapper.merge(mapper.mapping(), updateAllTypes); } else { newMapper = mapper; List<FieldMapper> fieldMappers = new ArrayList<>(); List<FieldAliasMapper> fieldAliasMappers = new ArrayList<>(); MetadataFieldMapper[] metadataMappers = newMapper.mapping().metadataMappers; Collections.addAll(fieldMappers, metadataMappers); MapperUtils.collect(newMapper.mapping().root(), objectMappers, fieldMappers, fieldAliasMappers); MapperMergeValidator.validateMapperStructure(newMapper.type(), objectMappers, fieldMappers,
private boolean assertSerialization(DocumentMapper mapper) { // capture the source now, it may change due to concurrent parsing final CompressedXContent mappingSource = mapper.mappingSource(); DocumentMapper newMapper = parse(mapper.type(), mappingSource, false); if (newMapper.mappingSource().equals(mappingSource) == false) { throw new IllegalStateException("DocumentMapper serialization result is different from source. \n--> Source [" + mappingSource + "]\n--> Result [" + newMapper.mappingSource() + "]"); } return true; }
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 checkPartitionedIndexConstraints(DocumentMapper newMapper) { if (indexSettings.getIndexMetaData().isRoutingPartitionedIndex()) { if (newMapper.parentFieldMapper().active()) { throw new IllegalArgumentException("mapping type name [" + newMapper.type() + "] cannot have a " + "_parent field for the partitioned index [" + indexSettings.getIndex().getName() + "]"); } if (!newMapper.routingFieldMapper().required()) { throw new IllegalArgumentException("mapping type [" + newMapper.type() + "] must have routing " + "required for partitioned index [" + indexSettings.getIndex().getName() + "]"); } } }
@Override public void hitExecute(SearchContext context, HitContext hitContext) { if (context.highlight() == null) { return; if (context.highlight().forceSource(field)) { SourceFieldMapper sourceFieldMapper = context.mapperService().documentMapper(hitContext.hit().getType()).sourceMapper(); if (!sourceFieldMapper.enabled()) { throw new IllegalArgumentException("source is forced for fields " + fieldNamesToHighlight + " but type [" + hitContext.hit().getType() + "] has disabled _source"); MappedFieldType fieldType = context.mapperService().fullName(fieldName); if (fieldType == null) { continue; if (fieldType.typeName().equals(TextFieldMapper.CONTENT_TYPE) == false && fieldType.typeName().equals(KeywordFieldMapper.CONTENT_TYPE) == false) { continue; highlightQuery = context.parsedQuery().query(); HighlighterContext highlighterContext = new HighlighterContext(fieldType.name(), field, fieldType, context, hitContext, highlightQuery); hitContext.hit().highlightFields(highlightFields);
@Override public void hitsExecute(SearchContext context, SearchHit[] hits) throws IOException { if (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false) { return ; MapperService mapperService = context.mapperService(); Set<String> parentFields = new HashSet<>(); for (SearchHit hit : hits) { ParentFieldMapper parentFieldMapper = mapperService.documentMapper(hit.getType()).parentFieldMapper(); if (parentFieldMapper.active()) { parentFields.add(parentFieldMapper.name()); Map<String, SortedDocValues> docValuesMap = new HashMap<>(); for (SearchHit hit : hits) { ParentFieldMapper parentFieldMapper = mapperService.documentMapper(hit.getType()).parentFieldMapper(); if (parentFieldMapper.active() == false) { continue; int readerId = ReaderUtil.subIndex(hit.docId(), context.searcher().getIndexReader().leaves()); LeafReaderContext subReaderContext = context.searcher().getIndexReader().leaves().get(readerId); if (lastReaderId != readerId) {
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); } }
private boolean isChildHit(SearchHit hit) { DocumentMapper hitDocumentMapper = mapperService.documentMapper(hit.getType()); return documentMapper.type().equals(hitDocumentMapper.parentFieldMapper().type()); } }
private SearchHit createSearchHit(SearchContext context, FieldsVisitor fieldsVisitor, int docId, int subDocId, Map<String, Set<String>> storedToRequestedFields, LeafReaderContext subReaderContext) { if (fieldsVisitor == null) { return new SearchHit(docId); } Map<String, DocumentField> searchFields = getSearchFields(context, fieldsVisitor, subDocId, storedToRequestedFields, subReaderContext); DocumentMapper documentMapper = context.mapperService().documentMapper(fieldsVisitor.uid().type()); Text typeText; if (documentMapper == null) { typeText = new Text(fieldsVisitor.uid().type()); } else { typeText = documentMapper.typeText(); } SearchHit searchHit = new SearchHit(docId, fieldsVisitor.uid().id(), typeText, searchFields); // Set _source if requested. SourceLookup sourceLookup = context.lookup().source(); sourceLookup.setSegmentAndDocument(subReaderContext, subDocId); if (fieldsVisitor.source() != null) { sourceLookup.setSource(fieldsVisitor.source()); } return searchHit; }
"] to be of type [percolator], but is of type [" + fieldType.typeName() + "]"); final MapperService mapperService = context.getMapperService(); if (context.getIndexSettings().isSingleType()) { Collection<String> types = mapperService.types(); if (types.size() != 1) { throw new IllegalStateException("Only a single type should exist, but [" + types.size() + " types exists"); docMapper = mapperService.documentMapper(type); for (BytesReference document : documents) { docs.add(docMapper.parse(source(context.index().getName(), type, "_temp_id", document, documentXContentType))); throw new IllegalArgumentException("[percolate] query is missing required [document_type] parameter"); DocumentMapperForType docMapperForType = mapperService.documentMapperWithAutoCreate(documentType); docMapper = docMapperForType.getDocumentMapper(); for (BytesReference document : documents) { docs.add(docMapper.parse(source(context.index().getName(), documentType, "_temp_id", document, documentXContentType))); FieldNameAnalyzer fieldNameAnalyzer = (FieldNameAnalyzer) docMapper.mappers().indexAnalyzer(); final IndexSearcher docSearcher; if (docs.size() > 1 || docs.get(0).docs().size() > 1) { assert docs.size() != 1 || docMapper.hasNestedObjects(); docSearcher = createMultiDocumentSearcher(analyzer, docs); } else {
final boolean needSource = context.sourceRequested() || context.highlight() != null; if (needSource || (context instanceof InnerHitsContext.InnerHitSubContext == false)) { FieldsVisitor rootFieldsVisitor = new FieldsVisitor(needSource); loadStoredFields(context, subReaderContext, rootFieldsVisitor, rootSubDocId); rootFieldsVisitor.postProcess(context.mapperService()); uid = rootFieldsVisitor.uid(); source = rootFieldsVisitor.source(); assert context.mapperService().types().size() == 1; typeText = context.mapperService().types().iterator().next(); DocumentMapper documentMapper = context.mapperService().documentMapper(typeText); SourceLookup sourceLookup = context.lookup().source(); sourceLookup.setSegmentAndDocument(subReaderContext, nestedSubDocId); ObjectMapper nestedObjectMapper = documentMapper.findNestedObjectMapper(nestedSubDocId, context, subReaderContext); assert nestedObjectMapper != null; SearchHit.NestedIdentity nestedIdentity = context.lookup().source().setSourceContentType(contentType); return new SearchHit(nestedTopDocId, uid.id(), documentMapper.typeText(), nestedIdentity, searchFields);
@Override public void hitExecute(SearchContext context, HitContext hitContext) { if (context.highlight() == null) { return; for (SearchContextHighlight.Field field : context.highlight().fields()) { Collection<String> fieldNamesToHighlight; if (Regex.isSimpleMatchPattern(field.field())) { DocumentMapper documentMapper = context.mapperService().documentMapper(hitContext.hit().type()); fieldNamesToHighlight = documentMapper.mappers().simpleMatchToFullName(field.field()); } else { fieldNamesToHighlight = Collections.singletonList(field.field()); if (context.highlight().forceSource(field)) { SourceFieldMapper sourceFieldMapper = context.mapperService().documentMapper(hitContext.hit().type()).sourceMapper(); if (!sourceFieldMapper.enabled()) { throw new IllegalArgumentException("source is forced for fields " + fieldNamesToHighlight + " but type [" + hitContext.hit().type() + "] has disabled _source"); if (fieldMapper.fieldType().typeName().equals(TextFieldMapper.CONTENT_TYPE) == false && fieldMapper.fieldType().typeName().equals(KeywordFieldMapper.CONTENT_TYPE) == false && fieldMapper.fieldType().typeName().equals(StringFieldMapper.CONTENT_TYPE) == false) { continue;
private boolean isParentHit(SearchHit hit) { return hit.getType().equals(documentMapper.parentFieldMapper().type()); }
final FieldsVisitor rootFieldsVisitor = new FieldsVisitor(context.sourceRequested() || context.highlight() != null); loadStoredFields(context, subReaderContext, rootFieldsVisitor, rootSubDocId); rootFieldsVisitor.postProcess(context.mapperService()); DocumentMapper documentMapper = context.mapperService().documentMapper(rootFieldsVisitor.uid().type()); SourceLookup sourceLookup = context.lookup().source(); sourceLookup.setSegmentAndDocument(subReaderContext, nestedSubDocId); ObjectMapper nestedObjectMapper = documentMapper.findNestedObjectMapper(nestedSubDocId, context, subReaderContext); assert nestedObjectMapper != null; SearchHit.NestedIdentity nestedIdentity = getInternalNestedIdentity(context, nestedSubDocId, subReaderContext, documentMapper, nestedObjectMapper); return new SearchHit(nestedTopDocId, rootFieldsVisitor.uid().id(), documentMapper.typeText(), nestedIdentity, searchFields);
existingMapper.merge(newMapper.mapping(), request.updateAllTypes()); } else { if (newMapper.parentFieldMapper().active()) { for (ObjectCursor<MappingMetaData> mapping : indexMetaData.getMappings().values()) { String parentType = newMapper.parentFieldMapper().type(); if (parentType.equals(mapping.value.type()) && mapperService.getParentTypes().contains(parentType) == false) { mappingType = newMapper.type(); } else if (mappingType.equals(newMapper.type()) == false) { throw new InvalidTypeNameException("Type name provided does not match type name within mapping definition"); DocumentMapper existingMapper = mapperService.documentMapper(mappingType); if (existingMapper != null) { existingSource = existingMapper.mappingSource(); DocumentMapper mergedMapper = mapperService.merge(mappingType, mappingUpdateSource, MergeReason.MAPPING_UPDATE, request.updateAllTypes()); CompressedXContent updatedSource = mergedMapper.mappingSource(); logger.debug("{} update_mapping [{}] with source [{}]", index, mergedMapper.type(), updatedSource); } else if (logger.isInfoEnabled()) { logger.info("{} update_mapping [{}]", index, mergedMapper.type()); for (DocumentMapper mapper : mapperService.docMappers(true)) { indexMetaDataBuilder.putMapping(new MappingMetaData(mapper.mappingSource()));
@Override public void hitExecute(SearchContext context, HitContext hitContext) { if (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false) { return ; } ParentFieldMapper parentFieldMapper = context.mapperService().documentMapper(hitContext.hit().type()).parentFieldMapper(); if (parentFieldMapper.active() == false) { return; } String parentId = getParentId(parentFieldMapper, hitContext.reader(), hitContext.docId()); if (parentId == null) { // hit has no _parent field. Can happen for nested inner hits if parent hit is a p/c document. return; } Map<String, SearchHitField> fields = hitContext.hit().fieldsOrNull(); if (fields == null) { fields = new HashMap<>(); hitContext.hit().fields(fields); } fields.put(ParentFieldMapper.NAME, new SearchHitField(ParentFieldMapper.NAME, Collections.singletonList(parentId))); }
@Override public void hitExecute(SearchContext context, HitContext hitContext) { Map<String, HighlightField> highlightFields = newHashMap(); for (SearchContextHighlight.Field field : context.highlight().fields()) { Collection<String> fieldNamesToHighlight; if (Regex.isSimpleMatchPattern(field.field())) { DocumentMapper documentMapper = context.mapperService().documentMapper(hitContext.hit().type()); fieldNamesToHighlight = documentMapper.mappers().simpleMatchToFullName(field.field()); } else { fieldNamesToHighlight = Collections.singletonList(field.field()); if (context.highlight().forceSource(field)) { SourceFieldMapper sourceFieldMapper = context.mapperService().documentMapper(hitContext.hit().type()).sourceMapper(); if (!sourceFieldMapper.enabled()) { throw new IllegalArgumentException("source is forced for fields " + fieldNamesToHighlight + " but type [" + hitContext.hit().type() + "] has disabled _source"); if (fieldMapper.fieldType().typeName().equals(StringFieldMapper.CONTENT_TYPE) == false) { continue;
@Override protected Query doToQuery(QueryShardContext context) throws IOException { //LUCENE 4 UPGRADE document mapper should use bytesref as well? DocumentMapper documentMapper = context.getMapperService().documentMapper(type); if (documentMapper == null) { // no type means no documents return new MatchNoDocsQuery(); } else { return documentMapper.typeFilter(context); } }