@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) {
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 void postProcess(MapperService mapperService) { if (mapperService.getIndexSettings().isSingleType()) { final Collection<String> types = mapperService.types(); assert types.size() <= 1 : types; if (types.isEmpty() == false) { type = types.iterator().next(); } } for (Map.Entry<String, List<Object>> entry : fields().entrySet()) { MappedFieldType fieldType = mapperService.fullName(entry.getKey()); if (fieldType == null) { throw new IllegalStateException("Field [" + entry.getKey() + "] exists in the index but not in mappings"); } List<Object> fieldValues = entry.getValue(); for (int i = 0; i < fieldValues.size(); i++) { fieldValues.set(i, fieldType.valueForDisplay(fieldValues.get(i))); } } }
@Override public IndexFieldData<?> build(IndexSettings indexSettings, MappedFieldType fieldType, IndexFieldDataCache cache, CircuitBreakerService breakerService, MapperService mapperService) { DEPRECATION_LOGGER.deprecated("Fielddata access on the _uid field is deprecated, use _id instead"); MappedFieldType idFieldType = mapperService.fullName(IdFieldMapper.NAME); IndexFieldData<?> idFieldData = idFieldType.fielddataBuilder(fullyQualifiedIndexName) .build(indexSettings, idFieldType, cache, breakerService, mapperService); final String type = mapperService.types().iterator().next(); return new UidIndexFieldData(indexSettings.getIndex(), type, idFieldData); } };
/** * Returns the document mapper created, including a mapping update if the * type has been dynamically created. */ public DocumentMapperForType documentMapperWithAutoCreate(String type) { DocumentMapper mapper = mappers.get(type); if (mapper != null) { return new DocumentMapperForType(mapper, null); } if (!dynamic) { throw new TypeMissingException(index(), new IllegalStateException("trying to auto create mapping, but dynamic mapping is disabled"), type); } mapper = parse(type, null, true); return new DocumentMapperForType(mapper, mapper.mapping()); }
if (entry == null) { QueryScorer queryScorer = new CustomQueryScorer(highlighterContext.query, field.fieldOptions().requireFieldMatch() ? fieldType.name() : null); queryScorer.setExpandMultiTermQuery(true); Fragmenter fragmenter; ArrayList<TextFragment> fragsList = new ArrayList<>(); List<Object> textsToHighlight; Analyzer analyzer = HighlightUtils.getAnalyzer(context.mapperService().documentMapper(hitContext.hit().getType()), fieldType); final int maxAnalyzedOffset = context.indexShard().indexSettings().getHighlightMaxAnalyzedOffset(); deprecationLogger.deprecated( "The length [" + text.length()+ "] of [" + highlighterContext.fieldName + "] field of [" + hitContext.hit().getId() + "] doc of [" + context.indexShard().shardId().getIndexName() + "] index has " + "exceeded the allowed maximum of ["+ maxAnalyzedOffset7 + "] set for the next major Elastic version. " + "This maximum can be set by changing the [" + IndexSettings.MAX_ANALYZED_OFFSET_SETTING.getKey() + hitContext.hit().getId() + "] doc of [" + context.indexShard().shardId().getIndexName() + "] index " + "has exceeded [" + maxAnalyzedOffset + "] - maximum allowed to be analyzed for highlighting. " + "This maximum can be set by changing the [" + IndexSettings.MAX_ANALYZED_OFFSET_SETTING.getKey() + try (TokenStream tokenStream = analyzer.tokenStream(fieldType.name(), text)) { if (!tokenStream.hasAttribute(CharTermAttribute.class) || !tokenStream.hasAttribute(OffsetAttribute.class)) {
Engine.GetResult result = null; try { Term uidTerm = context.mapperService().createUidTerm(request.type(), request.id()); if (uidTerm == null) { return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), false); result = context.indexShard().get(new Engine.Get(false, false, request.type(), request.id(), uidTerm)); if (!result.exists()) { return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), false); context.parsedQuery(context.getQueryShardContext().toQuery(request.query())); context.preProcess(true); int topLevelDocId = result.docIdAndVersion().docId + result.docIdAndVersion().docBase; GetResult getResult = context.indexShard().getService().get(result, request.id(), request.type(), request.storedFields(), request.fetchSourceContext()); return new ExplainResponse(shardId.getIndexName(), request.type(), request.id(), true, explanation, getResult);
for (SearchContextHighlight.Field field : context.highlight().fields()) { Collection<String> fieldNamesToHighlight; if (Regex.isSimpleMatchPattern(field.field())) { fieldNamesToHighlight = context.mapperService().simpleMatchToFullName(field.field()); } else { fieldNamesToHighlight = Collections.singletonList(field.field()); 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);
private boolean isChildHit(SearchHit hit) { DocumentMapper hitDocumentMapper = mapperService.documentMapper(hit.getType()); return documentMapper.type().equals(hitDocumentMapper.parentFieldMapper().type()); } }
@Override public void hitsExecute(SearchContext context, SearchHit[] hits) throws IOException { if (context.collapse() != null) { String name = context.collapse().getFieldName(); if (context.docValueFieldsContext() == null) { context.docValueFieldsContext(new DocValueFieldsContext( Collections.singletonList(new FieldAndFormat(name, DocValueFieldsContext.USE_DEFAULT_FORMAT)))); for (FieldAndFormat fieldAndFormat : context.docValueFieldsContext().fields()) { String field = fieldAndFormat.field; MappedFieldType fieldType = context.mapperService().fullName(field); if (fieldType != null) { final IndexFieldData<?> indexFieldData = context.getForField(fieldType); formatDesc = null; format = fieldType.docValueFormat(formatDesc, null); if (subReaderContext == null || hit.docId() >= subReaderContext.docBase + subReaderContext.reader().maxDoc()) { int readerIndex = ReaderUtil.subIndex(hit.docId(), context.searcher().getIndexReader().leaves()); subReaderContext = context.searcher().getIndexReader().leaves().get(readerIndex); data = indexFieldData.load(subReaderContext); if (hit.fieldsOrNull() == null) { hit.fields(new HashMap<>(2));
if (indexSettings.isSingleType()) { Set<String> actualTypes = new HashSet<>(mappers.keySet()); 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"); MapperMergeValidator.validateMapperStructure(newMapper.type(), objectMappers, fieldMappers, fieldAliasMappers, fullPathObjectMappers, fieldTypes, updateAllTypes); checkPartitionedIndexConstraints(newMapper); checkTotalFieldsLimit(objectMappers.size() + fieldMappers.size() - metadataMappers.length + fieldAliasMappers.size() ); checkNestedFieldsLimit(fullPathObjectMappers); checkDepthLimit(fullPathObjectMappers.keySet()); checkIndexSortCompatibility(indexSettings.getIndexSortConfig(), hasNested); assert assertMappersShareSameFieldType(); assert results.values().stream().allMatch(this::assertSerialization);
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);
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); } }
DocumentMapper existingMapper = mapperService.documentMapper(request.type()); if (MapperService.DEFAULT_MAPPING.equals(request.type())) { newMapper = mapperService.parse(request.type(), mappingUpdateSource, false); } else { newMapper = mapperService.parse(request.type(), mappingUpdateSource, existingMapper == null); if (existingMapper != null) { 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) { throw new IllegalArgumentException("can't add a _parent field that points to an " + "already existing type, that isn't already a parent"); final MapperService mapperService = indexMapperServices.get(index); CompressedXContent existingSource = null; 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(); 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 IndexWarmer.TerminationHandle warmReader(final IndexShard indexShard, final Engine.Searcher searcher) { if (indexSettings.getIndex().equals(indexShard.indexSettings().getIndex()) == false) { final MapperService mapperService = indexShard.mapperService(); for (DocumentMapper docMapper : mapperService.docMappers(false)) { if (docMapper.hasNestedObjects()) { hasNested = true; for (ObjectMapper objectMapper : docMapper.objectMappers().values()) { if (objectMapper.nested().isNested()) { ObjectMapper parentObjectMapper = objectMapper.getParentObjectMapper(mapperService); warmUp.add(Queries.newNonNestedFilter(indexSettings.getIndexVersionCreated())); final long start = System.nanoTime(); getAndLoadIfNotPresent(filterToWarm, ctx); if (indexShard.warmerService().logger().isTraceEnabled()) { indexShard.warmerService().logger().trace("warmed bitset for [{}], took [{}]", filterToWarm, TimeValue.timeValueNanos(System.nanoTime() - start));
private static ParsedDocument parseDocument(IndexShard indexShard, String index, String type, BytesReference doc, XContentType xContentType) { MapperService mapperService = indexShard.mapperService(); DocumentMapperForType docMapper = mapperService.documentMapperWithAutoCreate(type); ParsedDocument parsedDocument = docMapper.getDocumentMapper().parse(source(index, type, "_id_for_tv_api", doc, xContentType)); if (docMapper.getMapping() != null) { parsedDocument.addDynamicMappingsUpdate(docMapper.getMapping()); } return parsedDocument; }
private FieldMapper getMapperForField(String fieldName, SearchContext searchContext, HitContext hitContext) { DocumentMapper documentMapper = searchContext.mapperService().documentMapper(hitContext.hit().type()); // TODO: no need to lookup the doc mapper with unambiguous field names? just look at the mapper service return documentMapper.mappers().smartNameFieldMapper(fieldName); } }
public ReverseNestedAggregator(String name, AggregatorFactories factories, ObjectMapper objectMapper, SearchContext context, Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData) throws IOException { super(name, factories, context, parent, pipelineAggregators, metaData); if (objectMapper == null) { parentFilter = Queries.newNonNestedFilter(context.mapperService().getIndexSettings().getIndexVersionCreated()); } else { parentFilter = objectMapper.nestedTypeFilter(); } parentBitsetProducer = context.bitsetFilterCache().getBitSetProducer(parentFilter); }
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; }