-
Notifications
You must be signed in to change notification settings - Fork 17
/
post.py
141 lines (120 loc) · 3.06 KB
/
post.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import datetime
from anysearch.search_dsl import (
analyzer,
Boolean,
connections,
Completion,
Date,
Document,
InnerDoc,
Keyword,
Nested,
Text,
Integer,
Float,
)
from .read_only import ReadOnlyDocument
from .settings import BLOG_POST_DOCUMENT_NAME, ELASTICSEARCH_CONNECTION
try:
from elasticsearch import logger
except ImportError:
import logging
logger = logging.getLogger(__name__)
__all__ = (
'Comment',
'Post',
'ReadOnlyPost',
)
connections.create_connection(**ELASTICSEARCH_CONNECTION)
html_strip = analyzer(
'html_strip',
tokenizer="standard",
filter=["lowercase", "stop", "snowball"],
char_filter=["html_strip"]
)
class CommentAuthor(InnerDoc):
name = Text(fields={'raw': Keyword()})
age = Integer()
class Comment(InnerDoc):
author = Nested(CommentAuthor)
# author = Text(fields={'raw': Keyword()})
tag = Text(fields={'raw': Keyword()})
content = Text(analyzer='snowball')
created_at = Date()
def age(self):
return datetime.datetime.now() - self.created_at
class Post(Document):
title = Text(
analyzer=html_strip,
fields={'raw': Keyword()}
)
# title_suggest = Completion()
content = Text()
created_at = Date()
published = Boolean()
category = Text(
analyzer=html_strip,
fields={'raw': Keyword()}
)
comments = Nested(Comment)
tags = Text(
analyzer=html_strip,
fields={'raw': Keyword(multi=True)},
multi=True
)
num_views = Integer()
user_id = Integer()
class Index:
name = BLOG_POST_DOCUMENT_NAME
settings = {
'number_of_shards': 1,
'number_of_replicas': 1,
'blocks': {'read_only_allow_delete': None},
}
def add_comment(self, author, tag, content):
self.comments.append(
Comment(
author=author,
content=content,
tag=tag,
created_at=datetime.datetime.now()
)
)
def add_tag(self, name):
self.tags.append(name)
def save(self, ** kwargs):
if not self.created_at:
self.created_at = datetime.datetime.now()
return super().save(** kwargs)
class ReadOnlyPost(ReadOnlyDocument):
title = Text(
analyzer=html_strip,
fields={'raw': Keyword()}
)
# title_suggest = Completion()
content = Text()
created_at = Date()
published = Boolean()
category = Text(
analyzer=html_strip,
fields={'raw': Keyword()}
)
comments = Nested(Comment)
tags = Text(
analyzer=html_strip,
fields={'raw': Keyword(multi=True)},
multi=True
)
num_views = Integer()
class Index:
name = BLOG_POST_DOCUMENT_NAME
settings = {
'number_of_shards': 1,
'number_of_replicas': 1,
'blocks': {'read_only_allow_delete': None},
}
try:
# Create the mappings in Elasticsearch
Post.init()
except Exception as err:
logger.error(err)