cancel
Showing results for 
Search instead for 
Did you mean: 

Problem searching for aspect with Lucene

turgayz
Champ in-the-making
Champ in-the-making
Hi,
I have the following test case, which searches for nodes that have the "Auditable" aspect. I know that at least the "guest_home" node has this aspect, after checking with the Node Browser. But the search cannot find any nodes. Please help, what is wrong with my search?
Note: I am using the latest repository code.

import java.util.List;

import junit.framework.TestCase;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.search.impl.lucene.LuceneIndexerAndSearcher;
import org.alfresco.repo.search.impl.lucene.LuceneSearcherImpl;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.ApplicationContextHelper;
import org.springframework.context.ApplicationContext;

public class LuceneTest extends TestCase{
   static NodeService nodeService = null;
   static DictionaryService dictionaryService = null;
   static AuthenticationComponent authenticationComponent = null;
   static LuceneIndexerAndSearcher indexerAndSearcher = null;
   static boolean setupCompleted = false;
   
   @Override
   protected void setUp() throws Exception {
      if(!setupCompleted) {
      // initialise app content
      ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();
      // get registry of services
      ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);

      // get required services
      nodeService = serviceRegistry.getNodeService();
      dictionaryService = serviceRegistry.getDictionaryService();

      indexerAndSearcher = (LuceneIndexerAndSearcher) ctx.getBean("luceneIndexerAndSearcherFactory");

      authenticationComponent = (AuthenticationComponent) ctx.getBean("authenticationComponent");
      authenticationComponent.setSystemUserAsCurrentUser();
      setupCompleted = true;
      }
   }

   public void testFindAllAuditableAspectsInTheRepository() {
      LuceneSearcherImpl searcher = LuceneSearcherImpl.getSearcher(getCompanyHome().getStoreRef(), indexerAndSearcher);
      String queryStr = "ASPECT:\"" + ContentModel.ASPECT_AUDITABLE.toString() + "\"";
      searcher.setDictionaryService(dictionaryService);
      ResultSet results = searcher.query(getCompanyHome().getStoreRef(), "lucene", queryStr, null, null);
      assertTrue(results.getNodeRefs().size()>0);
   }

   
   private NodeRef getCompanyHome() {
      StoreRef spacesStoreRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");

      NodeRef rootNodeRef = nodeService.getRootNode(spacesStoreRef);
      List<ChildAssociationRef> childRefs = nodeService.getChildAssocs(rootNodeRef,
            RegexQNamePattern.MATCH_ALL, QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "company_home"));
      ChildAssociationRef companyHomeRef = childRefs.get(0);
      NodeRef companyHome = companyHomeRef.getChildRef();

      return companyHome;
   }

}
2 REPLIES 2

andy
Champ on-the-rise
Champ on-the-rise
Hi

I have just tried


ASPECT:"{http://www.alfresco.org/model/content/1.0}auditable"

via the node browser on the admin console and this is fine.

Have you changed any of the Spring configuration? Why are you not using the service registry to get the search service?

Regards

Andy

turgayz
Champ in-the-making
Champ in-the-making
Hi Andy,
Thanks, I tried getting the search service from the service registry, and the search works now Smiley Happy

I guess that method I used before cannot be used anymore. i.e. getting the indexerAndSearcher and then using it. This way it is more clear:

ResultSet results = serviceRegistry.getSearchService().query(getCompanyHome().getStoreRef(), "lucene", "ASPECT:\"" + ContentModel.ASPECT_AUDITABLE.toString() + "\"");