Create a gist now

Instantly share code, notes, and snippets.

@davidbgk /server.py
Last active Dec 16, 2015

Tutoriel elasticsearch pendant mix-IT 2013 en utilisant Python
# brew install elasticsearch
# modify elasticsearch.yml : cluster.name and node.name with your name
# set discovery.zen.ping.multicast.enabled to false in the same file
# mkvirtualenv elastic-mixit
# pip install pyes
from pyes import ES, TermQuery
conn = ES('127.0.0.1:9200')
try:
conn.delete_index("test-index")
except:
pass
conn.create_index("test-index")
mapping = {
u'desc': {
'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': u'string',
"term_vector": "with_positions_offsets"
},
u'name': {
'boost': 1.0,
'index': 'analyzed',
'store': 'yes',
'type': u'string',
"term_vector": "with_positions_offsets"
},
}
conn.put_mapping("test-type", {'properties': mapping}, ["test-index"])
conn.index({
"name": "David Larlet",
"desc": "A Python guy",
}, "test-index", "test-type", 1)
conn.index({
"name": "David Pilato",
"desc": "A Java guy",
}, "test-index", "test-type", 2)
conn.refresh(["test-index"])
for result in conn.search(query=TermQuery("name", "david")):
print result
# => {u'name': u'David Larlet', u'desc': u'A Python guy'}
# => {u'name': u'David Pilato', u'desc': u'A Java guy'}
for result in conn.search(query=TermQuery("name", "larlet")):
print result
# => {u'name': u'David Larlet', u'desc': u'A Python guy'}
for result in conn.search(query=TermQuery("desc", "python")):
print result
# => {u'name': u'David Larlet', u'desc': u'A Python guy'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment