-
Notifications
You must be signed in to change notification settings - Fork 28
/
pindex_bleve_delete_test.go
169 lines (151 loc) · 3.27 KB
/
pindex_bleve_delete_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright 2019-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"os"
"testing"
"github.com/blevesearch/bleve/v2"
)
func TestDeleteByQuery(t *testing.T) {
bleve.Config.DefaultIndexType = "scorch"
defer func() {
err := os.RemoveAll("testidx")
if err != nil {
t.Fatal(err)
}
}()
index, err := bleve.New("testidx", bleve.NewIndexMapping())
if err != nil {
t.Fatal(err)
}
batch := index.NewBatch()
doca := map[string]interface{}{
"name": "couchbase fts",
"desc": "awesome search functionality",
}
err = batch.Index("a", doca)
if err != nil {
t.Error(err)
}
docy := map[string]interface{}{
"name": "couchbase fts dev",
"desc": "steve abhi sreekanth",
}
err = batch.Index("y", docy)
if err != nil {
t.Error(err)
}
docx := map[string]interface{}{
"name": "couchbase fts dead",
"desc": "keshav",
}
err = batch.Index("x", docx)
if err != nil {
t.Error(err)
}
docb := map[string]interface{}{
"name": "couchbase fts test",
"desc": "girish master",
}
err = batch.Index("b", docb)
if err != nil {
t.Error(err)
}
err = index.Batch(batch)
if err != nil {
t.Error(err)
}
// close the index, open it again
err = index.Close()
if err != nil {
t.Fatal(err)
}
index, err = bleve.Open("testidx")
if err != nil {
t.Fatal(err)
}
defer func() {
err := index.Close()
if err != nil {
t.Fatal(err)
}
}()
count, err := index.DocCount()
if err != nil {
t.Fatal(err)
}
if count != 4 {
t.Errorf("expected doc count 4, got %d", count)
}
// delete all documents which has the text "couchbase"
cleaner := newDelByQueryHandler(index)
mq := bleve.NewMatchQuery("couchbase")
mq.Analyzer = "keyword"
pCount, err := cleaner.deleteByQuery(mq)
if err != nil {
t.Fatal(err)
}
if pCount != 4 {
t.Errorf("expected deleted doc count is 4, got %d", pCount)
}
count, err = index.DocCount()
if err != nil {
t.Fatal(err)
}
if count != 0 {
t.Errorf("expected doc count 0, got %d", count)
}
batch.Reset()
// index some more data
doca = map[string]interface{}{
"name": "full text search",
"desc": "work in progress",
}
err = batch.Index("a", doca)
if err != nil {
t.Error(err)
}
docb = map[string]interface{}{
"name": "full text search",
"desc": "work in progress",
}
err = batch.Index("b", docb)
if err != nil {
t.Error(err)
}
docd := map[string]interface{}{
"name": "couchbase",
"desc": "work in progress",
}
err = batch.Index("d", docd)
if err != nil {
t.Error(err)
}
err = index.Batch(batch)
if err != nil {
t.Error(err)
}
// delete all documents which has the text "search"
cleaner = newDelByQueryHandler(index)
mq = bleve.NewMatchQuery("search")
mq.Analyzer = "keyword"
pCount, err = cleaner.deleteByQuery(mq)
if err != nil {
t.Fatal(err)
}
if pCount != 2 {
t.Errorf("expected deleted doc count is 2, got %d", pCount)
}
count, err = index.DocCount()
if err != nil {
t.Fatal(err)
}
if count != 1 {
t.Errorf("expected doc count 1, got %d", count)
}
}