Last active
September 1, 2018 12:04
-
-
Save harshvb7/acf8212b6fe64f051939df71324769e3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from elasticsearch_dsl import Document, Date, Keyword, Text | |
from elasticsearch_dsl.connections import connections | |
# Define a default Elasticsearch client | |
connections.create_connection(hosts=['localhost']) | |
class Article(Document): | |
title = Text(analyzer='snowball', fields={'raw': Keyword()}) | |
body = Text(analyzer='snowball') | |
tags = Keyword() | |
published_from = Date() | |
class Index: | |
name = 'blog' | |
settings = { | |
"number_of_shards": 2, | |
} | |
def is_published(self): | |
return datetime.now() >= self.published_from | |
# create the mappings in elasticsearch | |
Article.init() | |
# create and save and article | |
article = Article(meta={'id': 42}, title='Hello world!', tags=['test']) | |
article.body = ''' looong text ''' | |
article.published_from = datetime.now() | |
article.save() | |
article = Article.get(id=42) | |
print(article.is_published()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment