Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Integrating Sphinx and Elasticsearch

Markus H
February 12, 2015

Integrating Sphinx and Elasticsearch

This is a lightning talk presentation I gave a the Python Users Berlin meetup on Feb 12, 2015

Markus H

February 12, 2015
Tweet

More Decks by Markus H

Other Decks in Programming

Transcript

  1. Sphinx and Elasticsearch

  2. • elasticsearch-dsl==0.0.3 • Sphinx==1.2.3 Which libraries to use?

  3. def setup(app): app.connect( 'doctree-resolved', index ) Sphinx integration

  4. from elasticsearch_dsl import DocType, String class Document(DocType): title = String(analyzer='snowball',

    boost=5) body = String(analyzer='snowball') docname = String(index='not_analyzed') class Meta: index = 'docs' Elasticsearch Document
  5. import hashlib from elasticsearch_dsl.connections import connections connections.create_connection(hosts=['localhost']) def index(app, doctree,

    docname): title = app.env.titles[docname].astext() doc = Document( title=title, body=doctree.astext(), docname=docname, ) doc.id = hashlib.md5(docname.encode('utf-8')).hexdigest() doc.save() Index a Document
  6. from elasticsearch_dsl.connections import connections connections.create_connection(hosts=['localhost']) search = Document.search() query =

    search.query( 'query_string', query='Migrations', fields=['title', 'body'], ).highlight_options(order='score').highlight('body') results = query.execute() Search
  7. >>> for hit in results: ... print(hit.title) Writing database migrations

    Writing your first Django app, part 1 Django documentation FAQ: Databases and models System check framework ... Using the hits
  8. Thank you! markusholtermann.eu @m_holtermann github.com/MarkusH