Class HibernateEmbeddingStore<E>
java.lang.Object
dev.langchain4j.store.embedding.hibernate.HibernateEmbeddingStore<E>
- All Implemented Interfaces:
EmbeddingStore<TextSegment>
Hibernate ORM EmbeddingStore Implementation
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final booleanprotected final DatabaseKindprotected final DistanceFunctionprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final org.hibernate.persister.entity.EntityPersisterprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final org.hibernate.generator.Generatorprotected final org.hibernate.type.descriptor.java.JavaType<Object> protected final booleanprotected final org.hibernate.SessionFactoryprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final jakarta.persistence.metamodel.Type<Map<?, ?>> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedHibernateEmbeddingStore(boolean isDynamic, org.hibernate.SessionFactory sessionFactory, DatabaseKind databaseKind, Class<E> entityClass, String embeddingAttributeName, String embeddedTextAttributeName, String unmappedMetadataAttributeName, String[] metadataAttributePaths, DistanceFunction distanceFunction) Constructor for HibernateEmbeddingStore Class -
Method Summary
Modifier and TypeMethodDescriptionAdds a given embedding to the store.add(Embedding embedding, TextSegment textSegment) Adds a given embedding and the corresponding content that has been embedded to the store.voidAdds a given embedding to the store.Adds multiple embeddings to the store.addAll(List<Embedding> embeddings, List<TextSegment> embedded) Adds multiple embeddings and their corresponding contents that have been embedded to the store.voidAdds multiple embeddings and their corresponding contents that have been embedded to the store.voidaddAllEntities(List<?> entities) static <E> HibernateEmbeddingStore.Builder<E> A builder for creating a Hibernate basedEmbeddingStorefor an existingSessionFactoryand entity classes.voidclose()protected jakarta.persistence.criteria.PredicatedistanceFilter(DistanceFunction distanceFunction, jakarta.persistence.criteria.Expression<Double> distance, jakarta.persistence.criteria.Expression<Double> minScore, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) A builder for creating a Hibernate basedEmbeddingStorewhen noSessionFactoryor entity classes exist.A builder for creating a Hibernate basedEmbeddingStorewhen noSessionFactoryor entity classes exist and a datasource shall be used.generateIds(int n) Generates list of UUID stringsquery(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction) Searches for the most similar (closest in the embedding space) entities based onEmbeddings.query(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction, int maxResults) Searches for the most similar (closest in the embedding space) entities based onEmbeddings.Searches for the most similar (closest in the embedding space) entities based onEmbeddings.voidRemoves all embeddings from the store.voidRemoves all embeddings that match the specifiedFilterfrom the store.voidremoveAll(Collection<String> ids) Removes all embeddings that match the specified IDs from the store.protected jakarta.persistence.criteria.Expression<Double> score(DistanceFunction distanceFunction, jakarta.persistence.criteria.Expression<Double> distance, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) search(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction) Searches for the most similar (closest in the embedding space)Embeddings.search(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction, int maxResults) Searches for the most similar (closest in the embedding space)Embeddings.Searches for the most similar (closest in the embedding space)Embeddings.search(EmbeddingSearchRequest request) Searches for the most similar (closest in the embedding space)Embeddings.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface EmbeddingStore
addListener, addListeners, remove
-
Field Details
-
isDynamic
protected final boolean isDynamic -
sessionFactory
protected final org.hibernate.SessionFactory sessionFactory -
databaseKind
-
entityClass
-
entityPersister
protected final org.hibernate.persister.entity.EntityPersister entityPersister -
idType
-
idGenerator
protected final org.hibernate.generator.Generator idGenerator -
allowUuidGeneration
protected final boolean allowUuidGeneration -
idAttributeMapping
protected final org.hibernate.metamodel.mapping.AttributeMapping idAttributeMapping -
embeddingAttributeMapping
protected final org.hibernate.metamodel.mapping.AttributeMapping embeddingAttributeMapping -
embeddedTextAttributeMapping
protected final org.hibernate.metamodel.mapping.AttributeMapping embeddedTextAttributeMapping -
unmappedMetadataAttributeMapping
protected final org.hibernate.metamodel.mapping.AttributeMapping unmappedMetadataAttributeMapping -
unmappedMetadataAttributeMapType
-
metadataAttributeMappings
-
distanceFunction
-
-
Constructor Details
-
HibernateEmbeddingStore
protected HibernateEmbeddingStore(boolean isDynamic, org.hibernate.SessionFactory sessionFactory, DatabaseKind databaseKind, Class<E> entityClass, String embeddingAttributeName, String embeddedTextAttributeName, String unmappedMetadataAttributeName, String[] metadataAttributePaths, DistanceFunction distanceFunction) Constructor for HibernateEmbeddingStore Class- Parameters:
isDynamic- Whether the session factory was created dynamicallysessionFactory- The Hibernate session factory to usedatabaseKind- The database kindentityClass- The Hibernate entity class to useembeddingAttributeName- The name of the entity attribute containing the embedding vectorembeddedTextAttributeName- The name of the entity attribute containing the text from which the embedding vector is derived, or nullunmappedMetadataAttributeName- The name of the entity attribute to store generic metadata inmetadataAttributePaths- The name of the explicit metadata entity attributesdistanceFunction- The distance function to use for vector search
-
HibernateEmbeddingStore
public HibernateEmbeddingStore()
-
-
Method Details
-
builder
A builder for creating a Hibernate basedEmbeddingStorefor an existingSessionFactoryand entity classes.- Returns:
- The builder
-
dynamicBuilder
A builder for creating a Hibernate basedEmbeddingStorewhen noSessionFactoryor entity classes exist.- Returns:
- The builder
-
dynamicDatasourceBuilder
A builder for creating a Hibernate basedEmbeddingStorewhen noSessionFactoryor entity classes exist and a datasource shall be used.- Returns:
- The builder
-
close
public void close() -
add
Adds a given embedding to the store.- Specified by:
addin interfaceEmbeddingStore<E>- Parameters:
embedding- The embedding to be added to the store.- Returns:
- The auto-generated ID associated with the added embedding.
-
add
Adds a given embedding to the store.- Specified by:
addin interfaceEmbeddingStore<E>- Parameters:
id- The unique identifier for the embedding to be added.embedding- The embedding to be added to the store.
-
add
Adds a given embedding and the corresponding content that has been embedded to the store.- Specified by:
addin interfaceEmbeddingStore<E>- Parameters:
embedding- The embedding to be added to the store.textSegment- Original content that was embedded.- Returns:
- The auto-generated ID associated with the added embedding.
-
addAll
-
removeAll
Description copied from interface:EmbeddingStoreRemoves all embeddings that match the specified IDs from the store.- Specified by:
removeAllin interfaceEmbeddingStore<E>- Parameters:
ids- A collection of unique IDs of the embeddings to be removed.
-
removeAll
Description copied from interface:EmbeddingStoreRemoves all embeddings that match the specifiedFilterfrom the store.- Specified by:
removeAllin interfaceEmbeddingStore<E>- Parameters:
filter- The filter to be applied to theMetadataof theTextSegmentduring removal. Only embeddings whoseTextSegment'sMetadatamatch theFilterwill be removed.
-
removeAll
public void removeAll()Description copied from interface:EmbeddingStoreRemoves all embeddings from the store.- Specified by:
removeAllin interfaceEmbeddingStore<E>
-
query
public List<E> query(Embedding embedding, org.hibernate.query.restriction.Restriction<E> restriction) Searches for the most similar (closest in the embedding space) entities based onEmbeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the queryrestriction- Filter for metadata- Returns:
- The list of all found entities.
-
query
public List<E> query(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction) Searches for the most similar (closest in the embedding space) entities based onEmbeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the queryminScore- The minimum distance scorerestriction- Filter for metadata- Returns:
- The list of all found entities.
-
query
public List<E> query(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction, int maxResults) Searches for the most similar (closest in the embedding space) entities based onEmbeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the queryminScore- The minimum distance scorerestriction- Filter for metadatamaxResults- The maximum number of results- Returns:
- The list of all found entities.
-
search
public EmbeddingSearchResult<TextSegment> search(Embedding embedding, org.hibernate.query.restriction.Restriction<E> restriction) Searches for the most similar (closest in the embedding space)Embeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the searchrestriction- Filter for metadata- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
search
public EmbeddingSearchResult<TextSegment> search(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction) Searches for the most similar (closest in the embedding space)Embeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the searchminScore- The minimum distance scorerestriction- Filter for metadata- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
search
public EmbeddingSearchResult<TextSegment> search(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction, int maxResults) Searches for the most similar (closest in the embedding space)Embeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the searchminScore- The minimum distance scorerestriction- Filter for metadatamaxResults- The maximum number of results- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
search
Searches for the most similar (closest in the embedding space)Embeddings.
All search criteria are defined inside theEmbeddingSearchRequest.
EmbeddingSearchRequest.filter()is used to filter by meta data.- Specified by:
searchin interfaceEmbeddingStore<E>- Parameters:
request- A request to search in anEmbeddingStore. Contains all search criteria.- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
score
protected jakarta.persistence.criteria.Expression<Double> score(DistanceFunction distanceFunction, jakarta.persistence.criteria.Expression<Double> distance, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) -
distanceFilter
protected jakarta.persistence.criteria.Predicate distanceFilter(DistanceFunction distanceFunction, jakarta.persistence.criteria.Expression<Double> distance, jakarta.persistence.criteria.Expression<Double> minScore, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder) -
addAllEntities
-
generateIds
Description copied from interface:EmbeddingStoreGenerates list of UUID strings- Specified by:
generateIdsin interfaceEmbeddingStore<E>- Parameters:
n- - dimension of list
-
addAll
Description copied from interface:EmbeddingStoreAdds multiple embeddings and their corresponding contents that have been embedded to the store.- Specified by:
addAllin interfaceEmbeddingStore<E>- Parameters:
embeddings- A list of embeddings to be added to the store.embedded- A list of original contents that were embedded.- Returns:
- A list of auto-generated IDs associated with the added embeddings.
-
addAll
Description copied from interface:EmbeddingStoreAdds multiple embeddings and their corresponding contents that have been embedded to the store.- Specified by:
addAllin interfaceEmbeddingStore<E>- Parameters:
idStrings- A list of IDs associated with the added embeddings.embeddings- A list of embeddings to be added to the store.embedded- A list of original contents that were embedded.
-