Documents
django-semantic-search
was designed to mimic some of the patterns used in popular Django libraries, such as
django-import-export
to reduce the learning curve for new users.
The base concept of the library is a Document
subclass that represents a single searchable entity. The library
provides a way to define a document class for a selected model. The document class is responsible for converting
the model instances into the vector representation and storing them in the vector search engine, as well as for
performing the search queries.
Documents
django_semantic_search.Document
Bases: ABC
, Generic[T]
Base class for all the documents. There is a one-to-one mapping between the document subclass and the model class, to configure how a specific model instances should be converted to a document.
Usage:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
from django_semantic_search import Document, VectorIndex
from django_semantic_search.decorators import register_document
@register_document
class ProductDocument(Document):
class Meta:
model = Product
indexes = [
VectorIndex("name"),
VectorIndex("description"),
]
django-semantic-search
will automatically handle all the configuration in the backend. The register_document
decorator will register the model signals to update the documents in the vector store when the model is updated
or deleted. As a user you don't have to manually call the save
or delete
methods on the document instances.
Search example:
from django.http import JsonResponse
from products.documents import ProductDocument
def my_view(request):
query = "this is a query"
results = ProductDocument.objects.find(name=query)
return JsonResponse(
{
"results": list(name_results.values())
}
)
The find
method on the objects
attribute of the document class will return the queryset of the model instances
that are similar to the query. The search is performed using the selected vector index passed as a keyword argument
to the find
method. In our case, we are searching for the query in the name
field of the Product
model. If we
want to search in the description
field, we would call ProductDocument.objects.find(description=query)
.
Source code in src/django_semantic_search/documents.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
|