SearchMapping mapping = ...; mapping.entity( SpecialArticle.class ) .indexed() .property( "uid", ElementType.FIELD ).documentId() .property( "someSpecialAttribute", ElementType.FIELD ).field(); //Map the super class directly, but don't call "indexed()" mapping.entity( Article.class ) .property( "articleNumber", ElementType.FIELD ).field() .property( "texts", ElementType.FIELD ) .indexEmbedded().targetElement( ArticleText.class ).entity( ArticleText.class ) .property( "article", ElementType.FIELD ).containedIn() .property( "someText", ElementType.FIELD ).field();
/** Built-in default programatic mapping for hibernate-search **/ private static SearchMapping getBuiltinMapping() { SearchMapping mapping = new SearchMapping(); // Map the getExternalId() method as a documentId for all domain classes // Note that hibernate-search currently requires this method to really exist in the // AbstractDomainObject class mapping.entity(AbstractDomainObject.class).property("externalId", ElementType.METHOD).documentId().name("id"); return mapping; }
@SuppressWarnings("unused") @Factory public SearchMapping buildSearchMapping() { SearchMapping searchMapping = new SearchMapping(); searchMapping.entity(MySearchableEntity.class); return searchMapping; }
@Override public void defineMappings(Cache cache, SearchMapping searchMapping) { searchMapping.entity(GenericData.Record.class) .indexed() .classBridgeInstance(new ValueWrapperFieldBridge()) .norms(Norms.NO) .analyze(Analyze.NO) .store(Store.NO); } }
@Override public void configure(Map<String,Object> cfg) { SearchMapping mapping = new SearchMapping(); mapping .entity( PlainPerson.class ) .indexed() .property( "id", ElementType.FIELD ) .documentId() .bridge( PersonPKMetadataProviderBridge.class ) .property( "", ElementType.FIELD ) .field(); cfg.put( Environment.MODEL_MAPPING, mapping ); }
@Override public void configure(Map<String,Object> cfg) { SearchMapping mapping = new SearchMapping(); mapping .entity( PlainPerson.class ) .indexed() .property( "id", ElementType.FIELD ) .documentId() .bridge( PersonPKBridge.class ) .property( "", ElementType.FIELD ) .field(); cfg.put( Environment.MODEL_MAPPING, mapping ); }
@SuppressWarnings("unchecked") private void verifyOptimizerImplementationIs(Class type, SearchConfigurationForTest cfg) { SearchMapping mapping = new SearchMapping(); mapping .entity( Document.class ).indexed() .property( "id", ElementType.FIELD ).documentId() .property( "title", ElementType.FIELD ).field() ; cfg.setProgrammaticMapping( mapping ); cfg.addClass( Document.class ); SearchIntegrator sf = integratorResource.create( cfg ); EntityIndexBinding indexBindingForEntity = sf.getIndexBindings().get( Document.class ); DirectoryBasedIndexManager indexManager = (DirectoryBasedIndexManager) indexBindingForEntity.getIndexManagerSelector().all().iterator().next(); OptimizerStrategy optimizerStrategy = indexManager.getOptimizerStrategy(); Assert.assertTrue( type.isAssignableFrom( optimizerStrategy.getClass() ) ); }
@Test public void testTypeWithNoDocumentIdThrowsException() { SearchConfigurationForTest cfg = getManualConfiguration(); SearchMapping mapping = new SearchMapping(); mapping .entity( Foo.class ).indexed() ; cfg.setProgrammaticMapping( mapping ); try { integratorResource.create( cfg ); fail( "Invalid configuration should have thrown an exception" ); } catch (SearchException e) { assertTrue( e.getMessage().startsWith( "HSEARCH000177" ) ); } }
@Test public void testGetIndexedTypeSingleIndexedType() { SearchConfigurationForTest cfg = getManualConfiguration(); SearchMapping mapping = new SearchMapping(); mapping .entity( Foo.class ).indexed() .property( "id", FIELD ).documentId() ; cfg.setProgrammaticMapping( mapping ); SearchIntegrator si = integratorResource.create( cfg ); IndexedTypeSet indexedClasses = si.getIndexedTypeIdentifiers(); assertEquals( "Wrong number of indexed entities", 1, indexedClasses.size() ); assertTrue( indexedClasses.iterator().next().equals( new PojoIndexedTypeIdentifier( Foo.class ) ) ); }
private SearchMapping createSearchMapping() { SearchMapping mapping = new SearchMapping(); mapping.entity( ProgrammaticConfiguredValue.class ) .indexed() .property( "id", ElementType.FIELD ).documentId().name( "id" ) .property( "value", ElementType.FIELD ).field().store( Store.YES ).indexNullAs( "@null@" ); return mapping; }
protected MutableSearchFactory getMutableSearchFactoryWithSingleEntity(SearchConfigurationForTest cfg) { SearchMapping mapping = new SearchMapping(); mapping .entity( Document.class ).indexed().indexName( "index1" ) .property( "id", ElementType.FIELD ).documentId() .property( "title", ElementType.FIELD ).field() ; cfg.setProgrammaticMapping( mapping ); cfg.addClass( Document.class ); return (MutableSearchFactory) integratorResource.create( cfg ); }
@Before public void setUp() { manualConfiguration = new SearchConfigurationForTest(); SearchMapping searchMapping = new SearchMapping(); searchMapping.entity( Document.class ).indexed() .property( "id", ElementType.FIELD ).documentId() .property( "title", ElementType.FIELD ).field(); manualConfiguration.setProgrammaticMapping( searchMapping ); manualConfiguration.addClass( Document.class ); }
@Test public void documentIdNotOverriden() { SearchMapping mapping = new SearchMapping(); mapping .entity( Book.class ).indexed() .property( "title", ElementType.FIELD ).documentId() .property( "text", ElementType.FIELD ).field() ; SearchConfigurationForTest cfg = new SearchConfigurationForTest() .setProgrammaticMapping( mapping ) //.setIdProvidedImplicit( false ) //Test it's the default .addClass( Book.class ); storeBooksViaProvidedId( cfg, "title", true ); }
@Test public void usingConfigurationTypeOverride() { SearchMapping mapping = new SearchMapping(); mapping .entity( Book.class ).indexed() //Entity missing both @DocumentId and @ProvidedId: .property( "title", ElementType.FIELD ).field() .property( "text", ElementType.FIELD ).field() ; SearchConfigurationForTest cfg = new SearchConfigurationForTest() .setProgrammaticMapping( mapping ) .setIdProvidedImplicit( true ) .addClass( Book.class ); storeBooksViaProvidedId( cfg, ProvidedId.defaultFieldName, false ); }
@Test public void testGetTypeDescriptorForIndexedType() { SearchConfigurationForTest cfg = getManualConfiguration(); SearchMapping mapping = new SearchMapping(); mapping .entity( Foo.class ).indexed() .property( "id", FIELD ).documentId() ; cfg.setProgrammaticMapping( mapping ); SearchIntegrator si = integratorResource.create( cfg ); IndexedTypeDescriptor indexedTypeDescriptor = si.getIndexedTypeDescriptor( new PojoIndexedTypeIdentifier( Foo.class ) ); assertNotNull( indexedTypeDescriptor ); assertTrue( indexedTypeDescriptor.isIndexed() ); }
@Test public void exceptionThrownWhenNotEnabled() { SearchMapping mapping = new SearchMapping(); mapping .entity( Book.class ).indexed() //Entity missing both @DocumentId and @ProvidedId: .property( "title", ElementType.FIELD ).field() .property( "text", ElementType.FIELD ).field() ; SearchConfigurationForTest cfg = new SearchConfigurationForTest() .setProgrammaticMapping( mapping ) .addClass( Book.class ); exceptions.expect( SearchException.class ); exceptions.expectMessage( "HSEARCH000177" ); storeBooksViaProvidedId( cfg, ProvidedId.defaultFieldName, false ); }
private void verifyTransactionsExpectedOption(boolean expectation, SearchConfigurationForTest cfg) { SearchMapping mapping = new SearchMapping(); mapping .entity( Document.class ).indexed() .property( "id", ElementType.FIELD ).documentId() .property( "title", ElementType.FIELD ).field() ; cfg.setProgrammaticMapping( mapping ); cfg.addClass( Document.class ); MutableSearchFactory sf = (MutableSearchFactory) integratorResource.create( cfg ); Assert.assertEquals( expectation, sf.isTransactionManagerExpected() ); // trigger a SearchFactory rebuild: sf.addClasses( Dvd.class ); // and verify the option is not lost: Assert.assertEquals( expectation, sf.isTransactionManagerExpected() ); }
@Test public void usingProvidedIdAsOptionsOverride() { SearchMapping mapping = new SearchMapping(); mapping .entity( Book.class ).indexed() .providedId().name( "myID" ) //Entity missing both @DocumentId and @ProvidedId: .property( "title", ElementType.FIELD ).field() .property( "text", ElementType.FIELD ).field() ; SearchConfigurationForTest cfg = new SearchConfigurationForTest() .setProgrammaticMapping( mapping ) .setIdProvidedImplicit( true ) .addClass( Book.class ); storeBooksViaProvidedId( cfg, "myID", false ); }
@Test public void usingExplicitProvidedId() { SearchMapping mapping = new SearchMapping(); mapping .entity( Book.class ).indexed() .providedId().name( "myID" ) //Entity missing both @DocumentId and @ProvidedId: .property( "title", ElementType.FIELD ).field() .property( "text", ElementType.FIELD ).field() ; SearchConfigurationForTest cfg = new SearchConfigurationForTest() .setProgrammaticMapping( mapping ) .setIdProvidedImplicit( false ) //DEFAULT .addClass( Book.class ); storeBooksViaProvidedId( cfg, "myID", false ); }
@Test public void testGetIndexedTypesMultipleTypes() { SearchConfigurationForTest cfg = getManualConfiguration(); SearchMapping mapping = new SearchMapping(); mapping .entity( Foo.class ).indexed() .property( "id", FIELD ).documentId() .entity( Bar.class ).indexed() .property( "id", FIELD ).documentId() ; cfg.setProgrammaticMapping( mapping ); SearchIntegrator si = integratorResource.create( cfg ); IndexedTypeSet indexedClasses = si.getIndexedTypeIdentifiers(); assertEquals( "Wrong number of indexed entities", 2, indexedClasses.size() ); }