Package org.dspace.discovery
Class SolrServiceImpl
- java.lang.Object
-
- org.dspace.discovery.SolrServiceImpl
-
- All Implemented Interfaces:
IndexingService,SearchService
@Service public class SolrServiceImpl extends Object implements SearchService, IndexingService
SolrIndexer contains the methods that index Items and their metadata, collections, communities, etc. It is meant to either be invoked from the command line (see dspace/bin/index-all) or via the indexContent() methods within DSpace.The Administrator can choose to run SolrIndexer in a cron that repeats regularly, a failed attempt to index from the UI will be "caught" up on in that cron. The SolrServiceImpl is registered as a Service in the ServiceManager via a Spring configuration file located under classpath://spring/spring-dspace-applicationContext.xml Its configuration is Autowired by the ApplicationContext
- Author:
- Kevin Van de Velde (kevin at atmire dot com), Mark Diggory (markd at atmire dot com), Ben Bosman (ben at atmire dot com)
-
-
Field Summary
Fields Modifier and Type Field Description protected ContentServiceFactorycontentServiceFactorystatic StringDISCOVER_WORKFLOW_CONFIGURATION_NAMEThe name of the discover configuration used to search for workflow tasks in the mydspacestatic StringDISCOVER_WORKSPACE_CONFIGURATION_NAMEThe name of the discover configuration used to search for inprogress submission in the mydspaceprotected GroupServicegroupServiceprotected IndexObjectFactoryFactoryindexObjectServiceFactoryprotected SolrSearchCoresolrSearchCore
-
Constructor Summary
Constructors Modifier Constructor Description protectedSolrServiceImpl()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidbuildSpellCheck()StringcalculateExtremeValue(Context context, String valueField, String sortField, DiscoverQuery.SORT_ORDER sortOrder)This method returns us either the highest or lowest value for the field that we give to it depending on what sortOrder we give this method.voidcleanIndex(boolean force)Iterates over all documents in the Lucene index and verifies they are in database, if not, they are removed.voidcommit()voidcreateIndex(Context c)create full index - wiping old indexStringcreateLocationQueryForAdministrableItems(Context context)Method to create a Query that includes all communities and collections a user may administrate.protected voidemailException(Exception exception)StringescapeQueryChars(String query)Utility method to escape any special characters in a user's queryprotected IndexableObjectfindIndexableObject(Context context, org.apache.solr.common.SolrDocument doc)Find the indexable object by type and UUIDFacetYearRangegetFacetYearRange(Context context, IndexableObject scope, DiscoverySearchFilterFacet facet, List<String> filterQueries, DiscoverQuery parentQuery)List<Item>getRelatedItems(Context context, Item item, DiscoveryMoreLikeThisConfiguration mltConfig)voidindexContent(Context context, IndexableObject dso)If the handle for the "dso" already exists in the index, and the "dso" has a lastModified timestamp that is newer than the document in the index then it is updated, otherwise a new document is added.voidindexContent(Context context, IndexableObject indexableObject, boolean force)If the handle for the "dso" already exists in the index, and the "dso" has a lastModified timestamp that is newer than the document in the index then it is updated, otherwise a new document is added.voidindexContent(Context context, IndexableObject dso, boolean force, boolean commit)StringlocationToName(Context context, String field, String value)voidoptimize()Maintenance to keep a SOLR index efficient.voidreIndexContent(Context context, IndexableObject dso)reIndexContent removes something from the index, then re-indexes itprotected booleanrequiresIndexing(String uniqueId, Date lastModified)Is stale checks the lastModified time stamp in the database and the index to determine if the index is stale.protected org.apache.solr.client.solrj.SolrQueryresolveToSolrQuery(Context context, DiscoverQuery discoveryQuery)protected DiscoverResultretrieveResult(Context context, DiscoverQuery query, org.apache.solr.client.solrj.response.QueryResponse solrQueryResponse)List<IndexableObject>search(Context context, String query, int offset, int max, String... filterquery)List<IndexableObject>search(Context context, String query, String orderfield, boolean ascending, int offset, int max, String... filterquery)DiscoverResultsearch(Context context, DiscoverQuery discoveryQuery)Convenient method to call @see #search(Context, DSpaceObject, DiscoverQuery) with a null DSpace Object as scope (i.e.DiscoverResultsearch(Context context, IndexableObject dso, DiscoverQuery discoveryQuery)Convenient method to call @see #search(Context, DSpaceObject, DiscoverQuery, boolean) with includeWithdrawn=falseDatetoDate(String t)Helper function to retrieve a date using a best guess of the potential date encodings on a fieldDiscoverFilterQuerytoFilterQuery(Context context, String field, String operator, String value)Transforms the given string field and value into a filter queryStringtoSortFieldIndex(String metadataField, String type)Transforms the metadata field of the given sort configuration into the indexed field which we can then use in our Solr queries.protected StringtransformAuthorityValue(Context context, String field, String value)protected StringtransformDisplayedValue(Context context, String field, String value)protected StringtransformFacetField(DiscoverFacetField facetFieldConfig, String field, boolean removePostfix)protected StringtransformSortValue(Context context, String field, String value)voidunIndexContent(Context context, String searchUniqueID)Unindex a Document in the Lucene index.voidunIndexContent(Context context, String searchUniqueID, boolean commit)Unindex a Document in the Lucene Index.voidunIndexContent(Context context, IndexableObject dso)unIndex removes an Item, Collection, or CommunityvoidunIndexContent(Context context, IndexableObject indexableObject, boolean commit)unIndex removes an Item, Collection, or Communityprotected voidupdate(Context context, IndexFactory indexableObjectService, IndexableObject indexableObject)voidupdateIndex(Context context)Iterates over all Items, Collections and Communities.voidupdateIndex(Context context, boolean force)Iterates over all Items, Collections and Communities.voidupdateIndex(Context context, boolean force, String type)
-
-
-
Field Detail
-
DISCOVER_WORKFLOW_CONFIGURATION_NAME
public static final String DISCOVER_WORKFLOW_CONFIGURATION_NAME
The name of the discover configuration used to search for workflow tasks in the mydspace- See Also:
- Constant Field Values
-
DISCOVER_WORKSPACE_CONFIGURATION_NAME
public static final String DISCOVER_WORKSPACE_CONFIGURATION_NAME
The name of the discover configuration used to search for inprogress submission in the mydspace- See Also:
- Constant Field Values
-
contentServiceFactory
@Autowired protected ContentServiceFactory contentServiceFactory
-
groupService
@Autowired protected GroupService groupService
-
indexObjectServiceFactory
@Autowired protected IndexObjectFactoryFactory indexObjectServiceFactory
-
solrSearchCore
@Autowired protected SolrSearchCore solrSearchCore
-
-
Method Detail
-
indexContent
public void indexContent(Context context, IndexableObject dso) throws SQLException
If the handle for the "dso" already exists in the index, and the "dso" has a lastModified timestamp that is newer than the document in the index then it is updated, otherwise a new document is added.- Specified by:
indexContentin interfaceIndexingService- Parameters:
context- Users Contextdso- DSpace Object (Item, Collection or Community- Throws:
SQLException- if error
-
indexContent
public void indexContent(Context context, IndexableObject indexableObject, boolean force)
If the handle for the "dso" already exists in the index, and the "dso" has a lastModified timestamp that is newer than the document in the index then it is updated, otherwise a new document is added.- Specified by:
indexContentin interfaceIndexingService- Parameters:
context- Users ContextindexableObject- The object we want to indexforce- Force update even if not stale.- Throws:
SQLException- if error
-
update
protected void update(Context context, IndexFactory indexableObjectService, IndexableObject indexableObject) throws IOException, SQLException, org.apache.solr.client.solrj.SolrServerException
- Throws:
IOExceptionSQLExceptionorg.apache.solr.client.solrj.SolrServerException
-
unIndexContent
public void unIndexContent(Context context, IndexableObject dso) throws SQLException, IOException
unIndex removes an Item, Collection, or Community- Specified by:
unIndexContentin interfaceIndexingService- Parameters:
context- The relevant DSpace Context.dso- DSpace Object, can be Community, Item, or Collection- Throws:
SQLException- if database errorIOException- if IO error
-
unIndexContent
public void unIndexContent(Context context, IndexableObject indexableObject, boolean commit) throws SQLException, IOException
unIndex removes an Item, Collection, or Community- Specified by:
unIndexContentin interfaceIndexingService- Parameters:
context- The relevant DSpace Context.indexableObject- The object to be indexedcommit- iftrueforce an immediate commit on SOLR- Throws:
SQLException- if database errorIOException- if IO error
-
unIndexContent
public void unIndexContent(Context context, String searchUniqueID) throws IOException
Unindex a Document in the Lucene index.- Specified by:
unIndexContentin interfaceIndexingService- Parameters:
context- the dspace contextsearchUniqueID- the search uniqueID of the document to be deleted- Throws:
IOException- if IO error
-
unIndexContent
public void unIndexContent(Context context, String searchUniqueID, boolean commit) throws IOException
Unindex a Document in the Lucene Index.- Specified by:
unIndexContentin interfaceIndexingService- Parameters:
context- the dspace contextsearchUniqueID- the search uniqueID of the document to be deleted- Throws:
IOException- if IO error
-
reIndexContent
public void reIndexContent(Context context, IndexableObject dso) throws SQLException, IOException
reIndexContent removes something from the index, then re-indexes it- Specified by:
reIndexContentin interfaceIndexingService- Parameters:
context- context objectdso- object to re-index- Throws:
SQLExceptionIOException
-
createIndex
public void createIndex(Context c) throws SQLException, IOException
create full index - wiping old index- Specified by:
createIndexin interfaceIndexingService- Parameters:
c- context to use- Throws:
SQLExceptionIOException
-
updateIndex
public void updateIndex(Context context)
Iterates over all Items, Collections and Communities. And updates them in the index. Uses decaching to control memory footprint. Uses indexContent and isStale to check state of item in index.- Specified by:
updateIndexin interfaceIndexingService- Parameters:
context- the dspace context
-
updateIndex
public void updateIndex(Context context, boolean force)
Iterates over all Items, Collections and Communities. And updates them in the index. Uses decaching to control memory footprint. Uses indexContent and isStale to check state of item in index.At first it may appear counterintuitive to have an IndexWriter/Reader opened and closed on each DSO. But this allows the UI processes to step in and attain a lock and write to the index even if other processes/jvms are running a reindex.
- Specified by:
updateIndexin interfaceIndexingService- Parameters:
context- the dspace contextforce- whether or not to force the reindexing
-
updateIndex
public void updateIndex(Context context, boolean force, String type)
- Specified by:
updateIndexin interfaceIndexingService
-
cleanIndex
public void cleanIndex(boolean force) throws IOException, SQLException, SearchServiceExceptionIterates over all documents in the Lucene index and verifies they are in database, if not, they are removed.- Specified by:
cleanIndexin interfaceIndexingService- Parameters:
force- whether or not to force a clean index- Throws:
IOException- IO exceptionSQLException- sql exceptionSearchServiceException- occurs when something went wrong with querying the solr server
-
optimize
public void optimize()
Maintenance to keep a SOLR index efficient. Note: This might take a long time.- Specified by:
optimizein interfaceIndexingService
-
buildSpellCheck
public void buildSpellCheck() throws SearchServiceException, IOException- Specified by:
buildSpellCheckin interfaceIndexingService- Throws:
SearchServiceExceptionIOException
-
emailException
protected void emailException(Exception exception)
-
requiresIndexing
protected boolean requiresIndexing(String uniqueId, Date lastModified) throws SQLException, IOException, SearchServiceException
Is stale checks the lastModified time stamp in the database and the index to determine if the index is stale.- Parameters:
uniqueId- the unique identifier of the object that we want to indexlastModified- the last modified date of the DSpace object- Returns:
- a boolean indicating if the dso should be re indexed again
- Throws:
SQLException- sql exceptionIOException- io exceptionSearchServiceException- if something went wrong with querying the solr server
-
createLocationQueryForAdministrableItems
public String createLocationQueryForAdministrableItems(Context context) throws SQLException
Description copied from interface:SearchServiceMethod to create a Query that includes all communities and collections a user may administrate. If a user has the appropriate rights to administrate communities and/or collections we want to look up all contents of those communities and/or collections, ignoring the read policies of the items (e.g. to list all private items of communities/collections the user administrates). This method returns a query to filter for items that belong to those communities/collections only.- Specified by:
createLocationQueryForAdministrableItemsin interfaceSearchService- Parameters:
context- The relevant DSpace Context.- Returns:
- query string specific to the user's rights
- Throws:
SQLException- An exception that provides information on a database access error or other errors.
-
toDate
public Date toDate(String t)
Helper function to retrieve a date using a best guess of the potential date encodings on a field- Parameters:
t- the string to be transformed to a date- Returns:
- a date if the formatting was successful, null if not able to transform to a date
-
locationToName
public String locationToName(Context context, String field, String value) throws SQLException
- Throws:
SQLException
-
search
public DiscoverResult search(Context context, IndexableObject dso, DiscoverQuery discoveryQuery) throws SearchServiceException
Description copied from interface:SearchServiceConvenient method to call @see #search(Context, DSpaceObject, DiscoverQuery, boolean) with includeWithdrawn=false- Specified by:
searchin interfaceSearchService- Parameters:
context- DSpace Context objectdso- a DSpace Object to use as scope of the search (only results within this object)discoveryQuery- the discovery query object- Returns:
- discovery search result object
- Throws:
SearchServiceException- if search error
-
search
public DiscoverResult search(Context context, DiscoverQuery discoveryQuery) throws SearchServiceException
Description copied from interface:SearchServiceConvenient method to call @see #search(Context, DSpaceObject, DiscoverQuery) with a null DSpace Object as scope (i.e. all the repository)- Specified by:
searchin interfaceSearchService- Parameters:
context- DSpace Context object.discoveryQuery- the discovery query object.- Returns:
- discovery search result object
- Throws:
SearchServiceException- if search error
-
resolveToSolrQuery
protected org.apache.solr.client.solrj.SolrQuery resolveToSolrQuery(Context context, DiscoverQuery discoveryQuery) throws SearchServiceException
- Throws:
SearchServiceException
-
retrieveResult
protected DiscoverResult retrieveResult(Context context, DiscoverQuery query, org.apache.solr.client.solrj.response.QueryResponse solrQueryResponse) throws SQLException
- Throws:
SQLException
-
findIndexableObject
protected IndexableObject findIndexableObject(Context context, org.apache.solr.common.SolrDocument doc) throws SQLException
Find the indexable object by type and UUID- Parameters:
context- The relevant DSpace Context.doc- the solr document, the following fields MUST be present RESOURCE_TYPE_FIELD, RESOURCE_ID_FIELD and HANDLE_FIELD- Returns:
- an IndexableObject
- Throws:
SQLException- An exception that provides information on a database access error or other errors.
-
search
public List<IndexableObject> search(Context context, String query, int offset, int max, String... filterquery)
-
search
public List<IndexableObject> search(Context context, String query, String orderfield, boolean ascending, int offset, int max, String... filterquery)
- Specified by:
searchin interfaceSearchService
-
toFilterQuery
public DiscoverFilterQuery toFilterQuery(Context context, String field, String operator, String value) throws SQLException
Description copied from interface:SearchServiceTransforms the given string field and value into a filter query- Specified by:
toFilterQueryin interfaceSearchService- Parameters:
context- The relevant DSpace Context.field- the field of the filter queryoperator- equals/notequals/notcontains/authority/notauthorityvalue- the filter query value- Returns:
- a filter query
- Throws:
SQLException- if database error An exception that provides information on a database access error or other errors.
-
getRelatedItems
public List<Item> getRelatedItems(Context context, Item item, DiscoveryMoreLikeThisConfiguration mltConfig)
- Specified by:
getRelatedItemsin interfaceSearchService
-
toSortFieldIndex
public String toSortFieldIndex(String metadataField, String type)
Description copied from interface:SearchServiceTransforms the metadata field of the given sort configuration into the indexed field which we can then use in our Solr queries.- Specified by:
toSortFieldIndexin interfaceSearchService- Parameters:
metadataField- the metadata fieldtype- seeDiscoveryConfigurationParameters- Returns:
- the indexed field
-
transformFacetField
protected String transformFacetField(DiscoverFacetField facetFieldConfig, String field, boolean removePostfix)
-
transformDisplayedValue
protected String transformDisplayedValue(Context context, String field, String value) throws SQLException
- Throws:
SQLException
-
transformAuthorityValue
protected String transformAuthorityValue(Context context, String field, String value) throws SQLException
- Throws:
SQLException
-
transformSortValue
protected String transformSortValue(Context context, String field, String value) throws SQLException
- Throws:
SQLException
-
indexContent
public void indexContent(Context context, IndexableObject dso, boolean force, boolean commit) throws SearchServiceException, SQLException
- Specified by:
indexContentin interfaceIndexingService- Throws:
SearchServiceExceptionSQLException
-
commit
public void commit() throws SearchServiceException- Specified by:
commitin interfaceIndexingService- Throws:
SearchServiceException
-
escapeQueryChars
public String escapeQueryChars(String query)
Description copied from interface:SearchServiceUtility method to escape any special characters in a user's query- Specified by:
escapeQueryCharsin interfaceSearchService- Parameters:
query- User's query to escape.- Returns:
- query with any special characters escaped
-
getFacetYearRange
public FacetYearRange getFacetYearRange(Context context, IndexableObject scope, DiscoverySearchFilterFacet facet, List<String> filterQueries, DiscoverQuery parentQuery) throws SearchServiceException
- Specified by:
getFacetYearRangein interfaceSearchService- Throws:
SearchServiceException
-
calculateExtremeValue
public String calculateExtremeValue(Context context, String valueField, String sortField, DiscoverQuery.SORT_ORDER sortOrder) throws SearchServiceException
Description copied from interface:SearchServiceThis method returns us either the highest or lowest value for the field that we give to it depending on what sortOrder we give this method.- Specified by:
calculateExtremeValuein interfaceSearchService- Parameters:
context- The relevant DSpace contextvalueField- The field in solr for which we'll calculate the extreme valuesortField- The field in solr for which we'll sort the calculated extreme value on This is typically the valueField appended with "_sort"sortOrder- Entering ascending will return the minimum value Entering descending will return the maximum value- Returns:
- Returns the min or max value based on the field in the parameters.
- Throws:
SearchServiceException
-
-