Skip to content

Instantly share code, notes, and snippets.

@harshvb7
Last active September 1, 2018 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harshvb7/acf8212b6fe64f051939df71324769e3 to your computer and use it in GitHub Desktop.
Save harshvb7/acf8212b6fe64f051939df71324769e3 to your computer and use it in GitHub Desktop.
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