/*
 * Copyright 2008-2009 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.broadleafcommerce.search.service;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.broadleafcommerce.catalog.domain.Product;
import org.broadleafcommerce.catalog.service.CatalogService;
import org.compass.core.Compass;
import org.compass.core.CompassContext;
import org.compass.core.CompassDetachedHits;
import org.compass.core.CompassIndexSession;
import org.compass.core.CompassSearchSession;
import org.compass.core.engine.SearchEngineIndexManager;
import org.springframework.stereotype.Service;

@Service("blSearchService")
public class SearchServiceCompassImpl implements SearchService {

    @CompassContext
    protected Compass compass;

    @Resource(name="blCatalogService")
    protected CatalogService catalogService;

    @Override
    public List<Product> performSearch(String input) {
        CompassSearchSession session = compass.openSearchSession();
        CompassDetachedHits hits = session.find(input).detach();
        session.close();
        List<Product> results = new ArrayList<Product>(hits.length());
        for(int i = 0; i < hits.length(); i ++ ) {
            Product resourceProduct = (Product) hits.data(i);
            results.add(catalogService.findProductById(resourceProduct.getId()));
        }
        return results;
    }

    @Override
    public void rebuildProductIndex() {
        SearchEngineIndexManager manager = compass.getSearchEngineIndexManager();
        manager.createIndex();
        CompassIndexSession session = compass.openIndexSession();
        List<Product> products = catalogService.findAllProducts();
        for (Product product : products) {
            session.save(product);
        }
        session.commit();
        session.close();
    }

}

@@ -2047,24 +2047,92 @@
 tIndex() %7B%0D%0A
+        List%3CProduct%3E products = catalogService.findAllProducts();%0D%0A
         Sear
@@ -2198,24 +2198,67 @@
 Manager();%0D%0A
+        if (!manager.indexExists()) %7B%0D%0A    
         mana
@@ -2273,24 +2273,35 @@
 teIndex();%0D%0A
+        %7D%0D%0A
         Comp
@@ -2359,76 +2359,8 @@
 );%0D%0A
-        List%3CProduct%3E products = catalogService.findAllProducts();%0D%0A
     

Make index update minimize downtime