Package org.olat.search.service

Source Code of org.olat.search.service.SearchServiceImpl

/**
* OLAT - Online Learning and Training<br>
* http://www.olat.org
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); <br>
* you may not use this file except in compliance with the License.<br>
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing,<br>
* software distributed under the License is distributed on an "AS IS" BASIS, <br>
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
* See the License for the specific language governing permissions and <br>
* limitations under the License.
* <p>
* Copyright (c) since 2004 at Multimedia- & E-Learning Services (MELS),<br>
* University of Zurich, Switzerland.
* <p>
*/

package org.olat.search.service;

import java.util.Calendar;
import java.util.Date;
import java.util.Set;

import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.olat.core.CoreSpringFactory;
import org.olat.core.commons.services.search.QueryException;
import org.olat.core.commons.services.search.SearchModuleConfig;
import org.olat.core.commons.services.search.SearchResults;
import org.olat.core.commons.services.search.SearchService;
import org.olat.core.commons.services.search.ServiceNotAvailableException;
import org.olat.core.id.Identity;
import org.olat.core.id.Roles;
import org.olat.core.logging.AssertException;
import org.olat.core.logging.OLog;
import org.olat.core.logging.Tracing;
import org.olat.search.service.indexer.Index;
import org.olat.search.service.searcher.OLATSearcher;
import org.olat.search.service.searcher.Search;
import org.olat.search.service.spell.SearchSpellChecker;
import org.olat.search.service.update.IndexUpdater;

/**
*
* @author Christian Guretzki
*/
public class SearchServiceImpl implements SearchService {
  private static final OLog log = Tracing.createLoggerFor(SearchServiceImpl.class);
 
  static final SearchServiceImpl instance = new SearchServiceImpl();
 
  // Const
  ////////
 
  // Attributes
  /////////////
  private Index indexer; 
  private OLATSearcher search;
  private SearchModuleConfig searchModuleConfig;
  private IndexUpdater indexUpdater;
   

  private boolean hasLocalImplementation;
   
  // Constructor
  //////////////
  private SearchServiceImpl() {
    // initialize in init-method
  }
 
  /**
   * Factory method to get an instance.
   * @return
   */
  public static SearchService getInstance() {
    return instance;
  }
 
  // TODO: chg: synchonized fix IOException: The handle is invalid, perhaps can be removed with newer lucene version.
  public SearchResults doSearch(String query, Identity identity, Roles roles, boolean doHighlighting) throws ServiceNotAvailableException, ParseException, QueryException {
    boolean doLog = log.isDebug();
    long start = 0;
    if (doLog) start = System.currentTimeMillis();   
    SearchResults searchResults = search.doSearch(query, identity, roles, doHighlighting);

    if (doLog) {
      long searchTime = (System.currentTimeMillis() - start);
      log.debug("Search query: " + query + " time: " + searchTime);
    }
    return searchResults;
  }
 
  public void addToIndex(Document document) {
    if (indexUpdater==null) throw new AssertException ("Try to call addToIndex() but indexUpdater is null!");
    log.info("addToIndex document=" + document);
    indexUpdater.addToIndex(document);
  }
 
  public void startIndexing() {
    if (indexer==null) throw new AssertException ("Try to call startIndexing() but indexer is null");
    indexer.startFullIndex();
    log.info("startIndexing...");
  }

  public void stopIndexing() {
    if (indexer==null) throw new AssertException ("Try to call stopIndexing() but indexer is null");
    indexer.stopFullIndex();
    log.info("stopIndexing.");
  }

  public void deleteFromIndex(Document document) {
    if (indexUpdater==null) throw new AssertException ("Try to call deleteFromIndex() but indexUpdater is null");
    log.info("deleteFromIndex document=" + document);
    indexUpdater.deleteFromIndex(document);
  }

  public void init(SearchModuleConfig searchModuleConfig) {
    this.searchModuleConfig = searchModuleConfig;
    //TODO:xy:a refactoring: create two implementations of search service a client version and a remote version via spring config and remove
    //the method isLocal()
    hasLocalImplementation = searchModuleConfig.getImplementation()!=null && !searchModuleConfig.getImplementation().equals("");
   
    log.info("Running with indexPath=" + searchModuleConfig.getFullIndexPath());
    log.info("        tempIndexPath=" + searchModuleConfig.getFullTempIndexPath());
    log.info("        generateAtStartup=" + searchModuleConfig.getGenerateAtStartup());
    log.info("        restartInterval=" + searchModuleConfig.getRestartInterval());
    log.info("        indexInterval=" + searchModuleConfig.getIndexInterval());
    log.info("        implementation=" + searchModuleConfig.getImplementation());

    SearchSpellChecker spellChecker = null;
    if (hasLocalImplementation) {     
      spellChecker = new SearchSpellChecker();
      spellChecker.setIndexPath(searchModuleConfig.getFullIndexPath());
      spellChecker.setSpellDictionaryPath(searchModuleConfig.getSpellCheckDictionaryPath());
      spellChecker.setSpellCheckEnabled(searchModuleConfig.getSpellCheckEnabled());
     
      indexer = new Index(searchModuleConfig.getFullIndexPath(),
        searchModuleConfig.getFullTempIndexPath()
        searchModuleConfig.getRestartInterval(),
        searchModuleConfig.getIndexInterval(),
        spellChecker);
      indexUpdater = new IndexUpdater(searchModuleConfig.getFullIndexPath(), searchModuleConfig.getUpdateInterval());
    }
    //search set via spring
    OLATSearcher implDelegate = null;
    if(hasLocalImplementation) {
      implDelegate = (OLATSearcher)CoreSpringFactory.getBean(searchModuleConfig.getImplementation());
      if(implDelegate instanceof Search) { //TODO: find solution to avoid casting
        ((Search)implDelegate).setIndexPath(searchModuleConfig.getFullIndexPath());
        ((Search)implDelegate).setSearchSpellChecker(spellChecker);
      }    
    }
    //if proxy configured
    if(searchModuleConfig.getProxy()!=null && !searchModuleConfig.getProxy().equals("")) {
      search = (OLATSearcher)CoreSpringFactory.getBean(searchModuleConfig.getProxy());     
    } else if(implDelegate!=null) {
      search = implDelegate;
    } else {
      //if no proxy and no implementation configured -> wrong configuration!
      log.info("        no proxy and no implementation configured !!!");
    }       
 
    if (hasLocalImplementation && startingFullAllowed()) {
      indexer.startFullIndex();
    }
    log.info("init DONE");
  }

  /**
   * Check if starting a generating full-index is allowed.
   * Depends config-parameter 'generateAtStartup', day of the week and
   * config-parameter 'restartDayOfWeek', current time and the restart-
   * window (config-parameter 'restartWindowStart', 'restartWindowEnd')
   * @return  TRUE: Starting is allowed
   */
  private boolean startingFullAllowed() {
    if (searchModuleConfig.getGenerateAtStartup()) {
      Calendar calendar = Calendar.getInstance();
      calendar.setTime(new Date());
      // check day, Restart only at config day of week, 0-7 8=every day
      int dayNow = calendar.get(Calendar.DAY_OF_WEEK);
      int restartDayOfWeek = searchModuleConfig.getRestartDayOfWeek();
      if (restartDayOfWeek == 0 || (dayNow == restartDayOfWeek) ) {
        // check time, Restart only in the config time-slot e.g. 01:00 - 03:00
        int hourNow = calendar.get(Calendar.HOUR_OF_DAY);
        int restartWindowStart = searchModuleConfig.getRestartWindowStart();
        int restartWindowEnd   = searchModuleConfig.getRestartWindowEnd();
        if ( (restartWindowStart <= hourNow) && (hourNow < restartWindowEnd) ) {
          return true;
        }       
      }
    }
    return false;
  }
 
  public SearchServiceStatus getStatus() {
    return new SearchServiceStatus(indexer,search);
  }

  public void setIndexInterval(long indexInterval) {
    if (indexer==null) throw new AssertException ("Try to call setIndexInterval() but indexer is null");
    indexer.setIndexInterval(indexInterval);
  }
 
  public long getIndexInterval() {
    if (indexer==null) throw new AssertException ("Try to call setIndexInterval() but indexer is null");
    return indexer.getIndexInterval();
  }
 
  /**
   *
   * @return  Resturn search module configuration.
   */
  public SearchModuleConfig getSearchModuleConfig() {
    return searchModuleConfig;
  }

  /**
   *
   * @see org.olat.search.service.SearchService#spellCheck(java.lang.String)
   */
  public Set<String> spellCheck(String query) throws ServiceNotAvailableException {   
    return search.spellCheck(query);
  }

  public void stop() {
    stopIndexing();
    search.stop();
  }

  /**
   * @see org.olat.search.service.SearchService#isLocal()
   */
  public boolean isLocal() {
    return hasLocalImplementation;
  }

}
TOP

Related Classes of org.olat.search.service.SearchServiceImpl

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.