示例#1
0
    def _build_es_filter(cls, filterable_attributes=None):
        """
        This method generates a `~pyes.filters.Filter` object from the
        request.args dictionary. This is then used to refine the search.

        For example, if the query string is -:
            "/search?q=product&color=black&color=blue&size=xl"
        then the filter will be generated as follows -:
        >>> and_filter = ANDFilter(
                [
                    ORFilter(
                        [
                            TermFilter('color', 'blue'),
                            TermFilter('color', 'black')
                        ]
                    ),
                    ORFilter(
                        [
                            TermFilter('size', 'xl')
                        ]
                    )
                ]
            )
        >>> main_filter = BoolFilter().add_must(and_filter)

        If there are no filters applied in the query string, `None` is returned.
        """
        # If no filterable attributes defined in database, return None.
        if not filterable_attributes:
            return None

        # Search for the attribute name in list of filterable attributes.
        # If present (meaning it is a valid argument), add as TermFilter.
        main_filter = BoolFilter()
        and_filter_list = []

        filterable_attr_names = [x.name for x in filterable_attributes]

        for key in request.args:
            if key in filterable_attr_names:
                or_filter = ORFilter([
                    TermFilter(key, value)
                    for value in request.args.getlist(key)
                ])
                and_filter_list.append(or_filter)

        # If no filterable attributes were found in query string
        if not and_filter_list:
            return None

        and_filter = ANDFilter(and_filter_list)
        main_filter.add_must(and_filter)

        return main_filter
示例#2
0
    def test_OR_AND_Filters(self):
        q1 = TermFilter("position", 1)
        q2 = TermFilter("position", 2)
        andq = ANDFilter([q1, q2])

        q = FilteredQuery(MatchAllQuery(), andq)
        resultset = self.conn.search(query=q, indices=self.index_name)
        self.assertEquals(resultset.total, 0)
        self.assertEquals(
            q,
            FilteredQuery(
                MatchAllQuery(),
                ANDFilter(
                    [TermFilter("position", 1),
                     TermFilter("position", 2)])))
        self.assertNotEquals(
            q,
            FilteredQuery(
                MatchAllQuery(),
                ANDFilter(
                    [TermFilter("position", 1),
                     TermFilter("position", 3)])))

        orq = ORFilter([q1, q2])
        q = FilteredQuery(MatchAllQuery(), orq)
        resultset = self.conn.search(query=q, indices=self.index_name)
        self.assertEquals(resultset.total, 2)
        self.assertEquals(
            q,
            FilteredQuery(
                MatchAllQuery(),
                ORFilter(
                    [TermFilter("position", 1),
                     TermFilter("position", 2)])))
        self.assertNotEquals(
            q,
            FilteredQuery(
                MatchAllQuery(),
                ORFilter(
                    [TermFilter("position", 1),
                     TermFilter("position", 3)])))
示例#3
0
    def test_OR_AND_Filters(self):
        q1 = TermFilter("position", 1)
        q2 = TermFilter("position", 2)
        andq = ANDFilter([q1, q2])

        q = FilteredQuery(MatchAllQuery(), andq)
        result = self.conn.search(query=q, indexes=["test-index"])
        self.assertEquals(result['hits']['total'], 0)

        orq = ORFilter([q1, q2])
        q = FilteredQuery(MatchAllQuery(), orq)
        result = self.conn.search(query=q, indexes=["test-index"])
        self.assertEquals(result['hits']['total'], 2)
示例#4
0
  parser = argparse.ArgumentParser(description='met à jour le type des annonces en base')
  parser.add_argument('--test', const=True, action='store_const', help='affiche les annonces mises à jour sans les stocker en base')
  parser.add_argument('--all', const=True, action='store_const', help='met à jour toutes les annonces, et pas seulement celles associées à aucun type')
  parser.add_argument('--term', action='store', help='met à jour uniquement les annonces correspondant au terme précisé')

  args = parser.parse_args()
  
  previous_total = -1
  total = 0
  while previous_total != total:
    # That's odd but it seems we do not update all
    # found pubs... so adding this while loop :(
    previous_total = total
    if args.all: pubs = get_pubs(filter=MatchAllFilter())
    elif args.term: 
      filter=ORFilter([TermFilter(field="description", value=args.term),
                       TermFilter(field="location", value=args.term)])
      pubs = get_pubs(filter=filter)
    else: pubs = get_pubs()

    count = 0
    total = pubs.total
    log('OK', str(total) + ' pubs to update')
    for pub in pubs:
      search_for_types(pub)
      if args.test: show_pub(pub)
      else: insert_to_db(pub)
      count = count + 1
      if count % 20 == 0:
        log('OK', str(count) + ' / ' + str(total) + ' updated')