Class SolrSearchIndex

java.lang.Object
io.goobi.viewer.solr.SolrSearchIndex
All Implemented Interfaces:
Closeable, AutoCloseable

public class SolrSearchIndex extends Object implements Closeable
Provides all Solr query operations for the viewer, wrapping the SolrJ client with viewer-specific convenience methods.
  • Field Details

  • Constructor Details

    • SolrSearchIndex

      public SolrSearchIndex(org.apache.solr.client.solrj.SolrClient client)
      Creates a new SolrSearchIndex instance.
      Parameters:
      client - Solr client to use; a new default client is created if null
  • Method Details

    • checkReloadNeeded

      public void checkReloadNeeded()
      Checks whether the server's configured URL matches that in the config file. If not, a new server instance is created.
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getNewSolrClient

      public static org.apache.solr.client.solrj.SolrClient getNewSolrClient()
      Returns:
      New SolrClient
    • getNewSolrClient

      public static org.apache.solr.client.solrj.SolrClient getNewSolrClient(String solrUrl)
    • isSolrIndexOnline

      public boolean isSolrIndexOnline()
      Returns:
      true if test query executes without errors; false otherwise
    • testQuery

      public org.apache.solr.client.solrj.response.QueryResponse testQuery(String query) throws org.apache.solr.client.solrj.SolrServerException, IOException
      testQuery.
      Parameters:
      query - Solr query string to execute as a test
      Returns:
      the QueryResponse from the test query
      Throws:
      org.apache.solr.client.solrj.SolrServerException - if any.
      IOException
    • search

      public org.apache.solr.client.solrj.response.QueryResponse search(String query, int first, int rows, List<StringPair> sortFields, List<String> facetFields, String facetSort, List<String> fieldList, List<String> filterQueries, Map<String,String> params) throws PresentationException, IndexUnreachableException
      Main Solr search method.
      Parameters:
      query - Solr query string to execute
      first - zero-based offset of the first result to return
      rows - maximum number of documents to return
      sortFields - Optional field/order pairs for sorting
      facetFields - list of Solr facet field names to compute facets for
      facetSort - sort order for facet values ("count" or field name)
      fieldList - If not null, only the fields in the list will be returned.
      filterQueries - list of Solr filter query strings to apply
      params - Additional query parameters.
      Returns:
      QueryResponse
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • search

      public org.apache.solr.client.solrj.response.QueryResponse search(String query, int first, int rows, List<StringPair> sortFields, List<String> facetFields, List<String> facetQueries, String facetSort, List<String> fieldList, List<String> filterQueries, Map<String,String> params) throws PresentationException, IndexUnreachableException
      Convenience overload of the core search method using the default query method. The 10-parameter arity keeps it unambiguous with the facetSort 9-parameter overload even when null arguments are passed.
      Parameters:
      query - Solr query string to execute
      first - zero-based offset of the first result to return
      rows - maximum number of documents to return
      sortFields - Optional field/order pairs for sorting
      facetFields - list of Solr facet field names to compute facets for
      facetQueries - list of arbitrary Solr queries to compute facet.query counts for
      facetSort - sort order for facet values ("count" or field name)
      fieldList - If not null, only the fields in the list will be returned.
      filterQueries - list of Solr filter query strings to apply
      params - additional Solr query parameters
      Returns:
      QueryResponse
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • search

      public org.apache.solr.client.solrj.response.QueryResponse search(String query, int first, int rows, List<StringPair> sortFields, List<String> facetFields, List<String> facetQueries, String facetSort, List<String> fieldList, List<String> filterQueries, Map<String,String> params, org.apache.solr.client.solrj.SolrRequest.METHOD queryMethod) throws PresentationException, IndexUnreachableException
      Throws:
      PresentationException
      IndexUnreachableException
    • search

      public org.apache.solr.client.solrj.response.QueryResponse search(String query, int first, int rows, List<StringPair> sortFields, List<String> facetFields, List<String> fieldList, List<String> filterQueries, Map<String,String> params) throws PresentationException, IndexUnreachableException
      search.
      Parameters:
      query - Solr query string to execute
      first - zero-based offset of the first result to return
      rows - maximum number of documents to return
      sortFields - Optional field/order pairs for sorting
      facetFields - list of Solr facet field names to compute facets for
      fieldList - If not null, only the fields in the list will be returned.
      filterQueries - list of Solr filter query strings to apply
      params - additional Solr query parameters
      Returns:
      QueryResponse
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • search

      public org.apache.solr.client.solrj.response.QueryResponse search(String query, int first, int rows, List<StringPair> sortFields, List<String> facetFields, List<String> fieldList) throws PresentationException, IndexUnreachableException
      search.
      Parameters:
      query - Solr query string to execute
      first - zero-based offset of the first result to return
      rows - maximum number of documents to return
      sortFields - Optional field/order pairs for sorting
      facetFields - list of Solr facet field names to compute facets for
      fieldList - If not null, only the fields in the list will be returned.
      Returns:
      QueryResponse
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • search

      public org.apache.solr.common.SolrDocumentList search(String query, int rows, List<StringPair> sortFields, List<String> fieldList) throws PresentationException, IndexUnreachableException
      search.
      Parameters:
      query - Solr query string to execute
      rows - maximum number of documents to return
      sortFields - Optional field/order pairs for sorting
      fieldList - If not null, only the fields in the list will be returned.
      Returns:
      the list of matching Solr documents
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • search

      public org.apache.solr.common.SolrDocumentList search(String query, List<String> fieldList) throws PresentationException, IndexUnreachableException
      Parameters:
      query - Solr query string to execute
      fieldList - list of Solr field names to include in returned documents
      Returns:
      SolrDocumentList
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • search

      public org.apache.solr.common.SolrDocumentList search(String query) throws PresentationException, IndexUnreachableException
      Diese Methode führt eine Suche im Lucene durch.
      Parameters:
      query - Solr query string to execute
      Returns:
      SolrDocumentList
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getFirstDoc

      public org.apache.solr.common.SolrDocument getFirstDoc(String query, List<String> fieldList) throws PresentationException, IndexUnreachableException
      Retrieves the first document found by the given query.
      Parameters:
      query - Solr query string to execute
      fieldList - list of Solr field names to include in returned document
      Returns:
      The first hit returned by the query
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getFirstDoc

      public org.apache.solr.common.SolrDocument getFirstDoc(String query, List<String> fieldList, List<StringPair> sortFields) throws PresentationException, IndexUnreachableException
      Retrieves the first document found by the given query.
      Parameters:
      query - The query to search
      fieldList - The fields retrieved
      sortFields - Sorting - the first volume according to this sorting is returned
      Returns:
      The first hit returned by the query
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getDocs

      public org.apache.solr.common.SolrDocumentList getDocs(String query, List<String> fieldList) throws PresentationException, IndexUnreachableException
      Returns all SolrDocuments matching the given query. If no documents were found, null is returned
      Parameters:
      query - Solr query string to execute
      fieldList - list of Solr field names to include in returned documents
      Returns:
      the list of matching Solr documents, or null if no documents are found
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getDocumentByIddoc

      public org.apache.solr.common.SolrDocument getDocumentByIddoc(String iddoc) throws IndexUnreachableException, PresentationException
      getDocumentByIddoc.
      Parameters:
      iddoc - internal Solr document identifier to look up
      Returns:
      the Solr document matching the given IDDOC, or null if not found
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
    • getDocumentByPI

      public org.apache.solr.common.SolrDocument getDocumentByPI(String pi) throws IndexUnreachableException, PresentationException
      getDocumentByPI.
      Parameters:
      pi - persistent identifier of the record to look up
      Returns:
      the Solr document matching the given PI, or null if not found
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
    • getDocumentByPIAndLogId

      public org.apache.solr.common.SolrDocument getDocumentByPIAndLogId(String pi, String divId) throws IndexUnreachableException, PresentationException
      Throws:
      IndexUnreachableException
      PresentationException
    • generateFilteredTagCloud

      public List<Tag> generateFilteredTagCloud(String fieldName, String querySuffix) throws IndexUnreachableException
      Returns a list of Tags created from the terms for the given field name. This method uses the slower doc search instead of term search, but can be filtered with a query.
      Parameters:
      fieldName - Solr field name whose terms to aggregate into tags
      querySuffix - additional query suffix appended to filter the tag cloud results
      Returns:
      a list of Tag objects generated from the terms of the given Solr field, optionally filtered by the query suffix
      Throws:
      IndexUnreachableException - if any.
    • getIddocFromIdentifier

      public String getIddocFromIdentifier(String identifier) throws PresentationException, IndexUnreachableException
      Returns the value of the IDDOC field for the document with the given PI (or 0 if none found).
      Parameters:
      identifier - persistent identifier of the record to look up
      Returns:
      String value; null if none found
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getIdentifierFromIddoc

      public String getIdentifierFromIddoc(String iddoc) throws PresentationException, IndexUnreachableException
      getIdentifierFromIddoc.
      Parameters:
      iddoc - internal Solr document identifier to resolve
      Returns:
      the record identifier (PI) for the given IDDOC, or null if not found
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getImageOwnerIddoc

      public String getImageOwnerIddoc(String pi, int pageNo) throws IndexUnreachableException, PresentationException
      Returns the IDDOC of the logical document to which the given page belongs.
      Parameters:
      pi - persistent identifier of the top-level record
      pageNo - page order number (1-based) to look up
      Returns:
      String value; null if none found
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
    • getIddocByLogid

      public String getIddocByLogid(String pi, String logId) throws IndexUnreachableException, PresentationException
      Returns the IDDOC of the logical document to which the given LOGID belongs.
      Parameters:
      pi - persistent identifier of the top-level record
      logId - logical structure identifier within the record
      Returns:
      String value; null if none found
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
    • getHitCount

      public long getHitCount(String query) throws IndexUnreachableException, PresentationException
      Returns the number of hits for the given query without actually returning any documents.
      Parameters:
      query - Solr query string to count hits for
      Returns:
      a long.
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
    • getHitCount

      public long getHitCount(String query, List<String> filterQueries) throws IndexUnreachableException, PresentationException
      Returns the number of hits for the given query and filter queries without actually returning any documents.
      Parameters:
      query - Solr query string to count hits for
      filterQueries - Solr filter query list to apply alongside the main query
      Returns:
      Number of hits for the given queries
      Throws:
      IndexUnreachableException
      PresentationException
    • findDataRepositoryName

      public String findDataRepositoryName(String pi) throws PresentationException, IndexUnreachableException
      Retrieves the repository name for the record with the given PI and persists it in a map. This method is package private to discourage clients from constructing data file paths manually instead of using Helper methods.
      Parameters:
      pi - persistent identifier of the record to look up
      Returns:
      Data repository name for the record with the given identifier; null if not in a repository
      Throws:
      PresentationException
      IndexUnreachableException
    • updateDataRepositoryNames

      public void updateDataRepositoryNames(String pi, String dataRepositoryName)
      Parameters:
      pi - persistent identifier of the record
      dataRepositoryName - name of the data repository to associate with the record
    • searchFacetsAndStatistics

      public org.apache.solr.client.solrj.response.QueryResponse searchFacetsAndStatistics(String query, List<String> filterQueries, List<String> facetFields, int facetMinCount, boolean getFieldStatistics) throws PresentationException, IndexUnreachableException
      Returns facets for the given facet field list. No actual docs are returned since they aren't necessary.
      Parameters:
      query - Solr query string to execute
      filterQueries - list of Solr filter query strings to apply
      facetFields - list of Solr facet field names to compute facets for
      facetMinCount - minimum count a facet value must have to be included
      getFieldStatistics - if true, field statistics are generated for every facet field
      Returns:
      the QueryResponse containing facets (and optional field statistics) without document results
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • searchFacetsAndStatistics

      public org.apache.solr.client.solrj.response.QueryResponse searchFacetsAndStatistics(String query, List<String> filterQueries, List<String> facetFields, int facetMinCount, Map<String,String> params, boolean getFieldStatistics) throws PresentationException, IndexUnreachableException
      Returns facets for the given facet field list. No actual docs are returned since they aren't necessary.
      Parameters:
      query - Solr query string to execute
      filterQueries - list of Solr filter query strings to apply
      facetFields - list of Solr facet field names to compute facets for
      facetMinCount - minimum count a facet value must have to be included
      params - additional Solr query parameters
      getFieldStatistics - if true, field statistics are generated for every facet field
      Returns:
      the QueryResponse containing facets (and optional field statistics) without document results
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • searchFacetsAndStatistics

      public org.apache.solr.client.solrj.response.QueryResponse searchFacetsAndStatistics(String query, List<String> filterQueries, List<String> facetFields, int facetMinCount, String facetPrefix, Map<String,String> params, boolean getFieldStatistics) throws PresentationException, IndexUnreachableException
      Returns facets for the given facet field list. No actual docs are returned since they aren't necessary.
      Parameters:
      query - Solr query string to execute
      filterQueries - list of Solr filter query strings to apply
      facetFields - list of Solr facet field names to compute facets for
      facetMinCount - minimum count a facet value must have to be included
      facetPrefix - prefix that facet values must start with; ignored if null or blank
      params - additional Solr query parameters
      getFieldStatistics - if true, field statistics are generated for every facet field
      Returns:
      the QueryResponse containing facets (and optional field statistics) without document results
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getAllFieldNames

      public List<String> getAllFieldNames() throws IndexUnreachableException
      getAllFieldNames.
      Returns:
      a list of all field names defined in the Solr schema
      Throws:
      DAOException
      org.apache.solr.client.solrj.SolrServerException - if any.
      IOException - if any.
      IndexUnreachableException
    • getAllBooleanFieldNames

      public List<String> getAllBooleanFieldNames() throws IndexUnreachableException
      Throws:
      IndexUnreachableException
    • loadSolrFields

      public void loadSolrFields() throws org.apache.solr.client.solrj.SolrServerException, IOException
      Throws:
      org.apache.solr.client.solrj.SolrServerException
      IOException
    • getAllSortFieldNames

      public List<String> getAllSortFieldNames() throws org.apache.solr.client.solrj.SolrServerException, IOException
      getAllSortFieldNames.
      Returns:
      a list of all SOLR fields starting with "SORT_".
      Throws:
      org.apache.solr.client.solrj.SolrServerException - if any.
      IOException - if any.
    • getAllGroupFieldNames

      public List<String> getAllGroupFieldNames() throws org.apache.solr.client.solrj.SolrServerException, IOException
      Returns:
      A list of all SOLR fields without the multivalues flag
      Throws:
      org.apache.solr.client.solrj.SolrServerException
      IOException
    • getDisplayUserGeneratedContentsForRecord

      public List<DisplayUserGeneratedContent> getDisplayUserGeneratedContentsForRecord(String pi) throws PresentationException, IndexUnreachableException
      getDisplayUserGeneratedContentsForPage.
      Parameters:
      pi - persistent identifier of the record to load UGC for
      Returns:
      contents for the given page
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getFilename

      public Optional<String> getFilename(String pi, int order) throws PresentationException, IndexUnreachableException
      Catches the filename of the page with the given order under the given ip.
      Parameters:
      pi - The topstruct pi
      order - The page order (1-based
      Returns:
      An optíonal containing the filename of the page with the given order under the given ip. Or an empty optional if no matching page was found.
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getFilename

      public Optional<String> getFilename(String pi, String basename) throws PresentationException, IndexUnreachableException
      Catches the filename of the page with the given basename under the given ip. Used in case a filename is requested without the file extension
      Parameters:
      pi - The topstruct pi
      basename - The filename of the image without the extension (everything before the last dot)
      Returns:
      An optíonal containing the filename of the page with the given order under the given ip. Or an empty optional if no matching page was found.
      Throws:
      PresentationException - if any.
      IndexUnreachableException - if any.
    • getLabelValuesForFacetField

      public Map<String,String> getLabelValuesForFacetField(String field, String labelField, Set<String> values) throws PresentationException, IndexUnreachableException
      Parameters:
      field - Solr field name whose values are to be looked up
      labelField - Solr field name that contains the human-readable label
      values - set of field values for which labels are requested
      Returns:
      Map
      Throws:
      PresentationException
      IndexUnreachableException
    • getSolrServerUrl

      public String getSolrServerUrl()
      Returns:
      Base URL of the active Solr server
    • pingSolrIndex

      public boolean pingSolrIndex()
      Returns:
      true if ping successful; false otherwise
    • checkSolrQueryResponse

      public boolean checkSolrQueryResponse()
      Checks whether the index answers an actual search query, rather than just the ping handler.

      The query matches every indexed record and is therefore valid for any index; an empty index returns zero hits and still counts as a successful check, because only the ability to answer a query is being verified.

      Returns:
      true if the query was answered by the index; false otherwise
    • getHeatMap

      public String getHeatMap(String solrField, String wktRegion, String query, String filterQuery, Integer gridLevel) throws IndexUnreachableException
      Parameters:
      solrField - Solr field name containing the geospatial coordinates
      wktRegion - WKT string defining the spatial region to compute the heatmap for
      query - Solr query string to filter documents
      filterQuery - additional Solr filter query string
      gridLevel - heatmap grid level controlling resolution
      Returns:
      String
      Throws:
      IndexUnreachableException
    • getPage

      getPage.
      Parameters:
      pi - persistent identifier of the record to load the page from
      order - page order number (1-based) to retrieve
      Returns:
      the PhysicalElement at the given order position within the record, or null if not found
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
      DAOException - if any.
    • getPage

      getPage.
      Parameters:
      struct - struct element representing the record to load the page from
      order - page order number (1-based) to retrieve
      Returns:
      the PhysicalElement at the given order position within the struct element
      Throws:
      IndexUnreachableException - if any.
      PresentationException - if any.
      DAOException - if any.
    • getDataRepositoryNames

      public Map<String,String> getDataRepositoryNames()