-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdev-client.py
71 lines (64 loc) · 1.93 KB
/
dev-client.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
import argparse
import json
from config import pukiwiki
from els.client import ElsClient
client = ElsClient(pukiwiki.ELASTIC_SEARCH_ENDPOINT, pukiwiki.INDEX)
def search(args):
query = {
"query": {
"query_string": {
# boost title
"fields": ["title^5", "body"],
"query": args.query,
# "analyzer": "jp_search_analyzer",
"default_operator": "AND",
# "type": "phrase",
}
}
}
if args.title: query["_source"] = ["title"]
if args.explain:
query["explain"] = True
query["profile"] = True
if args.highlight:
query["highlight"] = {
"encoder": "html",
"fields": {
"body": {
"pre_tags": ["<mark>"],
"post_tags": ["</mark>"],
"fragment_size": 220,
"no_match_size": 220,
"number_of_fragments": 1,
}
}
}
body = client.search(json.dumps(query)).read().decode("utf-8")
print(body)
parser = argparse.ArgumentParser(description='Elastic search client')
subparsers = parser.add_subparsers()
parser_search = subparsers.add_parser('search', help='search (see search -h)')
parser_search.add_argument('query', type=str, help='search query')
parser_search.add_argument(
'-t',
'--title',
action="store_true",
help="return title only"
)
parser_search.add_argument(
'-e',
'--explain',
action="store_true",
help="include explain"
)
parser_search.add_argument(
'--highlight',
action="store_true",
help="include highlight"
)
parser_search.set_defaults(func=search)
args = parser.parse_args()
if hasattr(args, 'func'):
args.func(args)
else:
parser.print_help()