Posts: 21
Threads: 8
Joined: Jul 2020
Hello Coders,
I have almost solved with my little search bar, just I cannot manage to look for the word with case insensitive, if I write parmesan it finds it, all work normally, if I write ParMeSan it doesn't. Any Idea? I'm reading the documentation but cannot manage to do like it's written there, thank you!
1 2 3 4 5 6 7 |
@app .route( '/search' , methods = [ "POST" ])
def search():
if request.method = = "POST" :
data = request.form.to_dict()
keyword = str (data[ 'mysearch' ])
query = { "ingredients" : { "$regex" : keyword }}
return render_template( "search.html" , recipes = mongo.db.recipes.find(query))
|
Posts: 1,583
Threads: 3
Joined: Mar 2020
I don't see what's actually doing the lookup. But if render_template uses python regex engine, you can probably add a (?i) to the beginning of the regex.
1 2 3 |
>>> re.search( "Hi" , "what is this?" )
>>> re.search( "(?i)Hi" , "what is this?" )
<_sre.SRE_Match object ; span = ( 9 , 11 ), match = 'hi' >
|
Posts: 21
Threads: 8
Joined: Jul 2020
(Jul-19-2020, 07:57 PM)bowlofred Wrote: I don't see what's actually doing the lookup. But if render_template uses python regex engine, you can probably add a (?i) to the beginning of the regex.
1 2 3 |
>>> re.search( "Hi" , "what is this?" )
>>> re.search( "(?i)Hi" , "what is this?" )
<_sre.SRE_Match object ; span = ( 9 , 11 ), match = 'hi' >
|
I'm using a form to look for an ingredients, the webpage will show me a page with the result of all the recipes with that ingredients.
1 2 3 4 5 6 7 |
@app .route( '/search' , methods = [ "POST" ])
def search():
if request.method = = "POST" :
data = request.form.to_dict()
keyword = str (data[ 'mysearch' ])
query = { "ingredients" : { "$regex" : keyword }}
return render_template( "search.html" , recipes = mongo.db.recipes.find(query))
|
This code is in main.py, my code works, the only thing is that I would like it looks for case insensitive, for example if I write parmisan, it gives me many results, but if I write ParMiSan none, because it's looking in case sensitive mode. I just would like to know how to add the option to make it case insensitive.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Did you try what I said above? Add "(?i)" to the front of the regex.
Posts: 21
Threads: 8
Joined: Jul 2020
(Jul-19-2020, 08:26 PM)bowlofred Wrote: Did you try what I said above? Add "(?i)" to the front of the regex.
No because I don't know exactly where to put it sorry
Posts: 1,583
Threads: 3
Joined: Mar 2020
I'm assuming it's just keyword. So try instead of your line 5, something like:
1 |
keyword = "(?i)" + str (data[ 'mysearch' ])
|
Posts: 21
Threads: 8
Joined: Jul 2020
(Jul-19-2020, 08:31 PM)bowlofred Wrote: I'm assuming it's just keyword. So try instead of your line 5, something like:
1 |
keyword = "(?i)" + str (data[ 'mysearch' ])
|
no it doesn't work at all on this way, thank you for trying
Posts: 1,583
Threads: 3
Joined: Mar 2020
Can you give an example of what one of your $regex is set to? Looks like mongo can take the perl-style /$regex/i form.
Posts: 21
Threads: 8
Joined: Jul 2020
(Jul-19-2020, 08:53 PM)bowlofred Wrote: Can you give an example of what one of your $regex is set to? Looks like mongo can take the perl-style /$regex/i form.
I made it, after checking 2000 websites, none of them was giving me the answer, and the answer was actually the simplest. Just needed to understand the logic of the syntax:
1 |
query = { "ingredients" : { "$regex" : keyword, "$options" : "i" }}
|
Thank you!
|