Example #1
0
 def test_get_facet_field_name(self):
     self.assertEqual(get_facet_field_name("id"), "id")
     self.assertEqual(get_facet_field_name("django_id"), "django_id")
     self.assertEqual(get_facet_field_name("django_ct"), "django_ct")
     self.assertEqual(get_facet_field_name("author"), "author_exact")
     self.assertEqual(get_facet_field_name("author_exact"),
                      "author_exact_exact")
 def test_get_facet_field_name(self):
     self.assertEqual(get_facet_field_name('id'), 'id')
     self.assertEqual(get_facet_field_name('django_id'), 'django_id')
     self.assertEqual(get_facet_field_name('django_ct'), 'django_ct')
     self.assertEqual(get_facet_field_name('author'), 'author_exact')
     self.assertEqual(get_facet_field_name('author_exact'),
                      'author_exact_exact')
Example #3
0
 def build_schema(self, fields):
     content_field_name = ''
     schema_fields = []
     
     for field_name, field_class in fields.items():
         field_data = {
             'field_name': field_class.index_fieldname,
             'type': 'text',
             'indexed': 'true',
             'stored': 'true',
             'multi_valued': 'false',
         }
         
         if field_class.document is True:
             content_field_name = field_class.index_fieldname
         
         # DRL_FIXME: Perhaps move to something where, if none of these
         #            checks succeed, call a custom method on the form that
         #            returns, per-backend, the right type of storage?
         # DRL_FIXME: Also think about removing `isinstance` and replacing
         #            it with a method call/string returned (like 'text' or
         #            'date').
         if isinstance(field_class, (DateField, DateTimeField)):
             field_data['type'] = 'date'
         elif isinstance(field_class, IntegerField):
             field_data['type'] = 'slong'
         elif isinstance(field_class, FloatField):
             field_data['type'] = 'sfloat'
         elif isinstance(field_class, BooleanField):
             field_data['type'] = 'boolean'
         elif isinstance(field_class, MultiValueField):
             field_data['multi_valued'] = 'true'
         
         if field_class.stored is False:
             field_data['stored'] = 'false'
         
         # Do this last to override `text` fields.
         if field_class.indexed is False:
             field_data['indexed'] = 'false'
             
             # If it's text and not being indexed, we probably don't want
             # to do the normal lowercase/tokenize/stemming/etc. dance.
             if field_data['type'] == 'text':
                 field_data['type'] = 'string'
         
         schema_fields.append(field_data)
         
         if field_class.faceted is True:
             # Duplicate the field.
             faceted_field = field_data.copy()
             faceted_field['field_name'] = get_facet_field_name(faceted_field['field_name'])
             
             # If it's text, it ought to be a string.
             if faceted_field['type'] == 'text':
                 faceted_field['type'] = 'string'
             
             schema_fields.append(faceted_field)
     
     return (content_field_name, schema_fields)
    def __new__(cls, name, bases, attrs):
        attrs['fields'] = {}

        # Inherit any fields from parent(s).
        try:
            parents = [b for b in bases if issubclass(b, SearchIndex)]
            # Simulate the MRO.
            parents.reverse()

            for p in parents:
                fields = getattr(p, 'fields', None)

                if fields:
                    attrs['fields'].update(fields)
        except NameError:
            pass

        # Build a dictionary of faceted fields for cross-referencing.
        facet_fields = {}

        for field_name, obj in attrs.items():
            # Only need to check the FacetFields.
            if hasattr(obj, 'facet_for'):
                if not obj.facet_for in facet_fields:
                    facet_fields[obj.facet_for] = []

                facet_fields[obj.facet_for].append(field_name)

        for field_name, obj in attrs.items():
            if isinstance(obj, SearchField):
                field = attrs.pop(field_name)
                field.set_instance_name(field_name)
                attrs['fields'][field_name] = field

                # Only check non-faceted fields for the following info.
                if not hasattr(field, 'facet_for'):
                    if field.faceted == True:
                        # If no other field is claiming this field as
                        # ``facet_for``, create a shadow ``FacetField``.
                        if not field_name in facet_fields:
                            shadow_facet_name = get_facet_field_name(
                                field_name)
                            shadow_facet_field = field.facet_class(
                                facet_for=field_name)
                            shadow_facet_field.set_instance_name(
                                shadow_facet_name)
                            attrs['fields'][
                                shadow_facet_name] = shadow_facet_field

        # Assigning default 'objects' query manager if it does not already exist
        if not attrs.has_key('objects'):
            try:
                attrs['objects'] = SearchIndexManager(
                    attrs['Meta'].index_label)
            except (KeyError, AttributeError):
                attrs['objects'] = SearchIndexManager(DEFAULT_ALIAS)

        return super(DeclarativeMetaclass,
                     cls).__new__(cls, name, bases, attrs)
Example #5
0
    def __new__(cls, name, bases, attrs):
        attrs["fields"] = {}

        # Inherit any fields from parent(s).
        try:
            parents = [b for b in bases if issubclass(b, SearchIndex)]
            # Simulate the MRO.
            parents.reverse()

            for p in parents:
                fields = getattr(p, "fields", None)

                if fields:
                    attrs["fields"].update(fields)
        except NameError:
            pass

        # Build a dictionary of faceted fields for cross-referencing.
        facet_fields = {}

        for field_name, obj in attrs.items():
            # Only need to check the FacetFields.
            if hasattr(obj, "facet_for"):
                if not obj.facet_for in facet_fields:
                    facet_fields[obj.facet_for] = []

                facet_fields[obj.facet_for].append(field_name)

        built_fields = {}

        for field_name, obj in attrs.items():
            if isinstance(obj, SearchField):
                field = attrs[field_name]
                field.set_instance_name(field_name)
                built_fields[field_name] = field

                # Only check non-faceted fields for the following info.
                if not hasattr(field, "facet_for"):
                    if field.faceted == True:
                        # If no other field is claiming this field as
                        # ``facet_for``, create a shadow ``FacetField``.
                        if not field_name in facet_fields:
                            shadow_facet_name = get_facet_field_name(field_name)
                            shadow_facet_field = field.facet_class(facet_for=field_name)
                            shadow_facet_field.set_instance_name(shadow_facet_name)
                            built_fields[shadow_facet_name] = shadow_facet_field

        attrs["fields"].update(built_fields)

        # Assigning default 'objects' query manager if it does not already exist
        if not "objects" in attrs:
            try:
                attrs["objects"] = SearchIndexManager(attrs["Meta"].index_label)
            except (KeyError, AttributeError):
                attrs["objects"] = SearchIndexManager(DEFAULT_ALIAS)

        return super(DeclarativeMetaclass, cls).__new__(cls, name, bases, attrs)
Example #6
0
 def full_prepare(self, obj):
     self.prepared_data = self.prepare(obj)
     
     # Duplicate data for faceted fields.
     for field_name, field in self.fields.items():
         if field.faceted is True and field.index_fieldname in self.prepared_data:
             self.prepared_data[get_facet_field_name(field.index_fieldname)] = self.prepared_data[field.index_fieldname]
     
     return self.prepared_data
Example #7
0
    def build_schema(self, fields):
        """
        Build the schema from fields.
        
        Required arguments:
            ``fields`` -- A list of fields in the index
        
        Returns a list of fields in dictionary format ready for inclusion in
        an indexed meta-data.
        """
        content_field_name = ''
        schema_fields = []
        column = 0
        
        for field_name, field_class in fields.items():
            if field_class.document is True:
                content_field_name = field_class.index_fieldname
            
            if field_class.indexed is True:
                field_data = {
                    'field_name': field_class.index_fieldname,
                    'type': 'text',
                    'multi_valued': 'false',
                    'column': column,
                }

                # Extra fields
                if field_class.extra.get('weight'):
                    field_data['weight'] = field_class.extra.get('weight')

                if isinstance(field_class, (DateField, DateTimeField)):
                    field_data['type'] = 'date'
                elif isinstance(field_class, IntegerField):
                    field_data['type'] = 'long'
                elif isinstance(field_class, FloatField):
                    field_data['type'] = 'float'
                elif isinstance(field_class, BooleanField):
                    field_data['type'] = 'boolean'
                elif isinstance(field_class, MultiValueField):
                    field_data['multi_valued'] = 'true'
                
                schema_fields.append(field_data)
                column += 1

                if field_class.faceted is True:
                    # Duplicate the field.
                    faceted_field = field_data.copy()
                    faceted_field['field_name'] = get_facet_field_name(faceted_field['field_name'])
                    faceted_field['column'] = column

                    schema_fields.append(faceted_field)
                    column += 1
        
        return (content_field_name, schema_fields)
Example #8
0
 def add_date_facet(self, field, start_date, end_date, gap_by, gap_amount=1):
     """Adds a date-based facet on a field."""
     if not gap_by in VALID_GAPS:
         raise FacetingError("The gap_by ('%s') must be one of the following: %s." % (gap_by, ', '.join(VALID_GAPS)))
     
     details = {
         'start_date': start_date,
         'end_date': end_date,
         'gap_by': gap_by,
         'gap_amount': gap_amount,
     }
     self.date_facets[get_facet_field_name(field)] = details
    def build_schema(self, fields):
        """
        Build the schema from fields.
        
        Required arguments:
            ``fields`` -- A list of fields in the index
        
        Returns a list of fields in dictionary format ready for inclusion in
        an indexed meta-data.
        """
        content_field_name = ""
        schema_fields = []
        column = 0

        for field_name, field_class in fields.items():
            if field_class.document is True:
                content_field_name = field_class.index_fieldname

            if field_class.indexed is True:
                field_data = {
                    "field_name": field_class.index_fieldname,
                    "type": "text",
                    "multi_valued": "false",
                    "column": column,
                }

                if isinstance(field_class, (DateField, DateTimeField)):
                    field_data["type"] = "date"
                elif isinstance(field_class, IntegerField):
                    field_data["type"] = "long"
                elif isinstance(field_class, FloatField):
                    field_data["type"] = "float"
                elif isinstance(field_class, BooleanField):
                    field_data["type"] = "boolean"
                elif isinstance(field_class, MultiValueField):
                    field_data["multi_valued"] = "true"

                schema_fields.append(field_data)
                column += 1

                if field_class.faceted is True:
                    # Duplicate the field.
                    faceted_field = field_data.copy()
                    faceted_field["field_name"] = get_facet_field_name(faceted_field["field_name"])
                    faceted_field["column"] = column

                    schema_fields.append(faceted_field)
                    column += 1

        return (content_field_name, schema_fields)
Example #10
0
    def __new__(cls, name, bases, attrs):
        attrs['fields'] = {}

        # Inherit any fields from parent(s).
        try:
            parents = [b for b in bases if issubclass(b, SearchIndex)]

            for p in parents:
                fields = getattr(p, 'fields', None)

                if fields:
                    attrs['fields'].update(fields)
        except NameError:
            pass

        # Build a dictionary of faceted fields for cross-referencing.
        facet_fields = {}

        for field_name, obj in attrs.items():
            # Only need to check the FacetFields.
            if hasattr(obj, 'facet_for'):
                if not obj.facet_for in facet_fields:
                    facet_fields[obj.facet_for] = []

                facet_fields[obj.facet_for].append(field_name)

        for field_name, obj in attrs.items():
            if isinstance(obj, SearchField):
                field = attrs.pop(field_name)
                field.set_instance_name(field_name)
                attrs['fields'][field_name] = field

                # Only check non-faceted fields for the following info.
                if not hasattr(field, 'facet_for'):
                    if field.faceted == True:
                        # If no other field is claiming this field as
                        # ``facet_for``, create a shadow ``FacetField``.
                        if not field_name in facet_fields:
                            shadow_facet_name = get_facet_field_name(
                                field_name)
                            shadow_facet_field = field.facet_class(
                                facet_for=field_name)
                            shadow_facet_field.set_instance_name(
                                shadow_facet_name)
                            attrs['fields'][
                                shadow_facet_name] = shadow_facet_field

        return super(DeclarativeMetaclass,
                     cls).__new__(cls, name, bases, attrs)
Example #11
0
    def __new__(cls, name, bases, attrs):
        attrs['fields'] = {}

        # Inherit any fields from parent(s).
        try:
            parents = [b for b in bases if issubclass(b, SearchIndex)]
            # Simulate the MRO.
            parents.reverse()

            for p in parents:
                fields = getattr(p, 'fields', None)

                if fields:
                    attrs['fields'].update(fields)
        except NameError:
            pass

        # Build a dictionary of faceted fields for cross-referencing.
        facet_fields = {}

        for field_name, obj in attrs.items():
            # Only need to check the FacetFields.
            if hasattr(obj, 'facet_for'):
                if not obj.facet_for in facet_fields:
                    facet_fields[obj.facet_for] = []

                facet_fields[obj.facet_for].append(field_name)

        for field_name, obj in attrs.items():
            if isinstance(obj, SearchField):
                field = attrs.pop(field_name)
                field.set_instance_name(field_name)
                attrs['fields'][field_name] = field

                # Only check non-faceted fields for the following info.
                if not hasattr(field, 'facet_for'):
                    if field.faceted == True:
                        # If no other field is claiming this field as
                        # ``facet_for``, create a shadow ``FacetField``.
                        if not field_name in facet_fields:
                            shadow_facet_name = get_facet_field_name(field_name)
                            shadow_facet_field = field.facet_class(facet_for=field_name)
                            shadow_facet_field.set_instance_name(shadow_facet_name)
                            attrs['fields'][shadow_facet_name] = shadow_facet_field

        return super(DeclarativeMetaclass, cls).__new__(cls, name, bases, attrs)
Example #12
0
 def test_get_facet_field_name(self):
     self.assertEqual(get_facet_field_name('id'), 'id')
     self.assertEqual(get_facet_field_name('django_id'), 'django_id')
     self.assertEqual(get_facet_field_name('django_ct'), 'django_ct')
     self.assertEqual(get_facet_field_name('author'), 'author_exact')
     self.assertEqual(get_facet_field_name('author_exact'), 'author_exact_exact')
Example #13
0
 def add_query_facet(self, field, query):
     """Adds a query facet on a field."""
     self.query_facets.append((get_facet_field_name(field), query))
Example #14
0
 def add_field_facet(self, field):
     """Adds a regular facet on a field."""
     self.facets.add(get_facet_field_name(field))
Example #15
0
 def test_get_facet_field_name(self):
     self.assertEqual(get_facet_field_name("id"), "id")
     self.assertEqual(get_facet_field_name("django_id"), "django_id")
     self.assertEqual(get_facet_field_name("django_ct"), "django_ct")
     self.assertEqual(get_facet_field_name("author"), "author_exact")
     self.assertEqual(get_facet_field_name("author_exact"), "author_exact_exact")