melhoria pesquisa
This commit is contained in:
parent
322e6034bc
commit
93e08a0378
@ -12,31 +12,16 @@ namespace OpenCand.Repository
|
|||||||
|
|
||||||
public async Task<List<Candidato>> SearchCandidatosAsync(string query)
|
public async Task<List<Candidato>> SearchCandidatosAsync(string query)
|
||||||
{
|
{
|
||||||
using (var connection = new NpgsqlConnection(ConnectionString))
|
using var connection = new NpgsqlConnection(ConnectionString);
|
||||||
{
|
return (await connection.QueryAsync<Candidato>(@"
|
||||||
return (await connection.QueryAsync<Candidato>(@"
|
SELECT c.*,
|
||||||
SELECT *,
|
GREATEST(similarity(c.apelido, @q), similarity(c.nome, @q)) AS sim
|
||||||
CASE
|
FROM candidato c
|
||||||
WHEN lower(apelido) = lower(@query) THEN 0 -- apelido Exact match (case-insensitive)
|
WHERE c.apelido % @q
|
||||||
WHEN lower(apelido) LIKE lower(@query) || '%' THEN 1 -- apelido Starts with match (case-insensitive)
|
OR c.nome % @q
|
||||||
WHEN lower(apelido) LIKE '%' || lower(@query) THEN 2 -- apelido Contains anywhere match (case-insensitive)
|
ORDER BY sim DESC, length(c.nome) ASC
|
||||||
WHEN lower(nome) = lower(@query) THEN 0 -- nome Exact match (case-insensitive)
|
LIMIT 10;
|
||||||
WHEN lower(nome) LIKE lower(@query) || '%' THEN 1 -- nome Starts with match (case-insensitive)
|
", new { q = query })).AsList();
|
||||||
WHEN lower(nome) LIKE '%' || lower(@query) THEN 2 -- nome Contains anywhere match (case-insensitive)
|
|
||||||
WHEN cpf = @query THEN 0 -- cpf Exact match for CPF
|
|
||||||
WHEN cpf LIKE @query || '%' THEN 1 -- cpf Starts with match for CPF
|
|
||||||
WHEN cpf LIKE '%' || @query THEN 2 -- cpf Contains anywhere match for CPF
|
|
||||||
ELSE 3
|
|
||||||
END AS name_rank
|
|
||||||
FROM candidato
|
|
||||||
WHERE apelido ILIKE '%' || @query || '%' OR
|
|
||||||
nome ILIKE '%' || @query || '%' OR
|
|
||||||
cpf ILIKE '%' || @query || '%'
|
|
||||||
ORDER BY name_rank,
|
|
||||||
length(nome) ASC
|
|
||||||
LIMIT 10;",
|
|
||||||
new { query })).AsList();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Candidato?> GetCandidatoAsync(Guid idcandidato)
|
public async Task<Candidato?> GetCandidatoAsync(Guid idcandidato)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user