I am attempting to build an autocomplete search using Flask, MongoDB, and jQuery Autocomplete. I want to show a list of addresses once a user starts to type them in a search bar. For example, if a user types 123 F
, I want to show the 5 closely matched addresses based on that string. Once you select an address from the list, I'd like to show some attributes associated with that address that are part of the same CSV table.
I have a simple CSV file of addresses that is structured like this:
ID ADDRESS
1 123 Fakevill St
2 35 Lakeshore Rd
.. and so on
I first imported this CSV file into MongoDB using the following command:
mongoimport --db myDB --collection myAddresses --file "/path/to/file" --type csv --headerline
While I haven't begun working on the client-side yet, I have run into some issues on the server-side. My server-side regex query does not return any results. Here is how I have set it up in Flask:
@app.route('/getInfoByAddress', methods=['GET'])
def getInfoByAddress():
searchedAddress = request.args.get("Address")
addressCollection = myDB["addresses"]
addressJSON = []
regex = "/^" + searchedAddress + "/"
for address in addressCollection.find({'Address': re.compile(regex, re.IGNORECASE)},{"Address":1, "ID":1, "_id":0}):
addressJSON.append({"Address":address["Address"]})
return jsonify({"Addresses":addressJSON})
I'm using a combination of regex
and re
to deal with autocomplete and type cases. Currently when I go to my browser to test the query, myapp.dev:5000/getInfoByAddress?Address=123 F
, it returns an empty JSON:
{
"Addresses": []
}
When I do print regex
, I can see the proper string (/^123 F/
) being sent to the database in my command line window, but unfortunately I am not seeing any results.