Example #1
0
    def basic_fulltext_search(self, search_string='', collection_names=None, skip=0, limit=10, sort=None, highlight_fields=None, object_fields=None):
        """ A functional basic full text search.
        Also a good example of using the other search methods.

        All parms are optional. Calling the method without specifying any parms
        queries for anything and everything.

        :param query: a query string that may contain wildcards or boolean operators
        :type query: string
        :param collection_names: restrict search to specific Collections
        :type collection_names: list of strings, or ``None``
        :param skip: number of results to omit from start of result set
        :type skip: integer
        :param limit: maximum number of results to return
        :type limit: integer
        :param sort: a :class:`audrey.sortutil.SortSpec` string; default sort is by relevance
        :type sort: string or ``None``
        :param highlight_fields: a list of Elastic mapping fields in which to highlight ``search_string`` matches. For example, to highlight matches in Audrey's default full "text" field: ``['text']``
        :type highlight_fields: list of strings, or ``None``
        :param object_fields: like ``fields`` param to :meth:`audrey.resources.collection.Collection.get_children`)
        :rtype: dictionary

        Returns a dictionary like :meth:`get_objects_and_highlights_for_raw_search_results` when ``highlight_fields``.  Otherwise returns a dictionary like :meth:`get_objects_for_raw_search_results`.
        """
        search_string = search_string.strip()
        if search_string:
            query = pyes.StringQuery(search_string)
        else:
            query = pyes.MatchAllQuery()
        # Set fields=[] since we only need _id and _type (which are always
        # in Elastic results) to get the objects out of MongoDB.
        # Retrieving _source would just waste resources.
        search = pyes.Search(query=query, fields=[], start=skip, size=limit)
        if highlight_fields:
            for hf in highlight_fields:
                search.add_highlight(hf)
        elastic_sort = sort and sortutil.sort_string_to_elastic(sort) or None
        method = highlight_fields and self.get_objects_and_highlights_for_query or self.get_objects_for_query
        return method(query=search, doc_types=collection_names, sort=elastic_sort, object_fields=object_fields)
Example #2
0
 def test_sortutil(self):
     from audrey import sortutil
     self.assertTrue(sortutil.sort_string_to_mongo('foo,-bar,+baz'), [('foo', 1), ('bar', -1), ('baz', 1)])
     self.assertTrue(sortutil.sort_string_to_elastic('foo,-bar,+baz'), 'foo,bar:desc,baz'), 
     self.assertTrue(sortutil.SortSpec('foo,-bar,+baz').to_string(pluses=True), '+foo,-bar,+baz')
     self.assertTrue(str(sortutil.SortSpec('foo,-bar,+baz')), 'foo,-bar,baz')