Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from elasticsearch import Elasticsearch
- import numpy as np
- # Connect to Elasticsearch
- es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
- # Index a document with its vector embedding
- doc = {
- 'text': 'The new EU AI regulations focus on transparency and accountability.',
- 'vector': np.random.rand(384).tolist() # Simulated 384-dimensional embedding
- }
- es.index(index='documents', body=doc)
- # Perform a vector similarity search
- query_vector = np.random.rand(384).tolist() # Simulated query embedding
- search_body = {
- "query": {
- "script_score": {
- "query": {"match_all": {}},
- "script": {
- "source": "cosineSimilarity(params.query_vector, 'vector') + 1.0",
- "params": {"query_vector": query_vector}
- }
- }
- }
- }
- results = es.search(index='documents', body=search_body)
Advertisement
Add Comment
Please, Sign In to add comment