-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Hibernate Search 5 - ShardIdentifierProvider
PostPosted: Fri Oct 03, 2014 10:43 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
I'm using this interface for sharding. I'm not using the ShardIdentifierProviderTemplate because during the delete/purge/purgeAll methods I need to filter only some shards.

My problem is initialize method, where I must populate the initial shards. Is there a way to use current the SessionFactory for execute a selecting query on database?

The example on documentation uses the "requestService" method of the ServiceManager class, but in the current version the parameters of the method are changed and I can't seem to use it.

Code:
public static class AnimalShardIdentifierProvider extends ShardIdentifierProviderTemplate {

  ...

@Override
protected Set<String> loadInitialShardNames(Properties properties, BuildContext buildContext) {
    ServiceManager serviceManager = buildContext.getServiceManager();
    SessionFactory sessionFactory = serviceManager.requestService(
        HibernateSessionFactoryServiceProvider.class, buildContext);
    Session session = sessionFactory.openSession();
    try {
       Criteria initialShardsCriteria = session.createCriteria(Animal.class);
       initialShardsCriteria.setProjection( Projections.distinct(Property.forName("type")));

       @SuppressWarnings("unchecked")
       List<String> initialTypes = initialShardsCriteria.list();
       return new HashSet<String>(initialTypes);
    }
    finally {
       session.close();
    }
}
}


Top
 Profile  
 
 Post subject: Re: Hibernate Search 5 - ShardIdentifierProvider
PostPosted: Mon Oct 06, 2014 9:44 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
thanks for noticinig! we missed to update that example.

I'll fix it as part of https://hibernate.atlassian.net/browse/HSEARCH-1595

A preview for the correct version is:


Code:
public static class AnimalShardIdentifierProvider extends ShardIdentifierProviderTemplate {

@Override
public String getShardIdentifier(Class<?> entityType, Serializable id,
         String idAsString, Document document) {
    if ( entityType.equals(Animal.class) ) {
       String typeValue = document.getField("type").stringValue();
       addShard(typeValue);
       return typeValue;
    }
    throw new RuntimeException("Animal expected but found " + entityType);
}

@Override
protected Set<String> loadInitialShardNames(Properties properties, BuildContext buildContext) {
    ServiceManager serviceManager = buildContext.getServiceManager();
    SessionFactory sessionFactory = serviceManager.requestService(
        HibernateSessionFactoryService.class).getSessionFactory();
    Session session = sessionFactory.openSession();
    try {
       Criteria initialShardsCriteria = session.createCriteria(Animal.class);
       initialShardsCriteria.setProjection( Projections.distinct(Property.forName("type")));
       List<String> initialTypes = initialShardsCriteria.list();
       return new HashSet<String>(initialTypes);
    }
    finally {
       session.close();
    }
}
}

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Hibernate Search 5 - ShardIdentifierProvider
PostPosted: Mon Oct 06, 2014 10:03 am 
Beginner
Beginner

Joined: Wed Aug 06, 2014 10:53 am
Posts: 30
Hi Sanne,
thanks a lot, your example was been very usefull!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.