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) voidvoidaddAllEntities(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) 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.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.voidvoidvoidremoveAll(Collection<String> ids) 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
- Specified by:
removeAllin interfaceEmbeddingStore<E>
-
removeAll
- Specified by:
removeAllin interfaceEmbeddingStore<E>
-
removeAll
public void removeAll()- 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
- Specified by:
generateIdsin interfaceEmbeddingStore<E>
-
addAll
- Specified by:
addAllin interfaceEmbeddingStore<E>
-
addAll
- Specified by:
addAllin interfaceEmbeddingStore<E>
-