# Path: api/theatrics/connections.py

# 
# Path: api/theatrics/settings.py
# ELASTICSEARCH_INDEX = os.environ.get('ELASTICSEARCH_INDEX', 'theatrics')
# 
# Path: api/theatrics/utils/functional.py
# def containing(value):
#     return lambda container: value in container


import aioes
from funcy import collecting, compact, is_seqcont, pluck
from .connections import elastic
from .settings import ELASTICSEARCH_INDEX
from .utils.functional import containing



__all__ = [
    'search', 'fetch_item', 'fetch_multiple_items', 'expand_item',
    'expand_multiple_items', 'simplify_item'
]


async def search(body, type_=None, page=1, page_size=20, fields=None):
    body['size'] = page_size
    body['from'] = (page - 1) * page_size
    if fields:
        body['_source'] = fields

    response = await elastic.search(ELASTICSEARCH_INDEX, type_, body)
    hits = response['hits']
    took = response['took']
    count = hits['total']
    items = list(map(simplify_item, hits['hits']))
    return items, count, took


async def fetch_item(type_, id_, fields=None):
    kwargs = {}
    if fields:
        kwargs['_source'] = ','.join(fields)

    try:

# Path: api/theatrics/connections.py

# 
# Path: api/theatrics/settings.py
# ELASTICSEARCH_INDEX = os.environ.get('ELASTICSEARCH_INDEX', 'theatrics')
# 
# Path: api/theatrics/utils/functional.py
# def containing(value):
#     return lambda container: value in container


# Path: api/theatrics/dao.py
import aioes
from funcy import collecting, compact, is_seqcont, pluck
from .connections import elastic
from .settings import ELASTICSEARCH_INDEX
from .utils.functional import containing




__all__ = [
    'search', 'fetch_item', 'fetch_multiple_items', 'expand_item',
    'expand_multiple_items', 'simplify_item'
]


async def search(body, type_=None, page=1, page_size=20, fields=None):
    body['size'] = page_size
    body['from'] = (page - 1) * page_size
    if fields:
        body['_source'] = fields

    response = await elastic.search(ELASTICSEARCH_INDEX, type_, body)
    hits = response['hits']
    took = response['took']
    count = hits['total']
    items = list(map(simplify_item, hits['hits']))
    return items, count, took


async def fetch_item(type_, id_, fields=None):
    kwargs = {}
    if fields:
        kwargs['_source'] = ','.join(fields)

    try:

        response = await elastic.get(ELASTICSEARCH_INDEX, id_, type_, **kwargs)