Exclude vectors from response
This commit is contained in:
@@ -22,7 +22,7 @@ load_dotenv(dotenv_path=env_path)
|
||||
sys.path.append(str(project_root / "service" / "src" / "main"))
|
||||
from inse.config import settings
|
||||
|
||||
ELASTIC_URL = settings.elasticsearch_url.rstrip("/") if settings.elasticsearch_url else "https://project.nes.uni-due.de/elastic"
|
||||
ELASTIC_URL = settings.elasticsearch_url
|
||||
API_KEY = settings.elasticsearch_api_key
|
||||
MODEL_NAME = "paraphrase-multilingual-mpnet-base-v2"
|
||||
|
||||
@@ -58,14 +58,14 @@ def check_and_update_mapping():
|
||||
|
||||
with httpx.Client(timeout=30.0) as client:
|
||||
# Check if index exists
|
||||
exists_resp = client.head(f"{ELASTIC_URL}/topicvectors", headers=headers)
|
||||
exists_resp = client.head(f"{ELASTIC_URL}topicvectors", headers=headers)
|
||||
if exists_resp.status_code == 404:
|
||||
print("Index 'topicvectors' does not exist. Creating index with mapping...")
|
||||
create_payload = {
|
||||
"mappings": mapping_payload
|
||||
}
|
||||
resp = client.put(
|
||||
f"{ELASTIC_URL}/topicvectors",
|
||||
f"{ELASTIC_URL}topicvectors",
|
||||
json=create_payload,
|
||||
headers=headers
|
||||
)
|
||||
@@ -77,7 +77,7 @@ def check_and_update_mapping():
|
||||
else:
|
||||
print("Index 'topicvectors' exists. Updating mapping...")
|
||||
resp = client.put(
|
||||
f"{ELASTIC_URL}/topicvectors/_mapping",
|
||||
f"{ELASTIC_URL}topicvectors/_mapping",
|
||||
json=mapping_payload,
|
||||
headers=headers
|
||||
)
|
||||
@@ -101,7 +101,7 @@ def fetch_all_topics():
|
||||
"size": size
|
||||
}
|
||||
resp = client.post(
|
||||
f"{ELASTIC_URL}/openalex-topics/_search",
|
||||
f"{ELASTIC_URL}openalex-topics/_search",
|
||||
json=payload,
|
||||
headers=headers
|
||||
)
|
||||
@@ -148,7 +148,7 @@ def bulk_index_topics(topics, vectors):
|
||||
|
||||
payload = "\n".join(bulk_lines) + "\n"
|
||||
resp = client.post(
|
||||
f"{ELASTIC_URL}/_bulk",
|
||||
f"{ELASTIC_URL}_bulk",
|
||||
content=payload,
|
||||
headers={"Authorization": f"ApiKey {API_KEY}", "Content-Type": "application/x-ndjson"}
|
||||
)
|
||||
|
||||
@@ -286,7 +286,7 @@ async def vector_search_topics(
|
||||
detail=f"Failed to encode query text: {exc}"
|
||||
)
|
||||
|
||||
# 2. Query Elasticsearch
|
||||
# 2. Query Elasticsearch using vector similarity search
|
||||
|
||||
body = {
|
||||
"knn": {
|
||||
@@ -294,6 +294,9 @@ async def vector_search_topics(
|
||||
"query_vector": query_vector,
|
||||
"k": limit,
|
||||
"num_candidates": 100
|
||||
},
|
||||
"_source": {
|
||||
"excludes": ["vector"] #remove vectors
|
||||
}
|
||||
}
|
||||
|
||||
@@ -314,13 +317,7 @@ async def vector_search_topics(
|
||||
hits = raw_results.get("hits", {}).get("hits", [])
|
||||
total = len(hits)
|
||||
|
||||
results = []
|
||||
for hit in hits:
|
||||
source = hit.get("_source", {})
|
||||
# Remove vector field (dont send to frontend)
|
||||
if "vector" in source:
|
||||
del source["vector"]
|
||||
results.append(source)
|
||||
results = [hit.get("_source", {}) for hit in hits]
|
||||
|
||||
data = {
|
||||
"meta": {
|
||||
|
||||
Reference in New Issue
Block a user