public static SearchHitField readSearchHitField(StreamInput in) throws IOException { SearchHitField result = new SearchHitField(); result.readFrom(in); return result; }
/** * we need to declare parse fields for each metadata field, except for _ID, _INDEX and _TYPE which are * handled individually. All other fields are parsed to an entry in the fields map */ private static void declareMetaDataFields(ObjectParser<Map<String, Object>, Void> parser) { for (String metadatafield : MapperService.getAllMetaFields()) { if (metadatafield.equals(Fields._ID) == false && metadatafield.equals(Fields._INDEX) == false && metadatafield.equals(Fields._TYPE) == false) { parser.declareField((map, field) -> { @SuppressWarnings("unchecked") Map<String, SearchHitField> fieldMap = (Map<String, SearchHitField>) map.computeIfAbsent(Fields.FIELDS, v -> new HashMap<String, SearchHitField>()); fieldMap.put(field.getName(), field); }, (p, c) -> { List<Object> values = new ArrayList<>(); values.add(parseStoredFieldsValue(p)); return new SearchHitField(metadatafield, values); }, new ParseField(metadatafield), ValueType.VALUE); } } }
private static Map<String, SearchHitField> parseFields(XContentParser parser) throws IOException { Map<String, SearchHitField> fields = new HashMap<>(); while ((parser.nextToken()) != XContentParser.Token.END_OBJECT) { String fieldName = parser.currentName(); ensureExpectedToken(XContentParser.Token.START_ARRAY, parser.nextToken(), parser::getTokenLocation); List<Object> values = new ArrayList<>(); while ((parser.nextToken()) != XContentParser.Token.END_ARRAY) { values.add(parseStoredFieldsValue(parser)); } fields.put(fieldName, new SearchHitField(fieldName, values)); } return fields; }
private Map<String, SearchHitField> getSearchFields(SearchContext context, int nestedSubDocId, Set<String> fieldNames, List<String> fieldNamePatterns, LeafReaderContext subReaderContext) { Map<String, SearchHitField> searchFields = null; if (context.hasStoredFields() && !context.storedFieldsContext().fieldNames().isEmpty()) { FieldsVisitor nestedFieldsVisitor = new CustomFieldsVisitor(fieldNames == null ? Collections.emptySet() : fieldNames, fieldNamePatterns == null ? Collections.emptyList() : fieldNamePatterns, false); if (nestedFieldsVisitor != null) { loadStoredFields(context, subReaderContext, nestedFieldsVisitor, nestedSubDocId); nestedFieldsVisitor.postProcess(context.mapperService()); if (!nestedFieldsVisitor.fields().isEmpty()) { searchFields = new HashMap<>(nestedFieldsVisitor.fields().size()); for (Map.Entry<String, List<Object>> entry : nestedFieldsVisitor.fields().entrySet()) { searchFields.put(entry.getKey(), new SearchHitField(entry.getKey(), entry.getValue())); } } } } return searchFields; }
private SearchHit createSearchHit(SearchContext context, FieldsVisitor fieldsVisitor, int docId, int subDocId, LeafReaderContext subReaderContext) { if (fieldsVisitor == null) { return new SearchHit(docId); } loadStoredFields(context, subReaderContext, fieldsVisitor, subDocId); fieldsVisitor.postProcess(context.mapperService()); Map<String, SearchHitField> searchFields = null; if (!fieldsVisitor.fields().isEmpty()) { searchFields = new HashMap<>(fieldsVisitor.fields().size()); for (Map.Entry<String, List<Object>> entry : fieldsVisitor.fields().entrySet()) { searchFields.put(entry.getKey(), new SearchHitField(entry.getKey(), entry.getValue())); } } 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; }
@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))); }
values = Collections.singletonList(value); hitField = new SearchHitField(scriptField.name(), values); hitContext.hit().fields().put(scriptField.name(), hitField);
hitField = new SearchHitField(field, new ArrayList<>(2)); hitContext.hit().fields().put(field, hitField);