-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearchdao.py
114 lines (82 loc) · 2.32 KB
/
elasticsearchdao.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
from elasticsearch import Elasticsearch, helpers
es = Elasticsearch([{"host": "47.120.39.188", "port": 9200, "timeout": 1500}])
def read_es(host, port, index, query=""):
if es.ping():
print("Successfully connect!")
else:
print("Failed.....")
exit()
if query == "": # query为es的搜索条件
query = {
"query": {
"match_all": {}
},
# "size":1000
}
res = helpers.scan(es, index=index, scroll="20m", query=query)
return res
# 查询es
match_all_body = {
"query": {
"match_all": {
}
}
}
match_body = {
"query": {
"match": {
"info": "春秋版画博物馆 北京"
}
}
}
def get_search_result(ner_result_arr):
match_sentence = "info"
match_sentence_relu = " ".join(ner_result_arr)
match_body = {
"query": {
"match": {
match_sentence: match_sentence_relu
}
}
}
result=es.search(index="culture_heritage", body=match_body)
return result
def get_search_result_test(ner_result_arr):
match_sentence = "info"
match_body = {
"query": {
"match": {
match_sentence: ner_result_arr
}
}
, "explain": True
}
result=es.search(index="culture_heritage", body=match_body)
return result
def get_search_result_page(blurcontent: str, pageindex: int=0, pagerows: int=10):
match_sentence = "info"
match_body = {
"from": (pageindex - 1) * pagerows,
"size": pagerows,
"query": {
}
}
#进行模糊查询判断
if blurcontent is None or blurcontent == "":
match_body["query"] = {"match_all": {}}
else:
match_body["query"] = {"match": {"info": blurcontent}}
return es.search(index="culture_heritage", body=match_body)
def get_search_result_amount(blurcontent: str):
match_sentence = "info"
match_body = {
"query": {
}
}
#进行模糊查询判断
if blurcontent is None or blurcontent == "":
match_body["query"] = {"match_all": {}}
else:
match_body["query"] = {"match": {"info": blurcontent}}
return es.count(index="culture_heritage", body=match_body)
print(get_search_result_test("北京博物馆"))