I am using flask, react on the frontend (which works and sends the query to the backend), and doing this all for the first time. The end goal here is to have the user entered query broken down so that it finds what the user wants and returns that value from the MongoDB document. So, for example, if a user wants to know the weight of a bag the code searches by checkedBagWeight
and it will go into the document where the airline matches and return the value for checkedBagWeight
My issue is no matter what I do I can never get it to return the data t always says nothing is found
How it works: User query: United Airlines checked bag weight query sent to backend backend deconstructs the query Checks for the following:
- Airline Name
- Search item (bag, baggage, etc.) - for now we're working with baggage
- Specific search item (checked bag, carry-on bag, etc.)
- If airline name is in the search query sent the DB named AirlineInfo
- Based on search item enter that collection (for now it's just BaggageInfo)
- Search through the collection for the airline name United Airlines (use the airlines dictionary so if a user enters United it searches with Unitec Airlines)
- Search that document in the collection that matches the airline for the specific information the query wants (if user says United checked bag weight it returns that item in the docment)
Example Document:
_id: 651d7a78cf3af11191af0e7a
airlineName: "United Airlines"
carryOnBagSize: "22 x 14 x 9"
checkedBagSize: "62 linear inches (L + W + H)"
checkedBagWeight: "50 lbs"
policy: "Placeholder text"
So as of now when I enter a search query here is what my backend returns to me:
Query received: United Airlines checked bag weight
Query: {'airlineName': 'United Airlines', 'checkedBagWeight': {'$exists': True, '$ne': None}}
Received search query: United Airlines checked bag weight
Response: No baggage information found.
Here are my two code files:
server.py:
from flask import Flask, request, jsonify
from flask_cors import CORS
from pymongo.mongo_client import MongoClient
from pymongo.errors import ConnectionFailure
from dotenv import load_dotenv
import os
from search import handle_search
load_dotenv()
uri = os.getenv('MONGO_URI')
client = MongoClient(uri)
app = Flask(__name__)
CORS(app)
try:
client.admin.command('ping')
print("Connected to MongoDB!")
except ConnectionFailure as e:
print("Failed to connect to MongoDB because:", e)
db = client.AirlineInfo
@app.route('/ping', methods=['GET'])
def ping():
return jsonify({'message': 'Pong!'})
@app.route('/search', methods=['POST'])
def search():
data = request.json
search_query = data.get('query')
print('Data:', data)
print('Search Query:', search_query)
response = handle_search(search_query, db)
return response
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
Search.py:
# search.py
from flask import jsonify
from pymongo import MongoClient
# Define search parameters
search_params = {
"checked": "checkedBagWeight",
"carry-on": "carryOn",
"weight": "checkedBagWeight",
}
# Define airline mappings
airlines = {
"united": "United Airlines",
}
def handle_search(query, db):
query_words = query.lower().split()
response_message = "No relevant information found."
baggage_info_collection = db.AirlineInfo.BaggageInfo
# Extract airline name from the query
airline_name = None
for word in query_words:
if word in airlines:
airline_name = airlines[word]
break
search_param = None
for word in query_words:
if word in search_params:
search_param = search_params[word]
break
if airline_name and search_param:
result = search_baggage_info(airline_name, search_param, baggage_info_collection)
if result:
response_message = result
print("Received search query:", query)
print("Response:", response_message)
return jsonify({'message': response_message})
def search_baggage_info(airline_name, search_param, baggage_info_collection):
try:
query = {
"airlineName": airline_name,
search_param: {"$exists": True, "$ne": None}
}
print("Query:", query)
result = baggage_info_collection.find_one(query)
if result and search_param in result:
return result[search_param]
return "No baggage information found."
except Exception as e:
print(f"Error occurred in search_baggage_info: {str(e)}")
return "Error occurred while searching for baggage information."
I've tried different ways to structure the query and I am just lost I know it's in the right collection and DB I just don't know why it won't return the value for the search_param
which is set to be the exact name of how it is in my mongo DB document.
from Python Flask and MongoDB custom search
No comments:
Post a Comment