Class SolrTools

java.lang.Object
io.goobi.viewer.solr.SolrTools

public final class SolrTools extends Object
Utility class providing static helpers for constructing Solr queries, parsing responses, and field value extraction.
  • Method Details

    • getSolrSortFieldsAsList

      public static List<StringPair> getSolrSortFieldsAsList(String solrSortFields, String splitFieldsBy, String splitNameOrderBy)
      Returns the comma-separated sorting fields in solrSortFields as a List.
      Parameters:
      solrSortFields - comma-separated Solr sort field string to parse
      splitFieldsBy - String by which the individual field configurations are split
      splitNameOrderBy - 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

      public static String getAsString(Object fieldValue)
      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

      public static Boolean getAsBoolean(Object fieldValue)
      Parameters:
      fieldValue - Solr field value to convert
      Returns:
      Boolean
    • getAsString

      public static String getAsString(Object fieldValue, String separator)
      Parameters:
      fieldValue - Solr field value to convert
      separator - string used to join multiple values
      Returns:
      String
    • getAsInt

      public static Integer getAsInt(Object fieldValue)
      getAsInt.
      Parameters:
      fieldValue - Solr field value to convert
      Returns:
      the integer value, or null if the input is null or cannot be parsed
    • getAsLong

      public static Long getAsLong(Object fieldValue)
      getAsLong.
      Parameters:
      fieldValue - Solr field value to convert
      Returns:
      the long value, or null if the input is null or cannot be parsed
    • getSingleFieldValue

      public static Object getSingleFieldValue(org.apache.solr.common.SolrDocument doc, String field)
      getSingleFieldValue.
      Parameters:
      doc - Solr document to read from
      field - 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 from
      field - 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 from
      field - Solr field name to retrieve
      Returns:
      the integer value of the field, or null if not present or not parseable
    • getSingleFieldLongValue

      public static Long getSingleFieldLongValue(org.apache.solr.common.SolrDocument doc, String field)
      getSingleFieldLongValue.
      Parameters:
      doc - Solr document to read from
      field - 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 from
      field - 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 from
      fieldName - Solr field name to retrieve values for
      Returns:
      a list of all string values for the given field in the given Solr document
    • getFieldValueMap

      public static Map<String,List<String>> getFieldValueMap(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 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 metadata
      key - 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

      public static Map<String,List<String>> getMetadataValuesForLanguage(StructElement doc, String key)
      getMetadataValuesForLanguage.
      Parameters:
      doc - The document containing the metadata
      key - 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

      public static boolean isGroup(MetadataContainer doc)
      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

      public static boolean isAnchor(MetadataContainer doc)
      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

      public static boolean isWork(MetadataContainer doc)
      Parameters:
      doc - metadata container to inspect
      Returns:
      true if doc contains ISWORK:true; false otherwise
    • isLanguageCodedField

      public static boolean isLanguageCodedField(String fieldName)
      Parameters:
      fieldName - Solr field name to check for language encoding
      Returns:
      true if fieldName contains _LANG_; false otherwise
    • isHasWrongLanguageCode

      public static boolean isHasWrongLanguageCode(String field, String language)
      Parameters:
      field - Solr field name potentially containing a language suffix
      language - two- or three-letter language code to compare against
      Returns:
      true if language code different
    • isQuerySyntaxError

      public static boolean isQuerySyntaxError(Exception e)
      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

      public static String extractExceptionMessageHtmlTitle(String exceptionMessage)
      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 for
      doc - 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 for
      doc - Solr document containing the field values
      combiner - 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 for
      doc - structure element containing the field values
      combiner - 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 for
      doc - structure element containing the field values
      translationLocales - list of locales to use for translation lookup
      combiner - 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 fields SolrConstants.FILENAME and SolrConstants.THUMBNAIL
      Returns:
      true if record described by doc has images; false otherwise
      Throws:
      IndexUnreachableException - if any.
    • getProcessedConditions

      public static String getProcessedConditions(String conditions)
      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 for
      filterQuery - 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 for
      filterQuery - Solr filter query to restrict the search
      useFacetField - 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:
      PresentationException
      IndexUnreachableException
    • generateRandomSortField

      public static String generateRandomSortField()
      Solr supports dynamic random_* sorting fields. Each value represents one particular order, so a random number is required.
      Returns:
      Randomized sorting field
    • checkSolrSchemaName

      public static String[] checkSolrSchemaName()
      checkSolrSchemaName.
      Returns:
      an array of String objects.
    • escapeSpecialCharacters

      public static String escapeSpecialCharacters(String string)
      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

      public static String unescapeSpecialCharacters(String string)
      Reverts the operation of escapeSpecialCharacters(String).
      Parameters:
      string - the string to unescape
      Returns:
      the unescaped string
    • cleanUpQuery

      public static String cleanUpQuery(String query)
      Parameters:
      query - Solr query string to clean up
      Returns:
      cleaned up query
    • getBaseFieldName

      public static String getBaseFieldName(String fieldName)
      Parameters:
      fieldName - Solr field name possibly containing a language suffix
      Returns:
      fieldName without language suffix
    • getLanguage

      public static String getLanguage(String fieldName)
      Parameters:
      fieldName - Solr field name from which to extract the language code
      Returns:
      language part of fieldName
    • getLocale

      public static Locale getLocale(String fieldName)
      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 from
      fieldNameFilter - 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 from
      metadata - existing metadata map to merge results into
      documentLocale - default locale to assign to non-language-coded fields
      fieldNameFilter - predicate to select which field names to include
      Returns:
      Map
    • getReferenceId

      public static final String getReferenceId(org.apache.solr.common.SolrDocument doc)
      Parameters:
      doc - Solr document from which to read the reference ID
      Returns:
      Value of MD_REFID in doc, if available