-
Notifications
You must be signed in to change notification settings - Fork 9
/
es_search.py
58 lines (47 loc) · 1.23 KB
/
es_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
# coding: utf-8
# 搜索
import sys
from elasticsearch import Elasticsearch
if len(sys.argv) >= 2:
keywords = ' '.join(sys.argv[1:])
else:
print '''
示例:
python es_search.py 人民的名义
'''
sys.exit(0)
es = Elasticsearch([{'host':'127.0.0.1', 'port': 9200}])
print '仅从 content 字段搜索,取前5条结果:'
result = es.search(index='jianshu', doc_type='ariticle', body={
"query" : { "match" : { "content" : keywords }},
"from": 0,
"size": 5
})
for item in result['hits']['hits']:
print 'url: ', item['_source']['url']
print 'title: ', item['_source']['title']
print 'score: ', item['_score']
print
print '-------------'
print
print '从 title 和 content 字段搜索,取前5条结果:'
result = es.search(index='jianshu', doc_type='ariticle', body={
"query" : {
"bool": {
"should": [
{
"match": {"title": keywords}
},
{
"match": {"content": keywords}
}
]
}},
"from": 0,
"size": 5
})
for item in result['hits']['hits']:
print 'url: ', item['_source']['url']
print 'title: ', item['_source']['title']
print 'score: ', item['_score']
print