datadabllp

handle both structured and unstructured data

Jul 19th, 2024
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. from elasticsearch import Elasticsearch
  2. from elasticsearch.helpers import bulk
  3.  
  4. es = Elasticsearch()
  5.  
  6. def index_documents(documents):
  7.     actions = [
  8.         {
  9.             "_index": "hybrid_search",
  10.             "_id": doc["id"],
  11.             "_source": {
  12.                 "content": doc["content"],
  13.                 "vector": doc["embedding"]
  14.             }
  15.         }
  16.         for doc in documents
  17.     ]
  18.     bulk(es, actions)
  19.  
  20. # Usage
  21. docs = [
  22.     {"id": 1, "content": "Java performance tuning", "embedding": [0.1, 0.2, ...]},
  23.     {"id": 2, "content": "Optimizing database queries", "embedding": [0.3, 0.4, ...]}
  24. ]
  25. index_documents(docs)
  26.  
Advertisement
Add Comment
Please, Sign In to add comment