0

I have a blog search function on my website.

This is my search input: children lazy

I have a column in my SQLite database that is called: Children is getting lazy

I use the codes(Python) below to query the data:

many_posts0 = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())

However using the codes above, I cannot shows the result of the column "Children is getting lazy" with the search input "children lazy". If I make my search result as "children is", the column's data will show. I wonder if my problem is because of "ilike" in my query codes.

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Upchanges
  • 310
  • 2
  • 14

1 Answers1

1

split the sraach string by white space and join with '%'

# split words and join with "%"
search_string = "%" + "%".join(search_data.split()) + "%"
many_posts0 = BlogPost.query.filter(or_((BlogPost.problem_name.ilike(search_string)),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())
gelonida
  • 5,327
  • 2
  • 23
  • 41
  • Hi @gelonida! Thank you so much for helping me multiple times! I would greatly appreciate if you could help me with my new problems: https://stackoverflow.com/q/62373393/13097721 (And please don't report me if this annoys you... Just tell me and I will stop doing this.) – Upchanges Jun 14 '20 at 13:51
  • Fine with me Upchanges . SO doesn't have any official ways to allow private messages between users if they want to (at least none, that I'm aware of). So until I can identify a 'cleaner' more official solution just continue doing this. If I don't react to a new suggested question, then assume it's either because I'm busy or because I don't know the answer. If I want you to stop I will tell you explicitly. Happy coding. **To all:** If anybody else knows a better way to handle this, then please tell me and upchanges, so that we don't pollute the comments channel. I'm open to suggestions. – gelonida Jun 14 '20 at 20:25
  • Thank you @gelonida!! – Upchanges Jun 14 '20 at 23:36