Package io.goobi.viewer.solr
Class SolrTools
java.lang.Object
io.goobi.viewer.solr.SolrTools
Utility class providing static helpers for constructing Solr queries, parsing responses, and field value extraction.
-
Method Summary
Modifier and TypeMethodDescriptionstatic String[]checkSolrSchemaName.static StringcleanUpQuery(String query) static StringescapeSpecialCharacters(String string) Escapes all special characters used by SOLR.static StringextractExceptionMessageHtmlTitle(String exceptionMessage) static StringSolr supports dynamic random_* sorting fields.static BooleangetAsBoolean(Object fieldValue) static IntegergetAsInt.static LonggetAsLong.static StringgetAsString(Object fieldValue) Parses a Solr-Field value in order to return it as String.static StringgetAsString(Object fieldValue, String separator) getAvailableValuesForField(String field, String filterQuery) getAvailableValuesForField.getAvailableValuesForField(String field, String filterQuery, boolean useFacetField) getAvailableValuesForField.static StringgetBaseFieldName(String fieldName) getFieldValueMap(org.apache.solr.common.SolrDocument doc) Converts the given SolrDocument to a value map.static StringgetLanguage(String fieldName) static LocalegetMetadataValues(org.apache.solr.common.SolrDocument doc, String fieldName) Returns a list with all (string) values for the given field name in the given SolrDocument.getMetadataValuesForLanguage(StructElement doc, String key) getMetadataValuesForLanguage.getMetadataValuesForLanguage(org.apache.solr.common.SolrDocument doc, String key) getMetadataValuesForLanguage.getMultiLanguageFieldValueMap(org.apache.solr.common.SolrDocument doc) Converts the given SolrDocument to a value map.static List<de.intranda.metadata.multilanguage.IMetadataValue> getMultiLanguageMetadata(Map<String, List<String>> mdValues) getMultiLanguageMetadata.static StringgetProcessedConditions(String conditions) static final StringgetReferenceId(org.apache.solr.common.SolrDocument doc) static booleangetSingleFieldBooleanValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldBooleanValue.static IntegergetSingleFieldIntegerValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldIntegerValue.static LonggetSingleFieldLongValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldLongValue.static StringgetSingleFieldStringValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldStringValue.static ObjectgetSingleFieldValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldValue.static List<StringPair> getSolrSortFieldsAsList(String solrSortFields, String splitFieldsBy, String splitNameOrderBy) Returns the comma-separated sorting fields insolrSortFieldsas a List. getTranslatedMetadata(org.apache.solr.common.SolrDocument doc, Function<String, Boolean> fieldNameFilter) getTranslatedMetadata(org.apache.solr.common.SolrDocument doc, Map<String, List<de.intranda.metadata.multilanguage.IMetadataValue>> metadata, Locale documentLocale, Function<String, Boolean> fieldNameFilter) static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, StructElement doc, BinaryOperator<String> combiner) getTranslations.static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, StructElement doc, List<Locale> translationLocales, BinaryOperator<String> combiner) getTranslations.static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, org.apache.solr.common.SolrDocument doc) getTranslations.static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, org.apache.solr.common.SolrDocument doc, BinaryOperator<String> combiner) getTranslations.static booleanstatic booleanisAnchor(org.apache.solr.common.SolrDocument doc) static booleanisGroup(MetadataContainer doc) static booleanisGroup(org.apache.solr.common.SolrDocument doc) static booleanisHasImages(org.apache.solr.common.SolrDocument doc) isHasImages.static booleanisHasWrongLanguageCode(String field, String language) static booleanisLanguageCodedField(String fieldName) static booleanisQuerySyntaxError.static booleanisWork(MetadataContainer doc) static booleanisWork(org.apache.solr.common.SolrDocument doc) static StringunescapeSpecialCharacters(String string) Reverts the operation ofescapeSpecialCharacters(String).
-
Method Details
-
getSolrSortFieldsAsList
public static List<StringPair> getSolrSortFieldsAsList(String solrSortFields, String splitFieldsBy, String splitNameOrderBy) Returns the comma-separated sorting fields insolrSortFieldsas a List. - Parameters:
solrSortFields- comma-separated Solr sort field string to parsesplitFieldsBy- String by which the individual field configurations are splitsplitNameOrderBy- String by which the field name and sorting order are split- Returns:
- a list of field name/sort order pairs parsed from the given sort field string
-
getAsString
Parses a Solr-Field value in order to return it as String.- Parameters:
fieldValue- Solr field value to convert- Returns:
- the string representation of the given Solr field value
-
getAsBoolean
- Parameters:
fieldValue- Solr field value to convert- Returns:
- Boolean
-
getAsString
- Parameters:
fieldValue- Solr field value to convertseparator- string used to join multiple values- Returns:
- String
-
getAsInt
getAsInt.- Parameters:
fieldValue- Solr field value to convert- Returns:
- the integer value, or null if the input is null or cannot be parsed
-
getAsLong
getAsLong.- Parameters:
fieldValue- Solr field value to convert- Returns:
- the long value, or null if the input is null or cannot be parsed
-
getSingleFieldValue
getSingleFieldValue.- Parameters:
doc- Solr document to read fromfield- Solr field name to retrieve- Returns:
- the first value for the given field in the document, or null if not present
-
getSingleFieldStringValue
public static String getSingleFieldStringValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldStringValue.- Parameters:
doc- Solr document to read fromfield- Solr field name to retrieve- Returns:
- the string value of the given Solr field, or null if the field is absent or its value is null
-
getSingleFieldIntegerValue
public static Integer getSingleFieldIntegerValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldIntegerValue.- Parameters:
doc- Solr document to read fromfield- Solr field name to retrieve- Returns:
- the integer value of the field, or null if not present or not parseable
-
getSingleFieldLongValue
getSingleFieldLongValue.- Parameters:
doc- Solr document to read fromfield- Solr field name to retrieve- Returns:
- the long value of the field, or null if not present or not parseable
-
getSingleFieldBooleanValue
public static boolean getSingleFieldBooleanValue(org.apache.solr.common.SolrDocument doc, String field) getSingleFieldBooleanValue.- Parameters:
doc- Solr document to read fromfield- Solr field name to retrieve- Returns:
- the boolean value of the field, or false if not present or not a boolean
-
getMetadataValues
public static List<String> getMetadataValues(org.apache.solr.common.SolrDocument doc, String fieldName) Returns a list with all (string) values for the given field name in the given SolrDocument.- Parameters:
doc- Solr document to read fromfieldName- Solr field name to retrieve values for- Returns:
- a list of all string values for the given field in the given Solr document
-
getFieldValueMap
Converts the given SolrDocument to a value map. IMAGEURN_OAI and PAGEURNS are not returned because they have no relevance in this application and can get quite large.- Parameters:
doc- Solr document to convert to a value map- Returns:
- a map of field names to their string values, excluding IMAGEURN_OAI and PAGEURNS
-
getMultiLanguageFieldValueMap
public static Map<String,List<de.intranda.metadata.multilanguage.IMetadataValue>> getMultiLanguageFieldValueMap(org.apache.solr.common.SolrDocument doc) Converts the given SolrDocument to a value map. IMAGEURN_OAI and PAGEURNS are not returned because they have no relevance in this application and can get quite large.- Parameters:
doc- Solr document to convert to a multi-language value map- Returns:
- a map of field names to their multi-language metadata values, excluding IMAGEURN_OAI and PAGEURNS
-
getMultiLanguageMetadata
public static List<de.intranda.metadata.multilanguage.IMetadataValue> getMultiLanguageMetadata(Map<String, List<String>> mdValues) getMultiLanguageMetadata.- Parameters:
mdValues- map of language codes to string value lists- Returns:
- a list of multi-language metadata values built from the given language-to-values map
-
getMetadataValuesForLanguage
public static Map<String,List<String>> getMetadataValuesForLanguage(org.apache.solr.common.SolrDocument doc, String key) getMetadataValuesForLanguage.- Parameters:
doc- The document containing the metadatakey- the metadata key without the '_LANG_...' suffix- Returns:
- A map with keys for each language and lists of all found metadata values for this language. Metadata that match the given key but have
no language information are listed as language
_DEFAULT
-
getMetadataValuesForLanguage
getMetadataValuesForLanguage.- Parameters:
doc- The document containing the metadatakey- the metadata key without the '_LANG_...' suffix- Returns:
- A map with keys for each language and lists of all found metadata values for this language. Metadata that match the given key but have
no language information are listed as language
_DEFAULT
-
isGroup
public static boolean isGroup(org.apache.solr.common.SolrDocument doc) - Parameters:
doc- Solr document to inspect- Returns:
- true if doc contains DOCTYPE:GROUP; false otherwise
-
isGroup
- Parameters:
doc- metadata container to inspect- Returns:
- true if doc contains DOCTYPE:GROUP; false otherwise
-
isAnchor
public static boolean isAnchor(org.apache.solr.common.SolrDocument doc) - Parameters:
doc- Solr document to inspect- Returns:
- true if doc contains ISANCHOR:true; false otherwise
-
isAnchor
- Parameters:
doc- metadata container to inspect- Returns:
- true if doc contains ISANCHOR:true; false otherwise
-
isWork
public static boolean isWork(org.apache.solr.common.SolrDocument doc) - Parameters:
doc- Solr document to inspect- Returns:
- true if doc contains ISWORK:true; false otherwise
-
isWork
- Parameters:
doc- metadata container to inspect- Returns:
- true if doc contains ISWORK:true; false otherwise
-
isLanguageCodedField
- Parameters:
fieldName- Solr field name to check for language encoding- Returns:
- true if fieldName contains _LANG_; false otherwise
-
isHasWrongLanguageCode
- Parameters:
field- Solr field name potentially containing a language suffixlanguage- two- or three-letter language code to compare against- Returns:
- true if language code different
-
isQuerySyntaxError
isQuerySyntaxError.- Parameters:
e- exception thrown by Solr to inspect- Returns:
- true if the exception indicates a Solr query syntax or validation error (HTTP 400), false otherwise
-
extractExceptionMessageHtmlTitle
- Parameters:
exceptionMessage- raw exception message string to parse- Returns:
- Extracted message
-
getTranslations
public static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, org.apache.solr.common.SolrDocument doc) getTranslations.- Parameters:
fieldName- Solr field name to look up translations fordoc- Solr document containing the field values- Returns:
- an Optional containing the translations for the field, or empty if no values are found
-
getTranslations
public static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, org.apache.solr.common.SolrDocument doc, BinaryOperator<String> combiner) getTranslations.- Parameters:
fieldName- Solr field name to look up translations fordoc- Solr document containing the field valuescombiner- function to merge multiple string values into one- Returns:
- an Optional containing the combined translations for the field, or empty if no values are found
-
getTranslations
public static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, StructElement doc, BinaryOperator<String> combiner) getTranslations.- Parameters:
fieldName- Solr field name to look up translations fordoc- structure element containing the field valuescombiner- function to merge multiple string values into one- Returns:
- an Optional containing the combined translations for the field, or empty if no values are found
-
getTranslations
public static Optional<de.intranda.metadata.multilanguage.IMetadataValue> getTranslations(String fieldName, StructElement doc, List<Locale> translationLocales, BinaryOperator<String> combiner) getTranslations.- Parameters:
fieldName- Solr field name to look up translations fordoc- structure element containing the field valuestranslationLocales- list of locales to use for translation lookupcombiner- function to merge multiple string values into one- Returns:
- an Optional containing the combined translations for the field using the specified locales, or empty if no values are found
-
isHasImages
public static boolean isHasImages(org.apache.solr.common.SolrDocument doc) throws IndexUnreachableException isHasImages.- Parameters:
doc- Solr document to check; needs to contain metadata fieldsSolrConstants.FILENAMEandSolrConstants.THUMBNAIL- Returns:
- true if record described by doc has images; false otherwise
- Throws:
IndexUnreachableException- if any.
-
getProcessedConditions
- Parameters:
conditions- raw Solr condition string to process- Returns:
- conditions with NOW/YEAR replaced by current year
-
getAvailableValuesForField
public static List<String> getAvailableValuesForField(String field, String filterQuery) throws PresentationException, IndexUnreachableException getAvailableValuesForField.- Parameters:
field- Solr field name to retrieve facet values forfilterQuery- Solr filter query to restrict the search- Returns:
- List of facet values for the given field and query
- Throws:
PresentationException- if any.IndexUnreachableException- if any.
-
getAvailableValuesForField
public static List<String> getAvailableValuesForField(String field, String filterQuery, boolean useFacetField) throws PresentationException, IndexUnreachableException getAvailableValuesForField.- Parameters:
field- Solr field name to retrieve facet values forfilterQuery- Solr filter query to restrict the searchuseFacetField- If true, "FACET_" field variant is used for the actual search; Only use false for single-token values- Returns:
- List of facet values for the given field and query
- Throws:
PresentationException- if any.IndexUnreachableException- if any.
-
getExistingSubthemes
public static List<String> getExistingSubthemes() throws PresentationException, IndexUnreachableException- Returns:
- List of existing values for the configured subtheme discriminator field
- Throws:
PresentationExceptionIndexUnreachableException
-
generateRandomSortField
Solr supports dynamic random_* sorting fields. Each value represents one particular order, so a random number is required.- Returns:
- Randomized sorting field
-
checkSolrSchemaName
checkSolrSchemaName.- Returns:
- an array of
Stringobjects.
-
escapeSpecialCharacters
Escapes all special characters used by SOLR. Details here: https://solr.apache.org/guide/7_3/the-standard-query-parser.html#escaping-special-characters as well as the characters '<' and '>' by adding a '\' before them. Special characters which already are escaped by '\' are not escaped any further making this method idempotent.- Parameters:
string- the string to escape- Returns:
- the escaped string. if the original string is null, null is also returned
-
unescapeSpecialCharacters
Reverts the operation ofescapeSpecialCharacters(String).- Parameters:
string- the string to unescape- Returns:
- the unescaped string
-
cleanUpQuery
- Parameters:
query- Solr query string to clean up- Returns:
- cleaned up query
-
getBaseFieldName
- Parameters:
fieldName- Solr field name possibly containing a language suffix- Returns:
- fieldName without language suffix
-
getLanguage
- Parameters:
fieldName- Solr field name from which to extract the language code- Returns:
- language part of fieldName
-
getLocale
- Parameters:
fieldName- Solr field name from which to derive the locale- Returns:
- Locale based on the language part of fieldName
-
getTranslatedMetadata
public static Map<String,List<de.intranda.metadata.multilanguage.IMetadataValue>> getTranslatedMetadata(org.apache.solr.common.SolrDocument doc, Function<String, Boolean> fieldNameFilter) - Parameters:
doc- Solr document to extract metadata fromfieldNameFilter- predicate to select which field names to include- Returns:
- Map
-
getTranslatedMetadata
public static Map<String,List<de.intranda.metadata.multilanguage.IMetadataValue>> getTranslatedMetadata(org.apache.solr.common.SolrDocument doc, Map<String, List<de.intranda.metadata.multilanguage.IMetadataValue>> metadata, Locale documentLocale, Function<String, Boolean> fieldNameFilter) - Parameters:
doc- Solr document to extract metadata frommetadata- existing metadata map to merge results intodocumentLocale- default locale to assign to non-language-coded fieldsfieldNameFilter- predicate to select which field names to include- Returns:
- Map
-
getReferenceId
- Parameters:
doc- Solr document from which to read the reference ID- Returns:
- Value of MD_REFID in doc, if available
-