-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathelastic.py
56 lines (49 loc) · 1.47 KB
/
elastic.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
import os
from elasticsearch import Elasticsearch
es_port = 9200
es = Elasticsearch('http://127.0.0.1:{0}'.format(es_port)) # Use HTTP
try:
es.indices.delete(index="test-index")
except:
pass
es.indices.create(index="test-index")
mapping = {
'parsedtext': {
'boost': 1.0,
'index': True,
'store': True,
'type': 'text',
"term_vector": "with_positions_offsets"
},
'name': {
'boost': 1.0,
'index': True,
'store': True,
'type': 'text',
"term_vector": "with_positions_offsets"
},
'title': {
'boost': 1.0,
'index': True,
'store': True,
'type': 'text',
"term_vector": "with_positions_offsets"
},
'pos': {
'store': True,
'type': 'integer'
},
'uuid': {
'boost': 1.0,
'index': False,
'store': True,
'type': 'keyword'
}
}
es.indices.put_mapping(doc_type="test-type", body={"properties": mapping}, index="test-index")
es.index(index="test-index", doc_type="test-type", id=1, body={"name":"Joe Tester", "parsedtext":"Joe Testere nice guy", "uuid":"11111", "position":1})
es.index(index="test-index", doc_type="test-type", id=2, body={"name":"Bill Baloney", "parsedtext":"Joe Testere nice guy", "uuid":"22222", "position":2})
es.indices.refresh(index="test-index")
results = es.search(index="test-index", body={"query": {"match": {"name": "joe"}}})
for r in results['hits']['hits']:
print(r)