Skip to content

Commit

Permalink
HSEARCH-4336 Deprecate assigning the same index to multiple entity types
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere committed Oct 11, 2021
1 parent 829a604 commit e9773ee
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
4 changes: 4 additions & 0 deletions documentation/src/main/asciidoc/advanced-features.asciidoc
Expand Up @@ -252,6 +252,10 @@ have ideas, or just to let us know how you're using this API.
It is technically possible to store the information of more than one entity into a single Lucene
index. There are two ways to accomplish this:

[WARNING]
====
Support for indexing multiple entity types in the same index is deprecated and is going to be removed in Hibernate Search 6.
====

* Configuring the underlying directory providers to point to the same physical index directory.
In practice, you set the property `hibernate.search.[fully qualified entity name].indexName` to the
Expand Down
Expand Up @@ -467,6 +467,15 @@ else if ( metadataProvider.containsSearchMetadata( mappedClassIdentifier ) ) {
documentBuildersContainedEntities
);

if ( HibernateSearch6DeprecationHelper.isWarningEnabled( searchConfiguration.getProperties() ) ) {
for ( IndexManager indexManager : indexesFactory.getIndexManagers() ) {
IndexedTypeSet indexedTypes = indexManager.getContainedTypes();
if ( indexedTypes.size() > 1 ) {
log.multipleTypesInSingleIndex( indexManager.getIndexName(), indexedTypes );
}
}
}

factoryState.addFilterDefinitions( configContext.initFilters() );
factoryState.addIntegrations( configContext.initIntegrations( indexesFactory ) );
}
Expand Down
Expand Up @@ -35,6 +35,7 @@
import org.hibernate.search.exception.EmptyQueryException;
import org.hibernate.search.exception.SearchException;
import org.hibernate.search.spi.IndexedTypeIdentifier;
import org.hibernate.search.spi.IndexedTypeSet;
import org.hibernate.search.store.DirectoryProvider;
import org.jboss.logging.Logger.Level;
import org.jboss.logging.annotations.Cause;
Expand Down Expand Up @@ -1031,4 +1032,12 @@ public interface Log extends BaseHibernateSearchLogger {
+ " and cannot be used with any other technology (Elasticsearch in particular)."
+ " Analyzer reference: %s")
SearchException invalidConversionFromLuceneAnalyzer(LuceneAnalyzerReference reference, @Cause Exception cause);

@LogMessage(level = Level.WARN)
@Message(id = 355, value = "Index '%1$s' is assigned to multiple entity types: %2$s"
+ " Support for indexing multiple entity types in the same index is going to be removed in Hibernate Search 6."
+ " To assign a dedicated index to each type in a entity class hierarchy,"
+ " use @Indexed(index = \"someUniqueName\") on non-root classes." )
void multipleTypesInSingleIndex(String indexName, IndexedTypeSet indexedTypes);

}
@@ -0,0 +1,72 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.test.configuration;

import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.test.util.impl.ExpectedLog4jLog;
import org.hibernate.search.testsupport.TestForIssue;
import org.hibernate.search.testsupport.junit.SearchIntegratorResource;
import org.hibernate.search.testsupport.setup.SearchConfigurationForTest;

import org.junit.Rule;
import org.junit.Test;

import org.apache.log4j.Level;
import org.hamcrest.CoreMatchers;

@TestForIssue(jiraKey = "HSEARCH-4336")
public class MultipleTypesInSingleIndexDeprecationTest {

@Rule
public final SearchIntegratorResource integratorResource = new SearchIntegratorResource();

@Rule
public ExpectedLog4jLog logged = ExpectedLog4jLog.create();

@Test
public void default_warning() {
SearchConfigurationForTest cfg = new SearchConfigurationForTest();
cfg.addClass( RootEntityType.class );
cfg.addClass( DerivedEntityType.class );

logged.expectEvent( Level.WARN, CoreMatchers.nullValue(),
"Index 'myIndex' is assigned to multiple entity types:",
RootEntityType.class.getName(),
DerivedEntityType.class.getName(),
"Support for indexing multiple entity types in the same index is going to be removed in Hibernate Search 6" );

integratorResource.create( cfg );
}

@Test
public void property_noWarning() {
SearchConfigurationForTest cfg = new SearchConfigurationForTest();
cfg.addClass( RootEntityType.class );
cfg.addClass( DerivedEntityType.class );

cfg.addProperty( "hibernate.search.v6_migration.deprecation_warnings", "false" );

logged.expectLevel( Level.WARN ).never();

integratorResource.create( cfg );
}

@Indexed(index = "myIndex")
public static class RootEntityType {
@DocumentId
private long id;
@Field
private String field;
}

@Indexed
public static class DerivedEntityType extends RootEntityType {
}

}

0 comments on commit e9773ee

Please sign in to comment.