-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmulti_index_search.py
executable file
·123 lines (109 loc) · 2.6 KB
/
multi_index_search.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
#!/usr/bin/env python
from conf_examples import get_tair
from tair import ResponseError
# create index, The field of index is parsed according to the field corresponding to the text
# @param index the index
# @param schema the index schema
# @return success: true, fail: false.
def create_index(index: str, schema: str) -> bool:
try:
tair = get_tair()
tair.tft_createindex(index, schema)
return True
except ResponseError as e:
print(e)
return False
# Add doc to index, doc is JSON format.
# @param index the index
# @param doc the doc content
# @return unique doc id
def add_doc(index: str, doc: str):
try:
tair = get_tair()
return tair.tft_adddoc(index, doc)
except:
return None
# search index by request
# @param index the index
# @param request the request
# @return
def search_index(index: str, request: str):
try:
tair = get_tair()
return tair.tft_search(index, request)
except ResponseError as e:
print(e)
return None
json1 = """{
"mappings": {
"properties": {
"departure": {
"type": "keyword"
},
"destination": {
"type": "keyword"
},
"date": {
"type": "keyword"
},
"seat": {
"type": "keyword"
},
"with": {
"type": "keyword"
},
"flight_id": {
"type": "keyword"
},
"price": {
"type": "double"
},
"departure_time": {
"type": "long"
},
"destination_time": {
"type": "long"
}
}
}
}"""
json2 = """{
"departure": "zhuhai",
"destination": "hangzhou",
"date": "2022-09-01",
"seat": "first",
"with": "baby",
"flight_id": "CZ1000",
"price": 986.1,
"departure_time": 1661991010,
"destination_time": 1661998210
}"""
json3 = """{
"sort": [
"departure_time"
],
"query": {
"bool": {
"must": [
{
"term": {
"date": "2022-09-01"
}
},
{
"term": {
"seat": "first"
}
}
]
}
}
}"""
if __name__ == "__main__":
key = "MultiIndexSearch"
# create index
create_index(key, json1)
# add doc
add_doc(key, json2)
# search index
search_index(key, json3)