Exemple #1
0
 def setUp(self):
     self.parser = l2cs.make_parser(int_fields=["count", "number"],
                                    yesno_fields=["active", "ready"],
                                    aliases={"alias": ["alias1", "alias2"]})
     self.schema = l2cs.make_schema(["foo", "bar", "baz", "count", "number",
                                     "active", "text", "ready", "active",
                                     "alias", "alias1", "alias2"],
                                    ["timestamp", "date"])
     self.schema_parser = l2cs.make_parser(int_fields=["count", "number"],
                                           yesno_fields=["active", "ready"],
                                           aliases={"alias": ["alias1",
                                                              "alias2"]},
                                           schema=self.schema)
Exemple #2
0
 def setUp(self):
     self.parser = l2cs.make_parser(int_fields=["count", "number"],
                                    yesno_fields=["active", "ready"],
                                    aliases={"alias": ["alias1", "alias2"]})
     self.schema = l2cs.make_schema([
         "foo", "bar", "baz", "count", "number", "active", "text", "ready",
         "active", "alias", "alias1", "alias2"
     ], ["timestamp", "date"])
     self.schema_parser = l2cs.make_parser(
         int_fields=["count", "number"],
         yesno_fields=["active", "ready"],
         aliases={"alias": ["alias1", "alias2"]},
         schema=self.schema)
Exemple #3
0
class LinkSearchQuery(CloudSearchQuery):
    search_api = g.CLOUDSEARCH_SEARCH_API
    sorts = {
        'relevance': '-relevance',
        'hot': '-hot2',
        'top': '-top',
        'new': '-timestamp',
        'comments': '-num_comments',
    }
    sorts_menu_mapping = {
        'relevance': 1,
        'hot': 2,
        'new': 3,
        'top': 4,
        'comments': 5,
    }
    recents = {
        'hour': timedelta(hours=1),
        'day': timedelta(days=1),
        'week': timedelta(days=7),
        'month': timedelta(days=31),
        'year': timedelta(days=366),
        'all': None,
        None: None,
    }
    schema = l2cs.make_schema(LinkFields.lucene_fieldnames())
    lucene_parser = l2cs.make_parser(
        int_fields=LinkFields.lucene_fieldnames(type_=int),
        yesno_fields=LinkFields.lucene_fieldnames(type_="yesno"),
        schema=schema)
    known_syntaxes = ("cloudsearch", "lucene", "plain")
    default_syntax = "lucene"

    def customize_query(self, bq=u''):
        queries = []
        if bq:
            queries = [bq]
        subreddit_query = self._get_sr_restriction(self.sr)
        if subreddit_query:
            queries.append(subreddit_query)
        if self.recent:
            recent_query = self._restrict_recent(self.recent)
            queries.append(recent_query)
        if not self.include_over18:
            queries.append('over18:0')
        return self.create_boolean_query(queries)

    @staticmethod
    def _restrict_recent(recent):
        now = datetime.now(g.tz)
        since = epoch_seconds(now - recent)
        return 'timestamp:%i..' % since

    @staticmethod
    def _get_sr_restriction(sr):
        '''Return a cloudsearch appropriate query string that restricts
        results to only contain results from self.sr
        
        '''
        bq = []
        if (not sr) or sr == All or isinstance(sr, DefaultSR):
            return None
        elif isinstance(sr, MultiReddit):
            bq = ["(or"]
            for sr_id in sr.sr_ids:
                bq.append("sr_id:%s" % sr_id)
            bq.append(")")
        elif isinstance(sr, DomainSR):
            bq = ["site:'%s'" % sr.domain]
        elif sr == Friends:
            if not c.user_is_loggedin or not c.user.friends:
                return None
            bq = ["(or"]
            # The query limit is roughly 8k bytes. Limit to 200 friends to
            # avoid getting too close to that limit
            friend_ids = c.user.friends[:200]
            friends = [
                "author_fullname:'%s'" %
                Account._fullname_from_id36(r2utils.to36(id_))
                for id_ in friend_ids
            ]
            bq.extend(friends)
            bq.append(")")
        elif isinstance(sr, ModContribSR):
            bq = ["(or"]
            for sr_id in sr.sr_ids:
                bq.append("sr_id:%s" % sr_id)
            bq.append(")")
        elif not isinstance(sr, FakeSubreddit):
            bq = ["sr_id:%s" % sr._id]

        return ' '.join(bq)
Exemple #4
0
class LinkSearchQuery(CloudSearchQuery):
    search_api = g.CLOUDSEARCH_SEARCH_API
    sorts = {
        'relevance': '-relevance',
        'relevance2': '-relevance2',
        'hot': '-hot2',
        'top': '-top',
        'new': '-timestamp',
        'comments': '-num_comments',
    }
    recents = {
        'hour': timedelta(hours=1),
        'day': timedelta(days=1),
        'week': timedelta(days=7),
        'month': timedelta(days=31),
        'year': timedelta(days=366),
        'all': None,
        None: None,
    }
    schema = l2cs.make_schema(LinkFields.lucene_fieldnames())
    lucene_parser = l2cs.make_parser(
        int_fields=LinkFields.lucene_fieldnames(type_=int),
        yesno_fields=LinkFields.lucene_fieldnames(type_="yesno"),
        schema=schema)
    known_syntaxes = g.search_syntaxes
    default_syntax = "lucene"

    def customize_query(self, bq=u''):
        queries = []
        if bq:
            queries = [bq]
        if self.sr:
            subverbify_query = self._restrict_sr(self.sr)
            if subverbify_query:
                queries.append(subverbify_query)
        if self.recent:
            recent_query = self._restrict_recent(self.recent)
            queries.append(recent_query)
        if not self.include_over18:
            queries.append('over18:0')
        return self.create_boolean_query(queries)

    @staticmethod
    def _restrict_recent(recent):
        now = datetime.now(g.tz)
        since = epoch_seconds(now - recent)
        return 'timestamp:%i..' % since

    @staticmethod
    def _restrict_sr(sr):
        '''Return a cloudsearch appropriate query string that restricts
        results to only contain results from sr
        
        '''
        if isinstance(sr, MultiVerbify):
            if not sr.sr_ids:
                raise InvalidQuery
            srs = ["sr_id:%s" % sr_id for sr_id in sr.sr_ids]
            return "(or %s)" % ' '.join(srs)
        elif isinstance(sr, DomainSR):
            return "site:'\"%s\"'" % sr.domain
        elif isinstance(sr, FriendsSR):
            if not c.user_is_loggedin or not c.user.friends:
                raise InvalidQuery
            # The query limit is roughly 8k bytes. Limit to 200 friends to
            # avoid getting too close to that limit
            friend_ids = c.user.friends[:200]
            friends = [
                "author_fullname:'%s'" %
                Account._fullname_from_id36(v1utils.to36(id_))
                for id_ in friend_ids
            ]
            return "(or %s)" % ' '.join(friends)
        elif isinstance(sr, AllMinus):
            if not sr.exclude_sr_ids:
                raise InvalidQuery
            exclude_srs = ["sr_id:%s" % sr_id for sr_id in sr.exclude_sr_ids]
            return "(not (or %s))" % ' '.join(exclude_srs)
        elif not isinstance(sr, FakeSubverbify):
            return "sr_id:%s" % sr._id

        return None
Exemple #5
0
class LinkSearchQuery(CloudSearchQuery):
    search_api = g.CLOUDSEARCH_SEARCH_API
    sorts = {
        'relevance': '-relevance',
        'hot': '-hot2',
        'top': '-top',
        'new': '-timestamp',
        'comments': '-num_comments',
    }
    sorts_menu_mapping = {
        'relevance': 1,
        'hot': 2,
        'new': 3,
        'top': 4,
        'comments': 5,
    }

    schema = l2cs.make_schema(LinkFields.lucene_fieldnames())
    lucene_parser = l2cs.make_parser(
        int_fields=LinkFields.lucene_fieldnames(type_=int),
        yesno_fields=LinkFields.lucene_fieldnames(type_="yesno"),
        schema=schema)
    known_syntaxes = ("cloudsearch", "lucene", "plain")
    default_syntax = "lucene"

    def customize_query(self, bq):
        subreddit_query = self._get_sr_restriction(self.sr)
        return self.create_boolean_query(bq, subreddit_query)

    @classmethod
    def create_boolean_query(cls, query, subreddit_query):
        '''Join a (user-entered) text query with the generated subreddit query
        
        Input:
            base_query: user input from the search textbox
            subreddit_query: output from _get_sr_restriction(sr)
        
        Test cases:
            base_query: simple, simple with quotes, boolean, boolean w/ parens
            subreddit_query: None, in parens '(or sr_id:1 sr_id:2 ...)',
                             without parens "author:'foo'"
        
        '''
        if subreddit_query:
            bq = "(and %s %s)" % (query, subreddit_query)
        else:
            bq = query
        return bq

    @staticmethod
    def _get_sr_restriction(sr):
        '''Return a cloudsearch appropriate query string that restricts
        results to only contain results from self.sr
        
        '''
        bq = []
        if (not sr) or sr == All or isinstance(sr, DefaultSR):
            return None
        elif isinstance(sr, MultiReddit):
            bq = ["(or"]
            for sr_id in sr.sr_ids:
                bq.append("sr_id:%s" % sr_id)
            bq.append(")")
        elif isinstance(sr, DomainSR):
            bq = ["site:'%s'" % sr.domain]
        elif sr == Friends:
            if not c.user_is_loggedin or not c.user.friends:
                return None
            bq = ["(or"]
            # The query limit is roughly 8k bytes. Limit to 200 friends to
            # avoid getting too close to that limit
            friend_ids = c.user.friends[:200]
            friends = [
                "author_fullname:'%s'" %
                Account._fullname_from_id36(r2utils.to36(id_))
                for id_ in friend_ids
            ]
            bq.extend(friends)
            bq.append(")")
        elif isinstance(sr, ModContribSR):
            bq = ["(or"]
            for sr_id in sr.sr_ids:
                bq.append("sr_id:%s" % sr_id)
            bq.append(")")
        elif not isinstance(sr, FakeSubreddit):
            bq = ["sr_id:%s" % sr._id]

        return ' '.join(bq)
Exemple #6
0
 def setUp(self):
     self.parser = l2cs.make_parser(int_fields=["count", "number"],
                                    yesno_fields=["active", "ready"],
                                    aliases={"alias": ["alias1", "alias2"]})