@honzakral
import requests
ES_URL = 'http://localhost:9200'
requests.put(ES_URL + '/i/t/42', json={"title": "PyCon Belarus"})
query = {"query": {"match": {"title": "pycon"}}}
data = requests.get(ES_URL + '/i/_search', json=query).json()
"Nobody should have a reason not to use the official client."
response = client.search(
index="my-index",
body={
"query": {
"bool": {
"must": {"match": {"title": "python"}},
"must_not": {"match": {"description": "beta"}}
}
}
}
)
curl -X GET localhost:9200/my-index/_search -d '{
"query": {
"bool": {
"must": {"match": {"title": "python"}},
"must_not": {"match": {"description": "beta"}}
}
}
}'
from elasticsearch import Elasticsearch
from elasticsearch_async import AsyncTransport
client = Elasticsearch(transport_class=AsyncTransport)
response = client.search(
index="my-index",
body={
"query": {
"bool": {
"must": [{"match": {"title": "python"}}],
"must_not": [{"match": {"description": "beta"}}]
"filter": [{"term": {"category": "search"}}]
}
},
"aggs" : {
"per_tag": {
"terms": {"field": "tags"},
"aggs": {
"max_lines": {"max": {"field": "lines"}}
}
}
}
}
)
for hit in response['hits']['hits']:
print(hit['_score'], hit['_source']['title'])
s = Search(using=client, index="my-index")
# filter only search
s = s.filter("term", category="search")
# we want python in title
s = s.query("match", title="python")
# and no beta releases
s = s.query(~Q("match", description="beta"))
# aggregate on tags
s.aggs.bucket('per_tag', 'terms', field='tags')
# max lines per tag
s.aggs['per_tag'].metric('max_lines', 'max', field='lines')
response = client.search(
index="my-index",
body={
"query": {
"bool": {
"must": [{"match": {"title": "python"}}],
"must_not": [{"match": {"description": "beta"}}]
"filter": [{"term": {"category": "search"}}]
}
},
"aggs" : {
"per_tag": {
"terms": {"field": "tags"},
"aggs": {
"max_lines": {"max": {"field": "lines"}}
}
}
}
}
)
for hit in response['hits']['hits']:
print(hit['_score'], hit['_source']['title'])
s = Search(using=client, index="my-index")
s = s.filter("term", category="search")
s = s.query("match", title="python")
s = s.query(~Q("match", description="beta"))
s.aggs.bucket('per_tag', 'terms', field='tags') \
.metric('max_lines', 'max', field='lines')
for hit in s:
print(hit.meta.score, hit.title)
from datetime import date
from elasticsearch_dsl import DocType, Text, Keyword, Date
class BlogPost(DocType):
title = Text(analyzer="english")
body = Text(analyzer="english")
published_date = Date()
tags = Keyword(multi=True)
BlogPost.search('terms', tags=['python', 'belarus'])
bp = BlogPost(title='PyCon Belarus', published_date=date.today())
bp.tags.append('python')
bp.save()
from django import models
from .search import BlogPost as BlogPostDoc
class BlogPost(models.Model):
title = models.CharField(max_length=200)
...
def to_search(self):
return BlogPostDoc(
_id=self.id,
title=self.title,
tags=[t.name for t in self.tags.all()],
...
)
def update_search(instance, **kwargs):
instance.to_search().save()
def remove_from_search(instance, **kwargs):
instance.to_search().delete()
post_save.connect(update_search, sender=BlogPost)
pre_delete.connect(remove_from_search, sender=BlogPost)
from elasticsearch_dsl import FacetedSearch, TermsFacet, DateHistogramFacet
class BlogSearch(FacetedSearch):
doc_types = [BlogPost, ]
fields = ['tags', 'title', 'body']
facets = {
'tags': TermsFacet(field='tags'),
'months': DateHistogramFacet(field='published_date', interval='month')
}
bs = BlogSearch('python web', {'months': date(2015, 6)})