Skip to content

Commit

Permalink
link service: add FTS by postgres
Browse files Browse the repository at this point in the history
Signed-off-by: Login Victor <[email protected]>
  • Loading branch information
batazor committed Aug 29, 2021
1 parent 59dd231 commit d1d0bec
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 12 deletions.
25 changes: 25 additions & 0 deletions docs/shortlink.postman_collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,31 @@
}
},
"response": []
},
{
"name": "Get links {Search}",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{API_GATEWAY.HTTP_API}}/api/cqrs/links?search=\"язык <-> программирования\"",
"host": [
"{{API_GATEWAY.HTTP_API}}"
],
"path": [
"api",
"cqrs",
"links"
],
"query": [
{
"key": "search",
"value": "\"язык <-> программирования\""
}
]
}
},
"response": []
}
]
},
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/db/postgres/migrations/000016_fts.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop index concurrently shortlink.idx_fts_link_view;
10 changes: 10 additions & 0 deletions internal/pkg/db/postgres/migrations/000016_fts.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE OR REPLACE FUNCTION make_tsvector_link_view(description TEXT, keywords TEXT)
RETURNS tsvector AS $$
BEGIN
RETURN (setweight(to_tsvector('simple', keywords),'A') ||
setweight(to_tsvector('simple', description), 'B'));
END
$$ LANGUAGE 'plpgsql' IMMUTABLE;

CREATE INDEX idx_fts_link_view ON shortlink.link_view
USING gin(make_tsvector_link_view(meta_description, meta_keywords));
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package postgres

import (
"context"
"database/sql"
"fmt"

"github.com/Masterminds/squirrel"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/jackc/pgx/v4/pgxpool"

"github.com/batazor/shortlink/internal/pkg/db"
Expand Down Expand Up @@ -65,8 +67,10 @@ func (s *Store) Get(ctx context.Context, id string) (*v12.LinkView, error) {
// List ...
func (s *Store) List(ctx context.Context, filter *query.Filter) (*v12.LinksView, error) {
// query builder
links := psql.Select("url, hash, describe, created_at, updated_at").
From("shortlink.link_view").
links := psql.Select("hash, describe, ts_headline(meta_description, q, 'StartSel=<em>, StopSel=</em>') as meta_description, created_at, updated_at").
From(fmt.Sprintf(`shortlink.link_view, to_tsquery('%s') AS q`, *filter.Search.Contains)).
Where("make_tsvector_link_view(meta_keywords, meta_description) @@ q").
OrderBy("ts_rank(make_tsvector_link_view(meta_keywords, meta_description), q) DESC").
Limit(uint64(filter.Pagination.Limit)).
Offset(uint64(filter.Pagination.Page * filter.Pagination.Limit))
q, args, err := links.ToSql()
Expand All @@ -85,16 +89,16 @@ func (s *Store) List(ctx context.Context, filter *query.Filter) (*v12.LinksView,

for rows.Next() {
var result v12.LinkView
//var (
// created_ad sql.NullTime
// updated_at sql.NullTime
//)
//err = rows.Scan(&result.Url, &result.Hash, &result.Describe, &created_ad, &updated_at)
//if err != nil {
// return nil, &v1.NotFoundError{Link: &v1.Link{}, Err: fmt.Errorf("Not found links")}
//}
//result.CreatedAt = &timestamp.Timestamp{Seconds: int64(created_ad.Time.Second()), Nanos: int32(created_ad.Time.Nanosecond())}
//result.UpdatedAt = &timestamp.Timestamp{Seconds: int64(updated_at.Time.Second()), Nanos: int32(updated_at.Time.Nanosecond())}
var (
created_ad sql.NullTime
updated_at sql.NullTime
)
err = rows.Scan(&result.Hash, &result.Describe, &result.MetaDescription, &created_ad, &updated_at)
if err != nil {
return nil, &v1.NotFoundError{Link: &v1.Link{}, Err: fmt.Errorf("Not found links")}
}
result.CreatedAt = &timestamp.Timestamp{Seconds: int64(created_ad.Time.Second()), Nanos: int32(created_ad.Time.Nanosecond())}
result.UpdatedAt = &timestamp.Timestamp{Seconds: int64(updated_at.Time.Second()), Nanos: int32(updated_at.Time.Nanosecond())}

response.Links = append(response.Links, &result)
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d1d0bec

Please sign in to comment.