Use indexes with dynamic where clause

Hi all!

I’m faced a requirement, to search in the table with dynamic criteria. Like (pseudo code):

if (firstname) {
  where += "firstname like %${firstname}%"
if (is18only) {
  where += "birthday <= 15.12.2001"
if (state) {
  where += "'data' @> '[{"state":"${state}"}]' // JSONB

Is there any way to define reasonable indexes?

Thanks in advance!

Hi Anton,

I’m not sure how to interpret your question - you could definitely create indexes on firstname and birthday, and an inverted index on your jsonb column.

Hey @tim-o,

thank you for your response! You are right, exactly this example is working. I look where the error in a more complex query come from.