Skip to content

Commit

Permalink
Search hits in answer bodies shouldn't result in multiple matches.
Browse files Browse the repository at this point in the history
- Simplify query by concatenating fields to a search body.
  • Loading branch information
magnusp committed Feb 20, 2020
1 parent 9c295bc commit e5ec99c
Showing 1 changed file with 5 additions and 13 deletions.
18 changes: 5 additions & 13 deletions impl/src/main/java/dao/QuestionDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public interface QuestionDao {
@Query(
value =
"WITH cte AS ( " +
"SELECT DISTINCT " +
"SELECT " +
"question.id, " +
"answer_accepted, " +
"question.question, " +
Expand All @@ -260,15 +260,15 @@ public interface QuestionDao {
"(SELECT COALESCE(SUM(question_vote.value), 0) FROM question_vote WHERE question_vote.question_id = question.id) AS votes, " +
"(SELECT COALESCE(jsonb_agg(tag ORDER BY label), '[]') FROM question_tag RIGHT JOIN tag ON question_tag.tag_id = tag.id WHERE question_tag.question_id = question.id) AS tags, " +
"ARRAY(SELECT tag.label FROM question_tag RIGHT JOIN tag ON question_tag.tag_id = tag.id WHERE question_tag.question_id = question.id ORDER BY tag.label) AS tag_labels, " +
"answer.answer " +
"COALESCE(question.title, '') || ' ' || COALESCE(question.question, '') || ' ' || COALESCE(answer.answer, '') AS search_body " +
"FROM " +
"question " +
"INNER JOIN " +
"\"user\" on \"user\".id = question.user_id " +
"LEFT JOIN " +
"answer on answer.question_id = question.id " +
") " +
"SELECT " +
"SELECT DISTINCT " +
"cte.id, " +
"cte.answer_accepted, " +
"cte.title, " +
Expand All @@ -287,19 +287,11 @@ public interface QuestionDao {
") " +
"OR " +
"( " +
":questionSearchOptions.contentSearch != '' AND '{}' = :questionSearchOptions.tags AND ( " +
"title ILIKE ('%' || :questionSearchOptions.contentSearch || '%') OR " +
"question ILIKE ('%' || :questionSearchOptions.contentSearch || '%') OR " +
"answer ILIKE ('%' || :questionSearchOptions.contentSearch || '%') " +
") " +
":questionSearchOptions.contentSearch != '' AND '{}' = :questionSearchOptions.tags AND search_body ILIKE ('%' || :questionSearchOptions.contentSearch || '%')" +
") " +
"OR " +
"( " +
":questionSearchOptions.contentSearch != '' AND tag_labels @> :questionSearchOptions.tags AND ( " +
"title ILIKE ('%' || :questionSearchOptions.contentSearch || '%') OR " +
"question ILIKE ('%' || :questionSearchOptions.contentSearch || '%') OR " +
"answer ILIKE ('%' || :questionSearchOptions.contentSearch || '%') " +
") " +
":questionSearchOptions.contentSearch != '' AND tag_labels @> :questionSearchOptions.tags AND search_body ILIKE ('%' || :questionSearchOptions.contentSearch || '%')" +
") " +
"ORDER BY " +
"votes desc, " +
Expand Down

0 comments on commit e5ec99c

Please sign in to comment.