/** * The source of the document (As a map). */ @SuppressWarnings({"unchecked"}) public Map<String, Object> sourceAsMap() throws ElasticsearchParseException { if (source == null) { return null; } if (sourceAsMap != null) { return sourceAsMap; } sourceAsMap = SourceLookup.sourceAsMap(source); return sourceAsMap; }
@Override protected Field[] getFields(IndexReader reader, int docId, String fieldName) throws IOException { // we know its low level reader, and matching docId, since that's how we call the highlighter with SourceLookup sourceLookup = searchContext.lookup().source(); sourceLookup.setSegmentAndDocument((LeafReaderContext) reader.getContext(), docId); List<Object> values = sourceLookup.extractRawValues(fieldType.name()); Field[] fields = new Field[values.size()]; for (int i = 0; i < values.size(); i++) { fields[i] = new Field(fieldType.name(), values.get(i).toString(), TextField.TYPE_NOT_STORED); } return fields; }
public SearchLookup(MapperService mapperService, Function<MappedFieldType, IndexFieldData<?>> fieldDataLookup, @Nullable String[] types) { docMap = new DocLookup(mapperService, fieldDataLookup, types); sourceLookup = new SourceLookup(); fieldsLookup = new FieldsLookup(mapperService, types); }
return null; SourceLookup sourceLookup = new SourceLookup(); sourceLookup.setSource(source); boolean sourceRequested = false; Map<String, DocumentField> fields = null; continue; Object value = sourceLookup.extractValue(field); if (value != null) { if (fields == null) { sourceRequested = true; if (request.fetchSource().includes().length > 0 || request.fetchSource().excludes().length > 0) { Object value = sourceLookup.filter(request.fetchSource()); try { final int initialCapacity = Math.min(1024, sourceAsBytes.length());
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; }
sourceLookup.setSegmentAndDocument(subReaderContext, nestedSubDocId); context.lookup().source().setSource(nestedSourceAsMap); XContentType contentType = tuple.v1(); context.lookup().source().setSourceContentType(contentType);
if (nestedHit == false) { if (fetchSourceContext.includes().length == 0 && fetchSourceContext.excludes().length == 0) { hitContext.hit().sourceRef(source.internalSourceRef()); return; if (source.internalSourceRef() == null) { throw new IllegalArgumentException("unable to fetch fields from _source field: _source is disabled in the mappings " + "for index [" + context.indexShard().shardId().getIndexName() + "]"); Object value = source.filter(fetchSourceContext); if (nestedHit) { value = getNestedSource((Map<String, Object>) value, hitContext); final int initialCapacity = nestedHit ? 1024 : Math.min(1024, source.internalSourceRef().length()); BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity); XContentBuilder builder = new XContentBuilder(source.sourceContentType().xContent(), streamOutput); if (value != null) { builder.value(value);
DocumentMapper documentMapper = context.mapperService().documentMapper(rootFieldsVisitor.uid().type()); SourceLookup sourceLookup = context.lookup().source(); sourceLookup.setSegmentAndDocument(subReaderContext, nestedSubDocId); } while (nested != null); context.lookup().source().setSource(sourceAsMap); XContentType contentType = tuple.v1(); BytesReference nestedSource = contentBuilder(contentType).map(sourceAsMap).bytes(); context.lookup().source().setSource(nestedSource); context.lookup().source().setSourceContentType(contentType); if (extractFieldNames != null) { for (String extractFieldName : extractFieldNames) { List<Object> values = context.lookup().source().extractRawValues(extractFieldName); if (!values.isEmpty()) { if (searchHit.fieldsOrNull() == null) {
Map<String, GetField> fields = null; if (request.fields() != null && request.fields().length > 0) { SourceLookup sourceLookup = new SourceLookup(); sourceLookup.setSource(source); for (String field : request.fields()) { if (field.equals("_source")) { continue; Object value = sourceLookup.extractValue(field); if (value != null) { if (fields == null) {
@Override public boolean containsValue(Object value) { return loadSourceIfNeeded().containsValue(value); }
sourceLookup.setSegmentAndDocument(subReaderContext, subDocId); if (fieldsVisitor.source() != null) { sourceLookup.setSource(fieldsVisitor.source()); List<Object> values = context.lookup().source().extractRawValues(extractFieldName); if (!values.isEmpty()) { if (searchHit.fieldsOrNull() == null) {
protected String getPrivilegedRepositoryName(Map<String,Object> source) { String repositoryName = (String) source.get(REPOSITORY_NAME); String repositoryFormat = (String) source.get(FORMAT); List<Map<String, Object>> assets = (List<Map<String, Object>>) source.getOrDefault("assets", Collections.emptyList()); SourceLookup lookup = new SourceLookup(); lookup.setSource(source); if (assets != null && !assets.isEmpty()) { VariableResolverAdapter variableResolverAdapter = variableResolverAdapterManager.get(repositoryFormat); VariableSource variableSource = variableResolverAdapter.fromSourceLookup(lookup, assets.get(0)); List<String> repositoryNames = repositoryManager.findContainingGroups(repositoryName); repositoryNames.add(0, repositoryName); for (String name : repositoryNames) { if (contentPermissionChecker.isPermitted(name, repositoryFormat, BreadActions.BROWSE, variableSource)) { return name; } } } return null; } }
searchLookup = new SearchLookup(mapperService, null, new String[]{type}); LeafSearchLookup leafSearchLookup = searchLookup.getLeafSearchLookup(docIdAndVersion.context); searchLookup.source().setSource(source); leafSearchLookup.setDocument(docIdAndVersion.docId); List<Object> values = searchLookup.source().extractRawValues(field); if (!values.isEmpty()) { for (int i = 0; i < values.size(); i++) {
@Override public void setSource(Map<String, Object> source) { if (lookup != null) { lookup.source().setSource(source); } }
public void setDocument(int docId) { docMap.setDocument(docId); sourceLookup.setSegmentAndDocument(ctx, docId); fieldsLookup.setDocument(docId); } }
public static String findTermInDoc(int termHash, int docId, String indexFieldName, Analyzer fieldIndexAnalyzer, SearchContext context) { int readerIndex = context.searcher().readerIndex(docId); IndexReader subReader = context.searcher().subReaders()[readerIndex]; int subDoc = docId - context.searcher().docStarts()[readerIndex]; context.lookup().setNextReader(subReader); context.lookup().setNextDocId(subDoc); String candidate; Object value = context.lookup().source().extractValue(indexFieldName); if (value instanceof ArrayList<?>) { for (Object v : (ArrayList<?>) value) { if (v == null) continue; candidate = analyzeStringForTerm(v.toString(), termHash, indexFieldName, fieldIndexAnalyzer); if (candidate != null) { return candidate; } } } else if (value != null) { candidate = analyzeStringForTerm(value.toString(), termHash, indexFieldName, fieldIndexAnalyzer); if (candidate != null) { return candidate; } } throw new ElasticSearchIllegalStateException( "Failed to find hash code " + termHash + " in an array of docId " + docId + ". You can only use stored fields or when you store the original document under _source"); }
context.lookup().source().setSourceContentType(contentType);
return null; SourceLookup sourceLookup = new SourceLookup(); sourceLookup.setSource(source); boolean sourceRequested = false; Map<String, GetField> fields = null; continue; Object value = sourceLookup.extractValue(field); if (value != null) { if (fields == null) { sourceRequested = true; if (request.fetchSource().includes().length > 0 || request.fetchSource().excludes().length > 0) { Object value = sourceLookup.filter(request.fetchSource()); try { final int initialCapacity = Math.min(1024, sourceAsBytes.length());
private SubSearchContext createSubSearchContext(SearchContext context, LeafReaderContext leafReaderContext, BytesReference source, int docId) { SubSearchContext subSearchContext = new SubSearchContext(context); subSearchContext.highlight(new SearchContextHighlight(context.highlight().fields())); // Enforce highlighting by source, because MemoryIndex doesn't support stored fields. subSearchContext.highlight().globalForceSource(true); subSearchContext.lookup().source().setSegmentAndDocument(leafReaderContext, docId); subSearchContext.lookup().source().setSource(source); return subSearchContext; } }
DocumentMapper documentMapper = context.mapperService().documentMapper(rootFieldsVisitor.uid().type()); SourceLookup sourceLookup = context.lookup().source(); sourceLookup.setSegmentAndDocument(subReaderContext, nestedSubDocId); context.lookup().source().setSource(nestedSourceAsMap); XContentType contentType = tuple.v1(); BytesReference nestedSource = contentBuilder(contentType).map(sourceAsMap).bytes(); context.lookup().source().setSource(nestedSource); context.lookup().source().setSourceContentType(contentType);