-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.py
63 lines (48 loc) · 1.6 KB
/
es.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
from datetime import datetime
from elasticsearch import Elasticsearch
from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Date, Keyword, Mapping, Nested, Text
from elasticsearch_dsl import DocType
# client = Elasticsearch()
# s = Search(using=client)
connections.create_connection(hosts=['localhost'])
class Page(DocType):
''' document, hashtags, url, store date
Note the resulting object, has its id accessible by ._id property, because
that counts as metadata to elasticsearch
'''
url = Text()
title = Text()
body = Text(analyzer='snowball')
tags = Text()
save_date = Date()
class Meta:
index = 'page'
def save(self, ** kwargs):
self.save_date = datetime.now()
return super(Page, self).save(**kwargs)
def initialize_indices():
# connections.create_connection(hosts=['localhost'])
print('Initiliazing Page index')
# name your type
m = Mapping('page')
# url = String(index='not_analyzed')
# title = String(analyzer='snowball', fields={'raw': String(index='not_analyzed')})
# body = String(analyzer='snowball')
# tags = String(index='not_analyzed')
# save_date = Date()
# add fields
m.field('url', 'text')
m.field('title', 'text')
m.field('body', 'text')
m.field('tags', 'text')
m.field('save_date', 'date')
# you can also define mappings for the meta fields
m.meta('_all', enabled=False)
# save the mapping into index 'my-index'
m.save('page')
def main():
# initialize_indices()
Page.init()
if __name__ == '__main__':
main()