forked from raharrison/elastic-jekyll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searcher.py
48 lines (42 loc) · 990 Bytes
/
searcher.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
from elasticsearch import Elasticsearch
es = Elasticsearch([{'host': 'localhost', 'port': 9200}])
user_query = "python"
query = {
"query": {
"multi_match": {
"query": user_query,
"type": "best_fields",
"fuzziness": "AUTO",
"tie_breaker": 0.3,
"fields": ["title^3", "body"]
}
},
"highlight": {
"fields" : {
"body" : {}
}
},
"_source": ["title", "url"]
}
res = es.search(index="blog", body=query)
print("Found %d Hits:" % res['hits']['total'])
for hit in res['hits']['hits']:
print(hit["_source"])
# POST /blog/post/_search
# {
# "query": {
# "multi_match": {
# "query": "python",
# "type": "best_fields",
# "fuzziness": "AUTO",
# "tie_breaker": 0.3,
# "fields": ["title^3", "body"]
# }
# },
# "highlight": {
# "fields" : {
# "body" : {}
# }
# },
# "_source": ["title", "url"]
# }