如何为搜索查询选择分片?

时间:2016-12-07 12:53:39

标签: hibernate-search

我最近实现了ShardIdentifierProvider。它工作正常。但是如何确保它只使用一个共享查询?

public class SiteIdAsShardIdProvider 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 {
        @SuppressWarnings("unchecked")
        List<String> ids = session.createSQLQuery("select cast(id as CHAR(3)) from website").list();
        return new HashSet<>(ids);
    } finally {
        session.close();
    }
}

@Override
public String getShardIdentifier(Class<?> entityType, Serializable id, String idAsString, Document document) {
    return document.getFieldable("siteId").stringValue();
}
}

2 个答案:

答案 0 :(得分:1)

创建自己的自定义过滤器并覆盖getShardIdentifiersForQuery应该可以解决问题。这与the documentation中的内容大致相同,但有ShardIdentifierProviderTemplate

@Override
public Set<String> getShardIdentifiersForQuery(FullTextFilterImplementor[] filters) {
    FullTextFilter filter = getFilterByName( filters, "customer" );
    if ( filter == null ) {
        return getAllShardIdentifiers();
    }
    else {
        Set<String> result = new HashSet<>();
        result.add( filter.getParameter( "customerID" ) );
        return result;
    }
}

private FullTextFilter getFilterByName(FullTextFilterImplementor[] filters, String name) {
    for ( FullTextFilterImplementor filter: filters ) {
        if ( filter.getName().equals( name ) ) {
            return filter;
        }
    }
    return null;
}

我创建了一张更新文档的凭单:https://hibernate.atlassian.net/browse/HSEARCH-2513

答案 1 :(得分:0)

查询时的分片选择是使用自定义过滤器控制的。

有关详细信息和示例,请参阅"5.3.1. Using filters in a sharded environment"