forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchfilter_test.go
107 lines (96 loc) · 4.07 KB
/
searchfilter_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
// Copyright 2013 Matthew Baird
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package elastigo
import (
"fmt"
//"github.com/araddon/gou"
"github.com/bmizerany/assert"
"testing"
)
func TestFilters(t *testing.T) {
c := NewTestConn()
// search for docs that are missing repository.name
qry := Search("github").Filter(
Filter().Exists("repository.name"),
)
out, err := qry.Result(c)
assert.T(t, err == nil, t, "should not have error")
expectedDocs := 10
expectedHits := 7695
assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
qry = Search("github").Filter(
Filter().Missing("repository.name"),
)
expectedHits = 390
out, _ = qry.Result(c)
assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
//actor_attributes: {type: "User",
qry = Search("github").Filter(
Filter().Terms("actor_attributes.location", "portland"),
)
out, _ = qry.Result(c)
expectedDocs = 10
expectedHits = 71
assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
/*
Should this be an AND by default?
*/
qry = Search("github").Filter(
Filter().Terms("actor_attributes.location", "portland"),
Filter().Terms("repository.has_wiki", true),
)
out, err = qry.Result(c)
expectedDocs = 10
expectedHits = 44
assert.T(t, err == nil, t, "should not have error")
assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
// NOW, lets try with two query calls instead of one
qry = Search("github").Filter(
Filter().Terms("actor_attributes.location", "portland"),
)
qry.Filter(
Filter().Terms("repository.has_wiki", true),
)
out, err = qry.Result(c)
//gou.Debug(out)
assert.T(t, err == nil, t, "should not have error")
assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
qry = Search("github").Filter(
"or",
Filter().Terms("actor_attributes.location", "portland"),
Filter().Terms("repository.has_wiki", true),
)
out, err = qry.Result(c)
expectedHits = 6676
assert.T(t, err == nil, t, "should not have error")
assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have %v total got %v", expectedHits, out.Hits.Total))
}
func TestFilterRange(t *testing.T) {
c := NewTestConn()
// now lets filter range for repositories with more than 100 forks
out, _ := Search("github").Size("25").Filter(
Range().Field("repository.forks").From("100"),
).Result(c)
if out == nil || &out.Hits == nil {
t.Fail()
return
}
expectedDocs := 25
expectedHits := 725
assert.T(t, out.Hits.Len() == expectedDocs, fmt.Sprintf("Should have %v docs got %v", expectedDocs, out.Hits.Len()))
assert.T(t, out.Hits.Total == expectedHits, fmt.Sprintf("Should have total %v got %v", expectedHits, out.Hits.Total))
}