# Path: elasticmagic/search.py
# class SearchQuery(BaseSearchQuery):
#     """Elasticsearch search query construction object.
# 
#     :class:`.SearchQuery` object is usually instantiated by calling
#     :func:`Index.search_query()` method.
# 
#     See :func:`Index.search_query()` for more details.
# 
#     .. testsetup:: *
# 
#        import datetime
# 
#        from elasticmagic import SearchQuery, DynamicDocument
# 
#        class PostDocument(DynamicDocument):
#            __doc_type__ = 'post'
#     """
# 
#     def get_compiler(self):
#         return self._index_or_cluster.get_compiler()
# 
#     def get_query_compiler(self):
#         return self.get_compiler().compiled_query
# 
#     def to_dict(self, compiler=None):
#         """Compiles the query and returns python dictionary that can be
#         serialized to json.
#         """
#         compiler = compiler or self.get_compiler()
#         return compiler.compiled_query(self).body
# 
#     def get_result(self):
#         """Executes current query and returns processed :class:`SearchResult`
#         object. Caches result so subsequent calls with the same search query
#         will return cached value.
#         """
#         if self._cached_result is not None:
#             return self._cached_result
# 
#         self._cached_result = self._index_or_cluster.search(self)
#         return self._cached_result
# 
#     @property
#     def result(self):
#         warnings.warn(
#             'Field `result` is deprecated, use `get_result` method instead',
#             DeprecationWarning
#         )
#         return self.get_result()
# 
#     @property
#     def results(self):
#         warnings.warn(
#             'Field `results` is deprecated, use `get_result` method instead',
#             DeprecationWarning
#         )
#         return self.get_result()
# 
#     def count(self):
#         """Executes current query and returns number of documents matched the
#         query. Uses `count api <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html>`_.
#         """  # noqa:E501
#         return self._index_or_cluster.count(self).count
# 
#     def exists(self):
#         """Executes current query optimized for checking that
#         there are at least 1 matching document. This method is an analogue of
#         the old `exists search api <https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-exists.html>`_
#         For Elasticsearch 5.x and more it emulates the exists API using
#         `size=0` and `terminate_after=1`.
#         """  # noqa:E501
#         return self._index_or_cluster.exists(self).exists
# 
#     def explain(self, doc_or_id, doc_cls=None, routing=None, **kwargs):
#         """Computes a score explanation for the current search query and the document.
# 
#         :return: :class:`.result.ExplainResult`
# 
#         https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html
#         """  # noqa:E501
#         return self._index_or_cluster.explain(
#             self, doc_or_id, doc_cls=doc_cls, routing=routing, **kwargs
#         )
# 
#     def delete(
#             self, conflicts=None, refresh=None, timeout=None,
#             scroll=None, scroll_size=None,
#             wait_for_completion=None, requests_per_second=None,
#             **kwargs
#     ):
#         """Deletes all documents that match the query.
# 
#         .. note::
#            As it uses `delete by query api <https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html>`_
#            the documents that were changed between the time when a snapshot was
#            taken and when the delete request was processed won't be deleted.
#         """  # noqa:E501
#         return self._index_or_cluster.delete_by_query(
#             self,
#             conflicts=conflicts,
#             refresh=refresh,
#             timeout=timeout,
#             scroll=scroll,
#             scroll_size=scroll_size,
#             wait_for_completion=wait_for_completion,
#             requests_per_second=requests_per_second,
#             **kwargs
#         )
# 
#     def __iter__(self):
#         return self._iter_result(self.get_result())
# 
#     def __getitem__(self, k):
#         clone, is_slice = self._prepare_slice(k)
#         if is_slice:
#             return list(clone)
#         else:
#             return list(clone)[0]
# 
# Path: tests_integ/asyncio/conftest.py
# def event_loop(request):
# async def es_client(event_loop, es_url):
# def es_cluster(es_client):
# async def es_index(es_cluster, es_client, index_name):
# async def cars(es_index, car_docs):
# async def all_cars(es_index, all_car_docs):


import pytest
from elasticmagic.search import SearchQuery
from .conftest import Car
    doc = res.hits[0]
    assert doc.name == 'Lightning McQueen'
    assert doc._id == '1'
    assert doc._index == es_index.get_name()
    assert doc._score > 0
    assert doc._score == res.max_score


@pytest.mark.asyncio
async def test_count(es_index, cars):
    assert (await es_index.count()).count == 2
    assert (await es_index.count({
        "match": {
            "name": "Lightning"
        }
    })).count == 1
    assert (await es_index.count(
        SearchQuery(Car.name.match("Lightning"))
    )).count == 1


@pytest.mark.asyncio
async def test_exists(es_index, cars):
    assert (await es_index.exists()).exists is True
    assert (await es_index.exists({
        "match": {
            "name": "Lightning"
        }
    })).exists is True
    assert (await es_index.exists(

# Path: elasticmagic/search.py
# class SearchQuery(BaseSearchQuery):
#     """Elasticsearch search query construction object.
# 
#     :class:`.SearchQuery` object is usually instantiated by calling
#     :func:`Index.search_query()` method.
# 
#     See :func:`Index.search_query()` for more details.
# 
#     .. testsetup:: *
# 
#        import datetime
# 
#        from elasticmagic import SearchQuery, DynamicDocument
# 
#        class PostDocument(DynamicDocument):
#            __doc_type__ = 'post'
#     """
# 
#     def get_compiler(self):
#         return self._index_or_cluster.get_compiler()
# 
#     def get_query_compiler(self):
#         return self.get_compiler().compiled_query
# 
#     def to_dict(self, compiler=None):
#         """Compiles the query and returns python dictionary that can be
#         serialized to json.
#         """
#         compiler = compiler or self.get_compiler()
#         return compiler.compiled_query(self).body
# 
#     def get_result(self):
#         """Executes current query and returns processed :class:`SearchResult`
#         object. Caches result so subsequent calls with the same search query
#         will return cached value.
#         """
#         if self._cached_result is not None:
#             return self._cached_result
# 
#         self._cached_result = self._index_or_cluster.search(self)
#         return self._cached_result
# 
#     @property
#     def result(self):
#         warnings.warn(
#             'Field `result` is deprecated, use `get_result` method instead',
#             DeprecationWarning
#         )
#         return self.get_result()
# 
#     @property
#     def results(self):
#         warnings.warn(
#             'Field `results` is deprecated, use `get_result` method instead',
#             DeprecationWarning
#         )
#         return self.get_result()
# 
#     def count(self):
#         """Executes current query and returns number of documents matched the
#         query. Uses `count api <https://www.elastic.co/guide/en/elasticsearch/reference/current/search-count.html>`_.
#         """  # noqa:E501
#         return self._index_or_cluster.count(self).count
# 
#     def exists(self):
#         """Executes current query optimized for checking that
#         there are at least 1 matching document. This method is an analogue of
#         the old `exists search api <https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-exists.html>`_
#         For Elasticsearch 5.x and more it emulates the exists API using
#         `size=0` and `terminate_after=1`.
#         """  # noqa:E501
#         return self._index_or_cluster.exists(self).exists
# 
#     def explain(self, doc_or_id, doc_cls=None, routing=None, **kwargs):
#         """Computes a score explanation for the current search query and the document.
# 
#         :return: :class:`.result.ExplainResult`
# 
#         https://www.elastic.co/guide/en/elasticsearch/reference/current/search-explain.html
#         """  # noqa:E501
#         return self._index_or_cluster.explain(
#             self, doc_or_id, doc_cls=doc_cls, routing=routing, **kwargs
#         )
# 
#     def delete(
#             self, conflicts=None, refresh=None, timeout=None,
#             scroll=None, scroll_size=None,
#             wait_for_completion=None, requests_per_second=None,
#             **kwargs
#     ):
#         """Deletes all documents that match the query.
# 
#         .. note::
#            As it uses `delete by query api <https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html>`_
#            the documents that were changed between the time when a snapshot was
#            taken and when the delete request was processed won't be deleted.
#         """  # noqa:E501
#         return self._index_or_cluster.delete_by_query(
#             self,
#             conflicts=conflicts,
#             refresh=refresh,
#             timeout=timeout,
#             scroll=scroll,
#             scroll_size=scroll_size,
#             wait_for_completion=wait_for_completion,
#             requests_per_second=requests_per_second,
#             **kwargs
#         )
# 
#     def __iter__(self):
#         return self._iter_result(self.get_result())
# 
#     def __getitem__(self, k):
#         clone, is_slice = self._prepare_slice(k)
#         if is_slice:
#             return list(clone)
#         else:
#             return list(clone)[0]
# 
# Path: tests_integ/asyncio/conftest.py
# def event_loop(request):
# async def es_client(event_loop, es_url):
# def es_cluster(es_client):
# async def es_index(es_cluster, es_client, index_name):
# async def cars(es_index, car_docs):
# async def all_cars(es_index, all_car_docs):


# Path: tests_integ/asyncio/test_index.py
import pytest
from elasticmagic.search import SearchQuery
from .conftest import Car

    doc = res.hits[0]
    assert doc.name == 'Lightning McQueen'
    assert doc._id == '1'
    assert doc._index == es_index.get_name()
    assert doc._score > 0
    assert doc._score == res.max_score


@pytest.mark.asyncio
async def test_count(es_index, cars):
    assert (await es_index.count()).count == 2
    assert (await es_index.count({
        "match": {
            "name": "Lightning"
        }
    })).count == 1
    assert (await es_index.count(
        SearchQuery(Car.name.match("Lightning"))
    )).count == 1


@pytest.mark.asyncio
async def test_exists(es_index, cars):
    assert (await es_index.exists()).exists is True
    assert (await es_index.exists({
        "match": {
            "name": "Lightning"
        }
    })).exists is True
    assert (await es_index.exists(

        SearchQuery(Car.name.match("Mack"))