1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-15 18:57:04 +00:00

Sort search results by relevance

This commit is contained in:
Jebbs 2019-02-12 14:20:07 +08:00 committed by fguillot
parent ed6ae7e0d2
commit 267b706544
6 changed files with 11 additions and 7 deletions

View file

@ -50,7 +50,7 @@ func (s *Storage) UpdateEntryContent(entry *model.Entry) error {
query := `
UPDATE entries
SET document_vectors = to_tsvector(substring(title || ' ' || coalesce(content, '') for 1000000))
SET document_vectors = setweight(to_tsvector(substring(coalesce(title, '') for 1000000)), 'A') || setweight(to_tsvector(substring(coalesce(content, '') for 1000000)), 'B')
WHERE id=$1 AND user_id=$2
`
_, err = tx.Exec(query, entry.ID, entry.UserID)
@ -68,7 +68,7 @@ func (s *Storage) createEntry(entry *model.Entry) error {
INSERT INTO entries
(title, hash, url, comments_url, published_at, content, author, user_id, feed_id, document_vectors)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, to_tsvector(substring($1 || ' ' || coalesce($6, '') for 1000000)))
($1, $2, $3, $4, $5, $6, $7, $8, $9, setweight(to_tsvector(substring(coalesce($1, '') for 1000000)), 'A') || setweight(to_tsvector(substring(coalesce($6, '') for 1000000)), 'B'))
RETURNING id, status
`
err := s.db.QueryRow(
@ -107,7 +107,7 @@ func (s *Storage) updateEntry(entry *model.Entry) error {
query := `
UPDATE entries SET
title=$1, url=$2, comments_url=$3, content=$4, author=$5,
document_vectors=to_tsvector(substring($1 || ' ' || coalesce($4, '') for 1000000))
document_vectors = setweight(to_tsvector(substring(coalesce($1, '') for 1000000)), 'A') || setweight(to_tsvector(substring(coalesce($4, '') for 1000000)), 'B')
WHERE user_id=$6 AND feed_id=$7 AND hash=$8
RETURNING id
`

View file

@ -33,6 +33,9 @@ func (e *EntryQueryBuilder) WithSearchQuery(query string) *EntryQueryBuilder {
e.conditions = append(e.conditions, fmt.Sprintf("e.document_vectors @@ plainto_tsquery($%d)", len(e.args)+1))
e.args = append(e.args, query)
}
// ordered by relevance, can be overrode
e.WithOrder(fmt.Sprintf("ts_rank(document_vectors, plainto_tsquery('%s'))", query))
e.WithDirection("DESC")
return e
}
@ -315,7 +318,7 @@ func (e *EntryQueryBuilder) buildSorting() string {
var parts []string
if e.order != "" {
parts = append(parts, fmt.Sprintf(`ORDER BY "%s"`, e.order))
parts = append(parts, fmt.Sprintf(`ORDER BY %s`, e.order))
}
if e.direction != "" {