Skip to content

Commit 24b402d

Browse files
vdimirpaskal
authored andcommitted
vendor deps for full text search
1 parent 261cf60 commit 24b402d

File tree

736 files changed

+358905
-1575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

736 files changed

+358905
-1575
lines changed

backend/_example/memory_store/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/go-pkgz/lgr v0.10.4
88
github.com/jessevdk/go-flags v1.5.0
99
github.com/stretchr/testify v1.8.1
10-
github.com/umputun/remark42/backend v1.11.2
10+
github.com/umputun/remark42/backend v1.11.3
1111
)
1212

1313
require (
@@ -36,6 +36,7 @@ require (
3636
golang.org/x/image v0.3.0 // indirect
3737
golang.org/x/net v0.5.0 // indirect
3838
golang.org/x/sys v0.4.0 // indirect
39+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
3940
gopkg.in/yaml.v3 v3.0.1 // indirect
4041
)
4142

backend/_example/memory_store/go.sum

Lines changed: 1171 additions & 1 deletion
Large diffs are not rendered by default.

backend/app/rest/api/rest_public.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@ func (s *public) searchQueryCtrl(w http.ResponseWriter, r *http.Request) {
444444
return encodeJSONWithHTML(commentsWithTotal{Comments: comments, Total: total})
445445
})
446446

447+
if err == service.ErrSearchIsNotEnabled {
448+
rest.SendErrorJSON(w, r, http.StatusNotFound, err, "can't perform search request", rest.ErrInternal)
449+
return
450+
}
451+
447452
if err != nil {
448453
rest.SendErrorJSON(w, r, http.StatusBadRequest, err, "can't perform search request", rest.ErrInternal)
449454
return

backend/app/rest/api/rest_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ func TestRest_Search(t *testing.T) {
256256
{"hello world test", "&sort=time&skip=1&limit=1", []int{2}, http.StatusOK},
257257
{"", "", []int{}, http.StatusBadRequest},
258258
{"test", "&sort=text", []int{}, http.StatusBadRequest},
259+
{"test", "&limit=a", []int{}, http.StatusBadRequest},
259260
{"test", "&skip=-1", []int{}, http.StatusBadRequest},
260261
{"test", "&limit=999999", []int{}, http.StatusBadRequest},
261262
}
@@ -299,7 +300,7 @@ func TestRest_SearchDisabledFeature(t *testing.T) {
299300
resp, err := http.Get(ts.URL + "/api/v1/search?site=remark42&query=test")
300301
require.NoError(t, err)
301302
require.NoError(t, resp.Body.Close())
302-
assert.Equal(t, http.StatusBadRequest, resp.StatusCode)
303+
assert.Equal(t, http.StatusNotFound, resp.StatusCode)
303304
}
304305

305306
func Test_URLKey(t *testing.T) {

backend/app/store/search/search_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package search
22

33
import (
44
"context"
5+
"encoding/hex"
56
"fmt"
7+
"hash/fnv"
68
"math/rand"
79
"os"
810
"testing"
@@ -113,6 +115,10 @@ func TestSearch_Site(t *testing.T) {
113115
require.NoError(t, err)
114116
assert.Len(t, res.Keys, 0)
115117

118+
res, err = searcher.Search(&Request{SiteID: "test-site-not-found", Query: "345", Limit: 3})
119+
require.Error(t, err)
120+
assert.Nil(t, res)
121+
116122
// restart service to check that saved index is loaded
117123
err = searcher.Close()
118124
require.NoError(t, err)
@@ -236,23 +242,39 @@ func TestSearch_IndexStartup(t *testing.T) {
236242
searcher, serviceTeardown := createTestService(t, sites)
237243
defer serviceTeardown()
238244

245+
sites = append(sites, "not-indexed")
239246
storeEngine, dbTeardown := createDB(t, 42, sites)
240247
defer dbTeardown()
241248

242249
grp := syncs.NewErrSizedGroup(4)
250+
243251
for _, siteID := range sites {
244252
err := IndexSite(context.Background(), siteID, searcher, storeEngine, grp)
245253
require.NoError(t, err)
246254
}
247255
err := grp.Wait()
248256
require.NoError(t, err)
249257

258+
// run index again, should not fail, but do nothing
259+
err = IndexSite(context.Background(), sites[0], searcher, storeEngine, grp)
260+
require.NoError(t, err)
261+
262+
err = grp.Wait()
263+
require.NoError(t, err)
264+
250265
for _, siteID := range sites {
266+
251267
serp, err := searcher.Search(&Request{
252268
SiteID: siteID,
253269
Query: "text",
254270
Limit: 19,
255271
})
272+
273+
if siteID == "not-indexed" {
274+
assert.Error(t, err)
275+
continue
276+
}
277+
256278
assert.NoError(t, err)
257279
assert.Len(t, serp.Keys, 19)
258280
}
@@ -270,6 +292,47 @@ func TestSearch_BadAnalyzerName(t *testing.T) {
270292
require.Error(t, err)
271293
}
272294

295+
func TestSearch_NewDirtyPath(t *testing.T) {
296+
idxPath := os.TempDir() + "/search-remark42"
297+
_ = os.RemoveAll(idxPath)
298+
299+
err := os.MkdirAll(idxPath, 0o0755)
300+
require.NoError(t, err)
301+
302+
defer func() { _ = os.RemoveAll(idxPath) }()
303+
304+
// that's how we calculate site index path in NewService
305+
siteIdxPath := idxPath + "/" + hex.EncodeToString(fnv.New32().Sum([]byte("test-size")))
306+
307+
// try to create index specifying path to file
308+
err = os.WriteFile(siteIdxPath, []byte("some_data"), 0o0644)
309+
require.NoError(t, err)
310+
// file exists, but not a directory
311+
_, err = NewService([]string{"test-size"}, ServiceParams{IndexPath: idxPath, Analyzer: "standard"})
312+
assert.Error(t, err)
313+
assert.ErrorContains(t, err, "index path should be a directory")
314+
315+
err = os.Remove(siteIdxPath)
316+
require.NoError(t, err)
317+
err = os.MkdirAll(siteIdxPath, 0o0755)
318+
require.NoError(t, err)
319+
320+
// write some garbage to site index dir
321+
err = os.WriteFile(siteIdxPath+"/dirty", []byte("garbage"), 0o0644)
322+
require.NoError(t, err)
323+
324+
// site index dir is dirty
325+
_, err = NewService([]string{"test-size"}, ServiceParams{IndexPath: idxPath, Analyzer: "standard"})
326+
assert.Error(t, err, "index path should be a directory")
327+
328+
err = os.Remove(siteIdxPath + "/dirty")
329+
require.NoError(t, err)
330+
331+
// empty index dir
332+
_, err = NewService([]string{"test-size"}, ServiceParams{IndexPath: idxPath, Analyzer: "standard"})
333+
assert.NoError(t, err)
334+
}
335+
273336
func createDB(t *testing.T, commentsPerSite int, sites []string) (e engine.Interface, teardown func()) {
274337
testDB := os.TempDir() + "/remark-db"
275338
_ = os.RemoveAll(testDB)

backend/app/store/service/service.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ var nonAdminUser = store.User{}
8383
// ErrRestrictedWordsFound returned in case comment text contains restricted words
8484
var ErrRestrictedWordsFound = fmt.Errorf("comment contains restricted words")
8585

86+
// ErrSearchIsNotEnabled returned on search request if search service was not configured
87+
var ErrSearchIsNotEnabled = fmt.Errorf("search service is not enabled")
88+
8689
// Create prepares comment and forward to Interface.Create
8790
func (s *DataStore) Create(comment store.Comment) (commentID string, err error) {
8891
if comment, err = s.prepareNewComment(comment); err != nil {
@@ -923,7 +926,7 @@ func (s *DataStore) Last(siteID string, limit int, since time.Time, user store.U
923926
// Search executes search query and enriches the result with comment content
924927
func (s *DataStore) Search(siteID, query, sortBy string, limit, skip int) ([]store.Comment, uint64, error) {
925928
if s.SearchService == nil {
926-
return []store.Comment{}, 0, fmt.Errorf("search service is not enabled")
929+
return []store.Comment{}, 0, ErrSearchIsNotEnabled
927930
}
928931
req := &search.Request{
929932
SiteID: siteID,
@@ -941,12 +944,14 @@ func (s *DataStore) Search(siteID, query, sortBy string, limit, skip int) ([]sto
941944
comments := make([]store.Comment, 0, len(searchRes.Keys))
942945
for _, docKey := range searchRes.Keys {
943946
comment, e := s.Get(docKey.Locator, docKey.ID, nonAdminUser)
944-
if e != nil {
947+
if e != nil || comment.Deleted {
948+
searchRes.Total--
945949
// ignore error because comment can be deleted from the store (but we still have it in search index)
946950
continue
947951
}
948952
comments = append(comments, comment)
949953
}
954+
950955
return comments, searchRes.Total, nil
951956
}
952957

backend/app/store/service/service_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/umputun/remark42/backend/app/store/admin"
2525
"github.com/umputun/remark42/backend/app/store/engine"
2626
"github.com/umputun/remark42/backend/app/store/image"
27+
"github.com/umputun/remark42/backend/app/store/search"
2728
)
2829

2930
func TestService_CreateFromEmpty(t *testing.T) {
@@ -1616,6 +1617,50 @@ func TestService_alterComment(t *testing.T) {
16161617
assert.Equal(t, engine.FlagRequest{Flag: engine.Blocked, UserID: "devid"}, engineMock.FlagCalls()[0].Req)
16171618
}
16181619

1620+
func TestService_Search(t *testing.T) {
1621+
ks := admin.NewStaticKeyStore("secret 123")
1622+
1623+
eng, engTeardown := prepStoreEngine(t)
1624+
defer engTeardown()
1625+
1626+
srch, srchTeardown := prepSearcher(t)
1627+
defer srchTeardown()
1628+
1629+
b := DataStore{Engine: eng, AdminStore: ks}
1630+
1631+
{
1632+
res, total, err := b.Search("radio-t", "text", "", 10, 0)
1633+
assert.Error(t, err)
1634+
assert.Equal(t, uint64(0), total)
1635+
assert.Equal(t, 0, len(res))
1636+
}
1637+
1638+
b.SearchService = srch
1639+
1640+
comment := store.Comment{
1641+
Text: "some text",
1642+
User: store.User{ID: "newUser", Name: "new user"},
1643+
Locator: store.Locator{URL: "https://radio-t.com", SiteID: "radio-t"},
1644+
}
1645+
1646+
id, err := b.Create(comment)
1647+
require.NoError(t, err)
1648+
1649+
res, total, err := b.Search("radio-t", "text user:\"new user\"", "", 10, 0)
1650+
assert.NoError(t, err)
1651+
assert.Equal(t, uint64(1), total)
1652+
assert.Equal(t, 1, len(res))
1653+
assert.Equal(t, "some text", res[0].Text)
1654+
1655+
err = b.Delete(comment.Locator, id, store.SoftDelete)
1656+
require.NoError(t, err)
1657+
1658+
res, total, err = b.Search("radio-t", "text user:\"new user\"", "", 10, 0)
1659+
assert.NoError(t, err)
1660+
assert.Equal(t, uint64(0), total)
1661+
assert.Equal(t, 0, len(res))
1662+
}
1663+
16191664
func Benchmark_ServiceCreate(b *testing.B) {
16201665
dbFile := fmt.Sprintf("%s/test-remark42-%d.db", os.TempDir(), rand.Intn(9999999999))
16211666
defer func() { _ = os.Remove(dbFile) }()
@@ -1705,3 +1750,23 @@ func getReq(locator store.Locator, commentID string) engine.GetRequest {
17051750
CommentID: commentID,
17061751
}
17071752
}
1753+
1754+
func prepSearcher(t *testing.T) (s *search.Service, teardown func()) {
1755+
idxPath, err := os.MkdirTemp("", "test_search_r42")
1756+
require.NoError(t, err)
1757+
_ = os.Remove(idxPath)
1758+
1759+
s, err = search.NewService([]string{"radio-t"}, search.ServiceParams{
1760+
IndexPath: idxPath,
1761+
Analyzer: "standard",
1762+
})
1763+
1764+
require.NoError(t, err)
1765+
1766+
teardown = func() {
1767+
err := s.Close()
1768+
require.NoError(t, err)
1769+
_ = os.RemoveAll(idxPath)
1770+
}
1771+
return s, teardown
1772+
}

backend/go.mod

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,29 @@ require (
3838
)
3939

4040
require (
41-
cloud.google.com/go/compute v1.6.1 // indirect
41+
cloud.google.com/go/compute v1.14.0 // indirect
42+
cloud.google.com/go/compute/metadata v0.2.3 // indirect
43+
github.com/RoaringBitmap/roaring v0.9.4 // indirect
4244
github.com/ajg/form v1.5.1 // indirect
4345
github.com/andybalholm/cascadia v1.3.1 // indirect
4446
github.com/aymerick/douceur v0.2.0 // indirect
47+
github.com/bits-and-blooms/bitset v1.2.0 // indirect
48+
github.com/blevesearch/bleve/v2 v2.3.6
49+
github.com/blevesearch/bleve_index_api v1.0.5 // indirect
50+
github.com/blevesearch/geo v0.1.16 // indirect
51+
github.com/blevesearch/go-porterstemmer v1.0.3 // indirect
52+
github.com/blevesearch/gtreap v0.1.1 // indirect
53+
github.com/blevesearch/mmap-go v1.0.4 // indirect
54+
github.com/blevesearch/scorch_segment_api/v2 v2.1.4 // indirect
55+
github.com/blevesearch/segment v0.9.0 // indirect
56+
github.com/blevesearch/snowballstem v0.9.0 // indirect
57+
github.com/blevesearch/upsidedown_store_api v1.0.1 // indirect
58+
github.com/blevesearch/vellum v1.0.9 // indirect
59+
github.com/blevesearch/zapx/v11 v11.3.7 // indirect
60+
github.com/blevesearch/zapx/v12 v12.3.7 // indirect
61+
github.com/blevesearch/zapx/v13 v13.3.7 // indirect
62+
github.com/blevesearch/zapx/v14 v14.3.7 // indirect
63+
github.com/blevesearch/zapx/v15 v15.3.8 // indirect
4564
github.com/cespare/xxhash/v2 v2.1.2 // indirect
4665
github.com/davecgh/go-spew v1.1.1 // indirect
4766
github.com/dghubble/oauth1 v0.7.2 // indirect
@@ -51,14 +70,17 @@ require (
5170
github.com/go-pkgz/email v0.4.1 // indirect
5271
github.com/go-pkgz/expirable-cache v0.1.0 // indirect
5372
github.com/go-redis/redis/v8 v8.11.5 // indirect
73+
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551 // indirect
5474
github.com/golang/protobuf v1.5.2 // indirect
5575
github.com/golang/snappy v0.0.4 // indirect
5676
github.com/gorilla/css v1.0.0 // indirect
5777
github.com/gorilla/websocket v1.5.0 // indirect
5878
github.com/hashicorp/errwrap v1.1.0 // indirect
5979
github.com/hashicorp/golang-lru v0.6.0 // indirect
80+
github.com/json-iterator/go v0.0.0-20171115153421-f7279a603ede // indirect
6081
github.com/klauspost/compress v1.15.2 // indirect
6182
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
83+
github.com/mschoch/smat v0.2.0 // indirect
6284
github.com/nullrocks/identicon v0.0.0-20180626043057-7875f45b0022 // indirect
6385
github.com/pkg/errors v0.9.1 // indirect
6486
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -68,11 +90,12 @@ require (
6890
github.com/xdg-go/stringprep v1.0.3 // indirect
6991
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect
7092
go.mongodb.org/mongo-driver v1.11.1 // indirect
93+
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
7194
golang.org/x/oauth2 v0.4.0 // indirect
72-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
95+
golang.org/x/sync v0.1.0 // indirect
7396
golang.org/x/sys v0.4.0 // indirect
7497
golang.org/x/text v0.6.0 // indirect
7598
google.golang.org/appengine v1.6.7 // indirect
76-
google.golang.org/protobuf v1.28.0 // indirect
99+
google.golang.org/protobuf v1.28.1 // indirect
77100
gopkg.in/yaml.v3 v3.0.1 // indirect
78101
)

0 commit comments

Comments
 (0)