Basic sample programΒΆ
Here’s a short script that gives you the gist of how to use ElasticUtils:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | """
This is a sample program that uses Elasticsearch (from elasticsearch-py)
object to create an index, create a mapping, and index some data. Then
it uses ElasticUtils S to show some behavior.
"""
from elasticutils import get_es, S
from elasticsearch.helpers import bulk_index
URL = 'localhost'
INDEX = 'fooindex'
DOCTYPE = 'testdoc'
# This creates an elasticsearch.Elasticsearch object which we can use
# to do all our indexing.
es = get_es(urls=[URL])
# First, delete the index if it exists.
es.indices.delete(index=INDEX, ignore=404)
# Define the mapping for the doctype 'testdoc'. It's got an id field,
# a title which is analyzed, and two fields that are lists of tags, so
# we don't want to analyze them.
mapping = {
DOCTYPE: {
'properties': {
'id': {'type': 'integer'},
'title': {'type': 'string', 'analyzer': 'snowball'},
'topics': {'type': 'string'},
'product': {'type': 'string', 'index': 'not_analyzed'},
}
}
}
# Create the index 'testdoc' mapping.
es.indices.create(INDEX, body={'mappings': mapping})
# Let's index some documents and make them available for searching.
documents = [
{'_id': 1,
'title': 'Deleting cookies',
'topics': ['cookies', 'privacy'],
'product': ['Firefox', 'Firefox for mobile']},
{'_id': 2,
'title': 'What is a cookie?',
'topics': ['cookies', 'privacy'],
'product': ['Firefox', 'Firefox for mobile']},
{'_id': 3,
'title': 'Websites say cookies are blocked - Unblock them',
'topics': ['cookies', 'privacy', 'websites'],
'product': ['Firefox', 'Firefox for mobile', 'Boot2Gecko']},
{'_id': 4,
'title': 'Awesome Bar',
'topics': ['tips', 'search', 'user interface'],
'product': ['Firefox']},
{'_id': 5,
'title': 'Flash',
'topics': ['flash'],
'product': ['Firefox']}
]
bulk_index(es, documents, index=INDEX, doc_type=DOCTYPE)
es.indices.refresh(index=INDEX)
# Now let's do some basic queries.
# Let's build a basic S that looks at our Elasticsearch cluster and
# the index and doctype we just indexed our documents in.
basic_s = S().es(urls=[URL]).indexes(INDEX).doctypes(DOCTYPE)
# How many documents are in our index?
print basic_s.count()
# Prints:
# 5
# Print articles with 'cookie' in the title.
print [item['title']
for item in basic_s.query(title__match='cookie')]
# Prints:
# [u'Deleting cookies', u'What is a cookie?',
# u'Websites say cookies are blocked - Unblock them']
# Print articles with 'cookie' in the title that are related to
# websites.
print [item['title']
for item in basic_s.query(title__match='cookie')
.filter(topics='websites')]
# Prints:
# [u'Websites say cookies are blocked - Unblock them']
# Print articles in the 'search' topic.
print [item['title']
for item in basic_s.filter(topics='search')]
# Prints:
# [u'Awesome Bar']
# Do a query and use the highlighter to denote the matching text.
print [(item['title'], item.es_meta.highlight['title'])
for item in basic_s.query(title__match='cookie').highlight('title')]
# Prints:
# [
# (u'Deleting cookies', [u'Deleting <em>cookies</em>']),
# (u'What is a cookie?', [u'What is a <em>cookie</em>?']),
# (u'Websites say cookies are blocked - Unblock them',
# [u'Websites say <em>cookies</em> are blocked - Unblock them']
# )
# ]
# That's the gist of it!
|