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.
2
Answers
Regarding the MongoDB query issue, based on the query log you shared, the constructed query seems to be in the correct format. The query
{'airlineName': 'United Airlines', 'checkedBagWeight': {'$exists': True, '$ne': None}}
should indeed look for a document where theairlineName
is "United Airlines
" and where thecheckedBagWeight
field exists and is not null.Make sure first that you are querying the correct collection.
In your code, you are trying to access the collection via
db.AirlineInfo.BaggageInfo
, but based on the structure you provided it seems like it should just bedb.BaggageInfo
(assumingBaggageInfo
is the name of your collection).And check that the field names
airlineName
andcheckedBagWeight
match exactly with the field names in your MongoDB documents, including the case (upper/lower).Also double-check there are proper indexes on the
airlineName
andcheckedBagWeight
fields in your MongoDB collection to improve query performance.For testing:
Try executing the query manually using a MongoDB client or shell to make sure the query indeed returns the expected result.
The query based on the information provided should be:
Temporarily modify your code to execute a simplified query to see if it returns the expected result. For example:
That being said, djmonki‘s comment remains valid: the discrepancy in field names would certainly cause the queries to not match any documents, resulting in the message "
No baggage information found.
"The
search_params
dictionary has"carry-on": "carryOn"
, but the field in the MongoDB document iscarryOnBagSize
. That discrepancy will cause the query to fail when searching for carry-on bag information. It should be corrected to"carry-on": "carryOnBagSize"
in thesearch_params
dictionary.The
search_params
dictionary maps"checked": "checkedBagWeight"
and"weight": "checkedBagWeight"
. If the intention is to map a query for checked bag size, then indeed"checked": "checkedBagSize"
should be the correct mapping, assumingcheckedBagSize
is the correct field name in the MongoDB document.The corrected
search_params
dictionary based on the observations would be:you should use NLP to extract information from given query statement . he is example code that will help you to use NLP