^processed_query
)
},
- where: not is_nil(u.nickname)
+ where:
+ fragment(
+ """
+ (setweight(to_tsvector('simple', regexp_replace(?, '\\W', ' ', 'g')), 'A') ||
+ setweight(to_tsvector('simple', regexp_replace(coalesce(?, ''), '\\W', ' ', 'g')), 'B')) @@ to_tsquery('simple', ?)
+ """,
+ u.nickname,
+ u.name,
+ ^processed_query
+ )
)
end
u.name
)
},
- where: not is_nil(u.nickname)
+ where: fragment("trim(? || ' ' || coalesce(?, '')) % ?", u.nickname, u.name, ^query)
)
end
use Ecto.Migration
def up do
- drop_if_exists index(:users, [], name: :users_trigram_index)
- create index(:users, ["(trim(nickname || ' ' || name)) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
+ drop_if_exists(index(:users, [], name: :users_trigram_index))
+
+ create(
+ index(:users, ["(trim(nickname || ' ' || coalesce(name, ''))) gist_trgm_ops"],
+ name: :users_trigram_index,
+ using: :gist
+ )
+ )
end
def down do
- drop_if_exists index(:users, [], name: :users_trigram_index)
- create index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
+ drop_if_exists(index(:users, [], name: :users_trigram_index))
+
+ create(
+ index(:users, ["(nickname || name) gist_trgm_ops"], name: :users_trigram_index, using: :gist)
+ )
end
end