Browse Source
fix repository bugs and make text searching work more like it did in the past.
master
2.2.0
fix repository bugs and make text searching work more like it did in the past.
master
2.2.0
Gisle Aune
4 years ago
22 changed files with 271 additions and 93 deletions
-
22cmd/rpdata-server/main.go
-
61database/postgres/character.go
-
2database/postgres/db.go
-
27database/postgres/logs.go
-
9database/postgres/migrations/20210329190042_alter_log_add_ts_vector.sql
-
9database/postgres/migrations/20210329190250_create_index_log_search.sql
-
47database/postgres/posts.go
-
2database/postgres/queries/chapters.sql
-
8database/postgres/queries/characters.sql
-
2database/postgres/queries/counter.sql
-
20database/postgres/queries/logs.sql
-
13database/postgres/queries/posts.sql
-
6database/postgres/queries/stories.sql
-
2database/postgres/queries/tags.sql
-
10database/postgres/stories.go
-
56database/postgres/utils.go
-
23database/postgres/utils_test.go
-
2models/tag.go
-
18services/characters.go
-
10services/logs.go
-
2services/services.go
-
3services/stories.go
@ -0,0 +1,9 @@ |
|||
-- +goose Up |
|||
-- +goose StatementBegin |
|||
ALTER TABLE log ADD COLUMN "ts_vector" TSVECTOR; |
|||
-- +goose StatementEnd |
|||
|
|||
-- +goose Down |
|||
-- +goose StatementBegin |
|||
ALTER TABLE log DROP COLUMN "ts_vector"; |
|||
-- +goose StatementEnd |
@ -0,0 +1,9 @@ |
|||
-- +goose Up |
|||
-- +goose StatementBegin |
|||
CREATE INDEX log_index_search ON log USING GIN (ts_vector); |
|||
-- +goose StatementEnd |
|||
|
|||
-- +goose Down |
|||
-- +goose StatementBegin |
|||
DROP INDEX IF EXISTS log_index_search; |
|||
-- +goose StatementEnd |
@ -0,0 +1,56 @@ |
|||
package postgres |
|||
|
|||
import "strings" |
|||
|
|||
// TSQueryFromSearch generates a TS query from a typical search string.
|
|||
func TSQueryFromSearch(search string) string { |
|||
if strings.HasPrefix(search, "tsq:") { |
|||
return search[4:] |
|||
} |
|||
|
|||
tokens := strings.Split(search, " ") |
|||
inQuotes := false |
|||
result := "" |
|||
|
|||
for i, token := range tokens { |
|||
clearQuotes := false |
|||
startQuotes := false |
|||
|
|||
if strings.HasPrefix(token, "\"") { |
|||
token = token[1:] |
|||
startQuotes = true |
|||
} |
|||
if strings.HasSuffix(token, "\"") { |
|||
token = token[:len(token)-1] |
|||
clearQuotes = true |
|||
} |
|||
|
|||
if startQuotes { |
|||
if i > 0 { |
|||
result += " & " |
|||
} |
|||
|
|||
result += "(" |
|||
} else if inQuotes { |
|||
result += "<->" |
|||
} else if i != 0 { |
|||
result += " & " |
|||
} |
|||
|
|||
result += token |
|||
|
|||
if startQuotes { |
|||
inQuotes = true |
|||
} |
|||
if clearQuotes { |
|||
inQuotes = false |
|||
result += ")" |
|||
} |
|||
} |
|||
|
|||
if inQuotes { |
|||
return result |
|||
} |
|||
|
|||
return strings.TrimLeft(result, " ") |
|||
} |
@ -0,0 +1,23 @@ |
|||
package postgres |
|||
|
|||
import ( |
|||
"fmt" |
|||
"github.com/stretchr/testify/assert" |
|||
"testing" |
|||
) |
|||
|
|||
func TestTSQueryFromSearch(t *testing.T) { |
|||
rows := [][2]string{ |
|||
{`asari matron`, `asari & matron`}, |
|||
{`"asari matron"`, `(asari<->matron)`}, |
|||
{`"asari matron" blue`, `(asari<->matron) & blue`}, |
|||
{`"christmas present" "wrapping paper"`, `(christmas<->present) & (wrapping<->paper)`}, |
|||
{`stuff`, `stuff`}, |
|||
} |
|||
|
|||
for i, row := range rows { |
|||
t.Run(fmt.Sprintf("Row_%d", i), func(t *testing.T) { |
|||
assert.Equal(t, row[1], TSQueryFromSearch(row[0])) |
|||
}) |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue