This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
162 lines (137 loc) · 4.98 KB
/
test.py
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
from elasticsearch import Elasticsearch
from gambit import Msearch, Mpercolate, percolate_and_get
from pprint import pprint
es = Elasticsearch('localhost:9200')
q1 = {"from": 0, "size": 2 }
q2 = {"from": 0, "size": 2 }
q3 = {"from": 0, "size": 2 }
dummy_mappings = {
"test-doc": {
"properties": {
"id": {
"type": "integer"
},
"name": {
"type": "string"
},
"color": {
"type": "string"
}
}
}
}
dummy_percolate_mappings = {
"test-percolate-doc": {
"properties": {
"location": {
"type": "geo_point"
}
}
}
}
test_doc1 = {
'author': 'kimchy',
'text': 'Elasticsearch: cool. bonsai cool.',
}
test_doc2 = {
'author': 'pydanny',
'text': 'Cookiecutter: cool. bonsai cool.',
}
d1 = {
'doc': {
'location': {
'lat': 28.411599,
'lon': 77.046451
}
}
}
d2 = {
'doc': {
'location': {
'lat': 28.411599,
'lon': 77.046451
}
}
}
polygon_vertices = [[76.2960577, 9.9366668], [76.3041687, 9.947209], [76.3106918, 9.9546484], [76.3180733, 9.9646238], [76.326828, 9.9749371], [76.3343811, 9.9837284], [76.3453674, 9.9888002], [76.3573837, 9.9935338], [76.3680267, 9.9984364], [76.3807297, 10.0036771], [76.3893127, 10.0141581], [76.3946342, 10.0232865], [76.4001274, 10.0317384], [76.3929176, 10.0381618], [76.3846779, 10.0461063], [76.3747215, 10.0548957], [76.3651085, 10.0619946], [76.3556671, 10.0684174], [76.3451958, 10.0760231], [76.3371277, 10.0817695], [76.3316345, 10.0849807], [76.3185883, 10.0810934], [76.3106918, 10.0788963], [76.3043404, 10.0780512], [76.295929, 10.0773752], [76.2885475, 10.0773752], [76.2829686, 10.0771216], [76.2816811, 10.0663891], [76.2784195, 10.0576], [76.2739563, 10.0526983], [76.2705231, 10.0466133], [76.2701797, 10.0413734], [76.2700081, 10.0352882], [76.2700081, 10.0292029], [76.2705231, 10.0207508], [76.2713814, 10.013651], [76.272068, 10.0080724], [76.2724972, 10.0027473], [76.2683773, 9.9937029], [76.2711239, 9.9871942], [76.2737847, 9.9822914], [76.2764454, 9.9766277], [76.2785911, 9.9711331], [76.2809944, 9.9661455], [76.2835693, 9.9611578], [76.2875175, 9.9529576], [76.2944698, 9.9386703], [76.2960577, 9.9366668]]
query = {
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"geo_polygon": {
"location": {
"points": polygon_vertices
}
}
}
}
}
}
def search_test_env(f):
def wrap():
es.indices.create(index='test-index')
es.indices.put_mapping(index='test-index', doc_type='test-doc', body=dummy_mappings)
es.index(index='test-index', doc_type='test-doc', body=test_doc1)
f()
es.indices.delete(index='test-index')
return wrap
def percolate_test_env(f):
def wrap():
es.indices.create(index='test-percolate-index')
es.indices.put_mapping(index='test-percolate-index', doc_type='test-percolate-doc', body=dummy_percolate_mappings)
es.create(
index='test-percolate-index',
doc_type=".percolator",
body=query
)
f()
es.indices.delete(index='test-percolate-index')
return wrap
@percolate_test_env
def test_percolate_and_get():
results = percolate_and_get(es, d2, index='test-percolate-index', doc='test-percolate-doc')
assert isinstance(results, list) == True
@search_test_env
def test_msearch():
s = Msearch(es)
s.add(q1, index='test-index', doc_type='test-doc')
s.add(q2, index='test-index', doc_type='test-doc')
results = s.execute()
assert isinstance(results, list) == True
for result in results:
assert isinstance(result, dict) == True
@search_test_env
def test_msearch_index():
s = Msearch(es, index='test-index')
s.add(q1, doc_type='test-doc')
s.add(q2, doc_type='test-doc')
results = s.execute()
assert isinstance(results, list) == True
for result in results:
assert isinstance(result, dict) == True
@percolate_test_env
def test_mpercolate():
s = Mpercolate(es)
s.add(d1, index='test-percolate-index', doc_type='test-percolate-doc')
s.add(d1, index='test-percolate-index', doc_type='test-percolate-doc')
results = s.execute()
assert isinstance(results, list) == True
for result in results:
assert isinstance(result, dict) == True
@percolate_test_env
def test_mpercolate_index():
s = Mpercolate(es, index='test-percolate-index')
s.add(d1, doc_type='test-percolate-doc')
s.add(d2, doc_type='test-percolate-doc')
results = s.execute()
assert isinstance(results, list) == True
for result in results:
assert isinstance(result, dict) == True
#test_msearch()
#test_msearch_index()
#test_percolate_and_get()
#test_mpercolate()
#test_mpercolate_index()