# Path: cms/dbutil.py
# def get_mongodb(request):
# def get_collection(request, name):
# def get_gridfs(request, collection='fs'):
# def encode_keys(d):
# def get_es_conn(request):
# def get_es_index_name(request):
# def serve_gridfs_file(file):
# def serve_gridfs_file_for_id(request, id, collection='fs'):


from folder import Folder
from article import Article
from pyramid import security
from bson.objectid import ObjectId
from cms import dbutil
from users import UserCollection, GroupCollection, User, generate_random_password
from trash import Trash
import permissions
import pyes

        query = pyes.MatchAllQuery()
        if fulltext or title or description:
            query = pyes.BoolQuery()
            if fulltext: query.add_must(pyes.StringQuery(fulltext, default_operator=default_operator))
            if title: query.add_must(pyes.StringQuery(title, search_fields=['title'], default_operator=default_operator))
            if description: query.add_must(pyes.StringQuery(description, search_fields=['description'], default_operator=default_operator))

        filters = []

        if __name__:
            filters.append(pyes.TermsFilter('__name__', __name__))
        if _object_type:
            filters.append(pyes.TermsFilter('_object_type', _object_type))
        if _pub_state:
            filters.append(pyes.TermsFilter('_pub_state', _pub_state))
        if path_id:
            # Convert ObjectIds to strings
            filters.append(pyes.TermsFilter('_id_path', [str(x) for x in path_id]))
        if viewable_only:
            filters.append(pyes.TermsFilter('_view', security.effective_principals(self.request)))

        if filters:
            query = pyes.FilteredQuery(query, pyes.ANDFilter(filters))

        search = pyes.Search(query=query, start=start, size=size, fields=fields)
        if highlight_fields:
            for field in highlight_fields:
                search.add_highlight(field)
        # FIXME: use new search() method???

# Path: cms/dbutil.py
# def get_mongodb(request):
# def get_collection(request, name):
# def get_gridfs(request, collection='fs'):
# def encode_keys(d):
# def get_es_conn(request):
# def get_es_index_name(request):
# def serve_gridfs_file(file):
# def serve_gridfs_file_for_id(request, id, collection='fs'):


# Path: cms/resources/root.py
from folder import Folder
from article import Article
from pyramid import security
from bson.objectid import ObjectId
from cms import dbutil
from users import UserCollection, GroupCollection, User, generate_random_password
from trash import Trash
import permissions
import pyes


        query = pyes.MatchAllQuery()
        if fulltext or title or description:
            query = pyes.BoolQuery()
            if fulltext: query.add_must(pyes.StringQuery(fulltext, default_operator=default_operator))
            if title: query.add_must(pyes.StringQuery(title, search_fields=['title'], default_operator=default_operator))
            if description: query.add_must(pyes.StringQuery(description, search_fields=['description'], default_operator=default_operator))

        filters = []

        if __name__:
            filters.append(pyes.TermsFilter('__name__', __name__))
        if _object_type:
            filters.append(pyes.TermsFilter('_object_type', _object_type))
        if _pub_state:
            filters.append(pyes.TermsFilter('_pub_state', _pub_state))
        if path_id:
            # Convert ObjectIds to strings
            filters.append(pyes.TermsFilter('_id_path', [str(x) for x in path_id]))
        if viewable_only:
            filters.append(pyes.TermsFilter('_view', security.effective_principals(self.request)))

        if filters:
            query = pyes.FilteredQuery(query, pyes.ANDFilter(filters))

        search = pyes.Search(query=query, start=start, size=size, fields=fields)
        if highlight_fields:
            for field in highlight_fields:
                search.add_highlight(field)
        # FIXME: use new search() method???

        return dbutil.get_es_conn(self.request).search_raw(search, dbutil.get_es_index_name(self.request), sort=sort or '_score')