Skip to content

Instantly share code, notes, and snippets.

@smn
Last active August 29, 2015 14:09
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 smn/306ddb1c3a4654ec4a7f to your computer and use it in GitHub Desktop.
Save smn/306ddb1c3a4654ec4a7f to your computer and use it in GitHub Desktop.
elastic git sample code
/the_repository/
*.pyc
/ve/
from elasticgit.models import Model, IntegerField, TextField
class Person(Model):
age = IntegerField('The Age')
name = TextField('The Name')
virtualenv ve
source ve/bin/activate
pip install -r requirements.txt
(ve)$ cat model/Person/a4f6b610c4564673afe95c1d1ed1bffc.json
{
"age": 34,
"_version": {
"package_version": "0.2.9",
"language_version": "2.7.6",
"language_version_string": "2.7.6 (default, Dec 22 2013, 09:30:03) \n[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)]",
"language": "python",
"package": "elastic-git"
},
"name": "Simon",
"uuid": "a4f6b610c4564673afe95c1d1ed1bffc"
}
from elasticgit import EG
from model import Person
workspace = EG.workspace('the_repository')
simon = Person({
'name': 'Simon',
'age': 34,
})
workspace.save(simon, 'Saving Simon!')
(ve)$ python workspace2.py
I have found 1 people
name: Simon age: 34
from elasticgit import EG
from model import Person
workspace = EG.workspace('the_repository')
people_count = workspace.S(Person).count()
print "I have found %s people" % (people_count,)
for person in workspace.S(Person):
print 'name:', person.name, 'age:', person.age
from elasticgit import EG
from model import Person
workspace = EG.workspace('the_repository')
for count in range(20):
person = Person({
'name': 'Person %s' % (count,),
'age': count,
})
workspace.save(person, 'Saving person %s' % (count,))
(ve)$ python workspace4.py
name: Person 15 age: 15
name: Person 14 age: 14
name: Person 13 age: 13
name: Person 12 age: 12
name: Person 11 age: 11
from elasticgit import EG
from model import Person
workspace = EG.workspace('the_repository')
people_count = workspace.S(Person).count()
# Find all people in older than 10 up until and including 15.
people = workspace.S(Person).filter(age__gt=10, age__lte=15).order_by('-age')
for person in people:
print 'name:', person.name, 'age:', person.age
(ve)$ python workspace5.py
ES result <elasticgit.manager.PersonMappingType object at 0x10fbbd4d0> name: Person 10
Git object <model.Person object at 0x10fbbd410> name: Person 10
from elasticgit import EG
from model import Person
workspace = EG.workspace('the_repository')
people_count = workspace.S(Person).count()
# Find all people in older than 10 up until and including 15.
[es_result] = workspace.S(Person).filter(age=10)
print 'ES result', es_result, 'name:', es_result.name
git_object = es_result.get_object()
print 'Git object', git_object, 'name: ', git_object.name
(ve)$ python workspace6.py
name: Person 10 age: 10
name: Simon age: 34
from elasticgit import EG, F
from model import Person
workspace = EG.workspace('the_repository')
people = workspace.S(Person).filter(F(age=10) | F(name='simon'))
for person in people:
print 'name:', person.name, 'age:', person.age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment