Hibernate Search - Entity/index mapping

Entity/index mapping


In order to index an entity, it must be annotated with @Indexed. All entities not annotated with @Indexed will be ignored by the indexing process.

Marking a class for indexing with @Indexed

@Entity
@Indexed
public class Book {
}


By default:

The index name will be equal to the entity name, which in Hibernate ORM is set using the @Entity annotation and defaults to the simple class name.

The index will be created in the default backend. See the getting stated guide or Structure of configuration properties for more information about how to configure backends.

The identifier of indexed documents will be generated from the entity identifier. Most types commonly used for entity identifiers are supported out of the box, but for more exotic types you may need specific configuration. See Mapping the document identifier for details.

The index won’t have any field. Fields must be mapped to properties explicitly. See Mapping a property to an index field with @GenericField, @FullTextField, …​ for details.

You can change the name of the index by setting @Indexed(index = …​). Note that index names must be unique in a given application.

Explicit index name with @Indexed.index
@Entity
@Indexed(index = "AuthorIndex")
public class Author {
If you defined multiple backends, you can map entities to another backend than the default one. By setting @Indexed(backend = "backend2") you inform Hibernate Search that the index for your entity must be created in the backend named "backend2". This may be useful if your model has clearly defined sub-parts with very different indexing requirements.

Explicit backend with @Indexed.backend
@Entity
@Indexed(backend = "backend2")
public class User {
Entities indexed in different backends cannot be targeted by the same query. For example, with the mappings defined above, and assuming "backend2" is not the default backend, the following code will throw an exception, because Author and User are indexed in different backends:

// This will fail because Author and User are indexed in different backends
List<Object> hits = searchSession.search(
                Arrays.asList( Author.class, User.class )
        )
        .where( f -> f.matchAll() )
        .fetchHits( 20 );

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations