I have created an MongoDB atlas vector index LocationSemanticSearch
based on location.
my api file
import base64
# import io
import os
from flask import Blueprint, jsonify, request
# from PIL import Image
from pymongo.mongo_client import MongoClient
from pymongo.server_api import ServerApi
import hf
from model import Property, User
server_bp = Blueprint("server", __name__)
password = os.environ.get("MONGODB_PWD")
uri = f"mongodb+srv://hacker:{password}@cluster0.akxrbxh.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0"
client = MongoClient(uri, server_api=ServerApi('1'))
db = client['Apnahood']
users_collection = db['users']
house_collection = db['houses']
@server_bp.route('/register', methods=['POST'])
def register_user():
data = request.get_json()
username = data['username']
password = data.get('password')
email = data.get('email')
cipher_text = hash(password)
if not username or not password or not email:
return jsonify({'error': 'Full credentials not given!'}), 400
if users_collection.find_one({'username': username}):
return jsonify({'error': 'Username already exists'}), 409
if users_collection.find_one({'email': email}):
return jsonify({'error': 'Email already exists'}), 409
try:
user = User(username, cipher_text, email)
users_collection.insert_one(user.to_dict())
return jsonify({'message': 'User registered successfully'}), 200
except Exception as e:
return jsonify({'error': str(e)}), 500
@server_bp.route('/login', methods=['POST'])
def login_user():
data = request.get_json()
username = data.get('username')
password = data.get('password')
cipher_text = hash(password)
if not username or not password:
return jsonify({'error': 'Username and password are required'}), 400
user = users_collection.find_one({'username': username})
if not user:
return jsonify({'error': 'User not found!'}), 404
elif user['password'] == cipher_text:
return jsonify({'message': 'Login successful'}), 200
else:
return jsonify({'error': 'Invalid username or password'}), 401
@server_bp.route('/add-property', methods=['POST'])
def setProperties():
try:
data = request.form.to_dict()
if house_collection.find_one({"name": data['name']}):
return jsonify({'error': 'Property already exists!'}), 409
image = request.files.get('image')
if not image:
return jsonify({'error': 'Image file is required'}), 400
image_data = image.read()
encoded_image = base64.b64encode(image_data).decode('utf-8')
property = Property(name=data.get('name'),
owner=data.get('owner'),
rating=int(data['rating']),
cost=float(data['cost']),
address=data.get('address'),
thumbnail=encoded_image)
property_dict = property.to_dict()
result = house_collection.insert_one(property_dict)
ref = result.inserted_id
pipeline = [{
'$match': {
'username': data['owner']
}
}, {
'$set': {
'properties': {
'$concatArrays': ['$properties', [ref]]
},
'islandlord': True
}
}, {
'$merge': {
'into': 'users',
'on': '_id',
'whenMatched': 'merge',
'whenNotMatched': 'fail'
}
}]
users_collection.aggregate(pipeline)
return jsonify({'objectId': str(ref)}), 201
except Exception as e:
return jsonify({"error": str(e)}), 500
@server_bp.route('/get-properties', methods=['POST'])
def getProperties():
data = request.get_json()
q = data.get('query')
if not q:
return jsonify({'error': 'Location parameter is required'}), 400
try:
properties = house_collection.aggregate([{
"$vectorSearch": {
"queryVector": hf.generate_embedding(q),
"path": "addr_embedding_hf",
"limit": 8,
"index": "LocationSemanticSearch",
"numCandidates": 300,
}
}])
properties_list = []
for i, property in enumerate(properties):
del property["_id"]
del property["addr_embedding_hf"]
'''testing purpose'''
# if 'thumbnail' in property:
# image_data = property['thumbnail']
# image_stream = io.BytesIO(base64.b64decode(image_data))
# image = Image.open(image_stream)
# if image.mode != 'RGB':
# image = image.convert('RGB')
# image.save(f'output{i}.jpg')
properties_list.append(property)
return jsonify(properties_list), 200
except Exception as e:
return jsonify({"error": str(e)}), 500
searchindex jsonschema
{
"fields": [
{
"numDimensions": 384,
"path": "addr_embedding_hf",
"similarity": "euclidean",
"type": "vector"
}
]
}
But upon querying, it is returning all documnets in my collection instead of filtered ones…
How to resolve this issue?