skip to Main Content

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:

  1. Airline Name
  2. Search item (bag, baggage, etc.) – for now we’re working with baggage
  3. Specific search item (checked bag, carry-on bag, etc.)
  4. If airline name is in the search query sent the DB named AirlineInfo
  5. Based on search item enter that collection (for now it’s just BaggageInfo)
  6. 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)
  7. 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


  1. 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 the airlineName is "United Airlines" and where the checkedBagWeight 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 be db.BaggageInfo (assuming BaggageInfo is the name of your collection).

    And check that the field names airlineName and checkedBagWeight 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 and checkedBagWeight 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:

      db.BaggageInfo.find({'airlineName': 'United Airlines', 'checkedBagWeight': {'$exists': True, '$ne': None}})
      
    • Temporarily modify your code to execute a simplified query to see if it returns the expected result. For example:

      result = baggage_info_collection.find_one({"airlineName": "United Airlines"})
      

    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 is carryOnBagSize. That discrepancy will cause the query to fail when searching for carry-on bag information. It should be corrected to "carry-on": "carryOnBagSize" in the search_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, assuming checkedBagSize is the correct field name in the MongoDB document.

    The corrected search_params dictionary based on the observations would be:

    search_params = {
        "checked": "checkedBagSize",
        "carry-on": "carryOnBagSize",
        "weight": "checkedBagWeight",
    }
    
    Login or Signup to reply.
  2. you should use NLP to extract information from given query statement . he is example code that will help you to use NLP

    import nltk
    from nltk.tokenize import word_tokenize
    from nltk.tag import pos_tag
    
    def get_question_type(text):
      tokens = word_tokenize(text)
      tagged_tokens = pos_tag(tokens)
      question_type = None
    
      for token, tag in tagged_tokens:
        if tag == "NN" or tag == "NNP":
          question_type = "airline"
          break
        elif tag == "VB" or tag == "VBD":
          question_type = "action"
          break
        elif tag == "JJ" or tag == "JJR" or tag == "JJS":
          question_type = "adjective"
          break
        elif tag == "WRB":
          question_type = "adverb"
          break
        elif token == "weight":
          question_type = "weight"
    
      return question_type
    
    def get_airline_name(text):
      tokens = word_tokenize(text)
      for token in tokens:
        if token.isalpha() and len(token) > 2:
          return token
    
    def get_weight(text):
      tokens = word_tokenize(text)
      for token in tokens:
        if token.isdigit():
          return token
    
    def main():
      text = "I flew United Airlines from San Francisco to Los Angeles. What is the baggage allowance for my flight?"
      question_type = get_question_type(text)
      airline_name = get_airline_name(text)
      weight = get_weight(text)
    
      if question_type == "weight":
        if airline_name == "United Airlines":
          print("The baggage allowance for United Airlines is 50 pounds per passenger.")
        else:
          print("The baggage allowance for {} is {} pounds per passenger.".format(airline_name, weight))
    
    if __name__ == "__main__":
      main()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search