From e862358d901c22b0ca6ccb5da6e475dfc62eb921 Mon Sep 17 00:00:00 2001 From: Robert Stephan Date: Mon, 20 Jul 2026 18:15:04 +0200 Subject: [PATCH] MCR-3774 add TikaMapping to collect metadata into one Solr field --- .../file/tika/MCRCollectingTikaMapper.java | 47 +++++++++++++++++++ .../index/file/tika/MCRSimpleTikaMapper.java | 5 +- .../solr/config/solr/main/solr-schema.json | 6 +++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRCollectingTikaMapper.java diff --git a/mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRCollectingTikaMapper.java b/mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRCollectingTikaMapper.java new file mode 100644 index 0000000000..8b94871c94 --- /dev/null +++ b/mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRCollectingTikaMapper.java @@ -0,0 +1,47 @@ +/* + * This file is part of *** M y C o R e *** + * See https://www.mycore.de/ for details. + * + * MyCoRe is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * MyCoRe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with MyCoRe. If not, see . + */ + +package org.mycore.solr.index.file.tika; + +import org.apache.solr.common.SolrInputDocument; + +import com.fasterxml.jackson.databind.node.ValueNode; + +/** + * A simple implementation of the MCRTikaMapper interface. + * This implementation converts JSON key and value of a Tika metadata field + * into a property-like ('key=value') string and stores it into the Solr field `tika_metadata`; + * If the property "StripNamespace" is set to true, the namespace of the key is removed before mapping. + * If the property "MultiValueField" is set to true, the values are added as multi value fields, otherwise the values + * will be concatenated to a single string with newlines. + * + * @author Robert Stephan + */ +public class MCRCollectingTikaMapper extends MCRSimpleTikaMapper implements MCRTikaMapper { + + private static final String SOLR_FIELD_4_TIKA_METADATA = "tika_metadata"; + + @Override + protected void mapValueNode(SolrInputDocument document, ValueNode vn, String simplifiedKey) { + String value = getValueNodeAsString(vn); + if (value == null || value.isEmpty()) { + return; + } + document.addField(SOLR_FIELD_4_TIKA_METADATA, simplifiedKey + "=" + value); + } +} diff --git a/mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRSimpleTikaMapper.java b/mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRSimpleTikaMapper.java index 751cf28d71..0fd5652285 100644 --- a/mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRSimpleTikaMapper.java +++ b/mycore-solr/src/main/java/org/mycore/solr/index/file/tika/MCRSimpleTikaMapper.java @@ -26,6 +26,7 @@ import com.fasterxml.jackson.core.TreeNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import com.fasterxml.jackson.databind.node.TextNode; import com.fasterxml.jackson.databind.node.ValueNode; /** @@ -104,11 +105,11 @@ private void mapArrayNode(SolrInputDocument document, ArrayNode an, String simpl sb.append(getValueNodeAsString(vn)); } }); - document.addField(simplifiedKey, sb.toString()); + mapValueNode(document, new TextNode(sb.toString()), simplifiedKey); } } - private void mapValueNode(SolrInputDocument document, ValueNode vn, String simplifiedKey) { + protected void mapValueNode(SolrInputDocument document, ValueNode vn, String simplifiedKey) { String value = getValueNodeAsString(vn); if (value == null || value.isEmpty()) { return; diff --git a/mycore-solr/src/main/resources/components/solr/config/solr/main/solr-schema.json b/mycore-solr/src/main/resources/components/solr/config/solr/main/solr-schema.json index e32ebac7ed..c8c0204e99 100644 --- a/mycore-solr/src/main/resources/components/solr/config/solr/main/solr-schema.json +++ b/mycore-solr/src/main/resources/components/solr/config/solr/main/solr-schema.json @@ -739,6 +739,12 @@ "type": "boolean" } }, + { + "add-field": { + "name": "tika_metadata", + "type": "strings" + } + }, { "add-field": { "name": "tika_has_error",