-
Notifications
You must be signed in to change notification settings - Fork 2
/
ESSearch.py
98 lines (81 loc) · 3.66 KB
/
ESSearch.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
import elasticsearch
import json
#This function maps using keyword_dict
#Parameter:
# index_name : name of index
# type : name of type
# keyword_dict : dictionary of goods
def search(ES, index_name, type, keyword_dict):
bodydict = {
'query': {
"function_score" : {
"query" : {
"bool" : {
"must" : [
]
},
},
"boost" : "1",
"functions" : [
{
"filter" : {
"bool" : {
"must" : [
]
}
},
"weight" : 2
},
{
"field_value_factor": {
"field" : "clickct",
"factor" : 0.05,
},
"weight" : 1
}
],
"score_mode" : "sum"
}
}
}
if "site_name" in list(keyword_dict.keys()):
bodydict["query"]["function_score"]["query"]["bool"]["must"].append({"match": {"site_name": keyword_dict["site_name"]}})
bodydict["query"]["function_score"]["functions"][0]["filter"]["bool"]["must"].append({"match": {"site_name": keyword_dict["site_name"]}})
if "cate1" in list(keyword_dict.keys()):
bodydict["query"]["function_score"]["query"]["bool"]["must"].append({"match": {"cate1": keyword_dict["cate1"]}})
bodydict["query"]["function_score"]["functions"][0]["filter"]["bool"]["must"].append({"match": {"cate1": keyword_dict["cate1"]}})
if "cate2" in list(keyword_dict.keys()):
bodydict["query"]["function_score"]["query"]["bool"]["must"].append({"match": {"cate2": keyword_dict["cate2"]}})
bodydict["query"]["function_score"]["functions"][0]["filter"]["bool"]["must"].append(
{"match": {"cate2": keyword_dict["cate2"]}})
if "cate3" in list(keyword_dict.keys()):
bodydict["query"]["function_score"]["query"]["bool"]["must"].append({"match": {"cate3": keyword_dict["cate3"]}})
bodydict["query"]["function_score"]["functions"][0]["filter"]["bool"]["must"].append(
{"match": {"cate3": keyword_dict["cate3"]}})
if "keyword" in list(keyword_dict.keys()):
bodydict["query"]["function_score"]["query"]["bool"]["must"].append({"match": {"name": keyword_dict["keyword"]}})
bodydict["query"]["function_score"]["functions"][0]["filter"]["bool"]["must"].append(
{"match": {"name": keyword_dict["keyword"]}})
docs = ES.search(index=index_name, doc_type=type,body=bodydict)
print(json.dumps(docs
,ensure_ascii=False, indent=2))
#This function process keywords using Hangul morpheme anlyzer
#Parameter:
# index_name : name of index
# keyword : search word
def ProcessKeyword(ES, index_name, keyword):
keywordlist = []
reskey = ""
res = elasticsearch.client.IndicesClient.analyze(ES, index=index_name, body = {
"analyzer" : "korean",
"text" : keyword
})
for element in res['tokens']:
if element['type'] == "NNG" or element['type'] == "NNP":
keyword = element['token']
if not keyword in keywordlist:
keywordlist.append(keyword)
for key in keywordlist:
reskey += key
reskey += " "
return reskey[:-1]