Пример #1
0
 def index(self):
     dbutil.get_es_conn(self.request).index(self._get_es_document(), dbutil.get_es_index_name(self.request), self._get_es_doctype(), str(self._id))
Пример #2
0
 def unindex(self):
     try:
         dbutil.get_es_conn(self.request).delete(dbutil.get_es_index_name(self.request), self._get_es_doctype(), str(self._id))
     except pyes.exceptions.NotFoundException, e:
         pass
Пример #3
0
    def search_raw(self, fulltext=None, title=None, description=None, __name__=None, _object_type=None, _pub_state=None, path_id=None, start=0, size=10, fields=None, highlight_fields=None, viewable_only=False, default_operator='AND', sort=None):
        """
        fulltext, title and description should be query strings and may contain
        boolean operators and wildcards

        __name__, _object_type and _pub_state should be either a string or sequence of strings (with OR logic implied) and must be exact matches (no wildcards)

        path_id should be either an ObjectId or a sequence of ObjectIds
        identifying one or more portions of the site to restrict the search to

        sort should be a pyes-style sort string, in other words a comma-delimited list of field names each with the options suffix ":asc" or ":desc"
        (example: "_object_type,_created:desc")

        Returns a pyes result dictionary.
        Keys are [u'hits', u'_shards', u'took', u'timed_out'].
        result['hits'] has the keys: [u'hits', u'total', u'max_score']
        
        result['took'] -> search time in ms
        result['hits']['total'] -> total number of hits
        result['hits']['hits'] -> list of hit dictionaries, each with the keys: [u'_score', u'_type', u'_id', u'_source', u'_index', u'highlight']
        Although if the fields argument is a list of field names (instead 
        of the default value None), instead of a '_source' key, each hit will
        have a '_fields' key whose value is a dictionary of the requested fields.
        
        The "highlight" key will only be present if highlight_fields were used
        and there was a match in at least one of those fields.
        In that case, the value of "highlight" will be dictionary of strings.
        Each dictionary key is a field name and each string is an HTML fragment
        where the matched term is in an <em> tag.
        """
        # Convert singleton values to lists
        if __name__ and (type(__name__) in (str, unicode)):
            __name__ = [__name__]
        if _object_type and (type(_object_type) in (str, unicode)):
            _object_type = [_object_type]
        if _pub_state and (type(_pub_state) in (str, unicode)):
            _pub_state = [_pub_state]
        if type(path_id) == ObjectId:
            path_id = [path_id]

        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')