-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathObjectBoxEmbeddingDao.kt
More file actions
182 lines (153 loc) · 6.16 KB
/
Copy pathObjectBoxEmbeddingDao.kt
File metadata and controls
182 lines (153 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package me.grey.picquery.data.dao
import io.objectbox.Box
import io.objectbox.kotlin.query
import io.objectbox.query.ObjectWithScore
import me.grey.picquery.common.calculateSimilarity
import me.grey.picquery.data.model.ObjectBoxEmbedding
import me.grey.picquery.data.model.ObjectBoxEmbedding_
import timber.log.Timber
class ObjectBoxEmbeddingDao(private val embeddingBox: Box<ObjectBoxEmbedding>) {
fun getAll(): List<ObjectBoxEmbedding> {
return embeddingBox.all
}
// 分页查询所有嵌入向量
fun getEmbeddingsPaginated(limit: Int, offset: Int): List<ObjectBoxEmbedding> {
return embeddingBox.query {
orderDesc(ObjectBoxEmbedding_.photoId)
}.find(offset.toLong(), limit.toLong())
}
// 按相册ID分页查询嵌入向量
fun getEmbeddingsByAlbumIdPaginated(albumId: Long, limit: Int, offset: Int): List<ObjectBoxEmbedding> {
return embeddingBox.query {
equal(ObjectBoxEmbedding_.albumId, albumId)
orderDesc(ObjectBoxEmbedding_.photoId)
}.find(offset.toLong(), limit.toLong())
}
// 按多个相册ID分页查询嵌入向量
fun getEmbeddingsByAlbumIdsPaginated(albumIds: List<Long>, limit: Int, offset: Int): List<ObjectBoxEmbedding> {
return embeddingBox.query {
`in`(ObjectBoxEmbedding_.albumId, albumIds.toLongArray())
orderDesc(ObjectBoxEmbedding_.photoId)
}.find(offset.toLong(), limit.toLong())
}
// 获取分页查询的总数
fun getEmbeddingsCountByAlbumIds(albumIds: List<Long>): Long {
return embeddingBox.query {
`in`(ObjectBoxEmbedding_.albumId, albumIds.toLongArray())
orderDesc(ObjectBoxEmbedding_.photoId)
}.count()
}
fun getEmbeddingByPhotoId(photoId: Long): ObjectBoxEmbedding? {
return embeddingBox
.query { equal(ObjectBoxEmbedding_.photoId, photoId) }
.findFirst()
}
fun getAllByPhotoIds(photoIds: LongArray): List<ObjectBoxEmbedding> {
return embeddingBox.query {
`in`(ObjectBoxEmbedding_.photoId, photoIds)
orderDesc(ObjectBoxEmbedding_.photoId)
}.find()
}
// 获取总数
fun getTotalCount(): Long {
return embeddingBox.count()
}
// 根据相册ID获取嵌入向量(精确匹配)
fun getAllByAlbumId(albumId: Long): List<ObjectBoxEmbedding> {
return embeddingBox.query {
equal(ObjectBoxEmbedding_.albumId, albumId)
orderDesc(ObjectBoxEmbedding_.photoId)
}.find()
}
// 根据相册ID列表获取嵌入向量
fun getByAlbumIdList(albumIds: List<Long>): List<ObjectBoxEmbedding> {
return embeddingBox.query {
`in`(ObjectBoxEmbedding_.albumId, albumIds.toLongArray())
orderDesc(ObjectBoxEmbedding_.photoId)
}.find()
}
// 根据相册ID列表分页获取嵌入向量
fun getByAlbumIdList(albumIds: List<Long>, limit: Int, offset: Int): List<ObjectBoxEmbedding> {
return embeddingBox.query {
`in`(ObjectBoxEmbedding_.albumId, albumIds.toLongArray())
orderDesc(ObjectBoxEmbedding_.photoId)
}.find(offset.toLong(), limit.toLong())
}
// 根据指定相册ID删除嵌入向量
fun removeByAlbumId(albumId: Long) {
embeddingBox.query {
equal(ObjectBoxEmbedding_.albumId, albumId)
}.remove()
}
// 根据照片ID列表批量删除嵌入向量(用于增量更新)
fun removeByPhotoIds(photoIds: LongArray) {
if (photoIds.isEmpty()) return
embeddingBox.query {
`in`(ObjectBoxEmbedding_.photoId, photoIds)
}.remove()
}
// 仅查询指定相册下已索引的照片ID列表(高效,不加载向量数据)
fun getPhotoIdsByAlbumId(albumId: Long): List<Long> {
return embeddingBox.query {
equal(ObjectBoxEmbedding_.albumId, albumId)
}.property(ObjectBoxEmbedding_.photoId).find()
}
// 批量更新或插入嵌入向量
fun upsertAll(embeddings: List<ObjectBoxEmbedding>) {
embeddingBox.put(embeddings)
}
// 删除单个嵌入向量
fun delete(embedding: ObjectBoxEmbedding) {
embeddingBox.remove(embedding)
}
// 批量删除嵌入向量
fun deleteAll(embeddings: List<ObjectBoxEmbedding>) {
embeddingBox.remove(embeddings)
}
fun searchNearestVectors(
queryVector: FloatArray,
topK: Int = 10,
similarityThreshold: Float = 0.7f,
albumIds: List<Long>? = null
): List<ObjectWithScore<ObjectBoxEmbedding>> {
val query =
embeddingBox
.query()
.nearestNeighbors(ObjectBoxEmbedding_.data, queryVector, topK)
.build()
val results = query.findWithScores().filter { result ->
val cosineSimilarity = 1.0 - result.score
cosineSimilarity > similarityThreshold
}
results.forEachIndexed { index, result ->
Timber.d("Result $index:")
Timber.d("Photo ID: ${result.get().photoId}")
Timber.d("Score: ${result.score}")
Timber.d("Cosine Similarity: ${calculateSimilarity(queryVector, result.get().data)}")
}
return results
}
fun searchNearestVectors2(
queryVector: FloatArray,
topK: Int = 10,
similarityThreshold: Float = 0.95f,
albumIds: List<Long>? = null
): List<ObjectWithScore<ObjectBoxEmbedding>> {
val query =
embeddingBox
.query()
.nearestNeighbors(ObjectBoxEmbedding_.data, queryVector, topK)
.build()
val results = query.findWithScores()
.filter { result ->
val cosineSimilarity = 1.0 - result.score
Timber.d("Photo ID: ${result.get().photoId}")
Timber.d("Score: ${result.score}")
Timber.d("Cosine Similarity: $cosineSimilarity")
Timber.d("Similarity Condition: ${cosineSimilarity >= similarityThreshold}")
cosineSimilarity >= similarityThreshold
}
Timber.d("Filtered Results Count: ${results.size}")
return results
}
}