Esempio n. 1
0
            class CarDocument(Document):
                color = fields.TextField()
                name = fields.TextField()

                class Django:
                    fields = ["name"]
                    model = Car
Esempio n. 2
0
class BookDocument(Document):

    category = fields.ObjectField(properties={'name': fields.TextField()})

    class Index:
        name = 'books'

    class Django:
        model = Book
        fields = ['title', 'description', 'created']
        related_models = [Category]

    def get_queryset(self):
        """Select related in sql requests to improve performance"""
        return super(BookDocument,
                     self).get_queryset().select_related('category')

    def get_instances_from_related(self, related_instance):
        """If related_models is set, define how to retrieve the Category instance(s) from the related model.
        Use related_models with caution, it cause the index to updating a lot of items.
        """
        if isinstance(related_instance, Category):
            return related_instance.books.all()
        elif isinstance(related_instance, Author):
            return related_instance.books
Esempio n. 3
0
class CarDocument(Document):
    color = fields.TextField()
    type = fields.TextField()

    def prepare_color(self, instance):
        return "blue"

    class Meta:
        doc_type = "car_document"

    class Django:
        fields = ["name", "price"]
        model = Car
        related_models = [Manufacturer]

    class Index:
        name = "car_index"
        doc_type = "car_document"
Esempio n. 4
0
class AuthorDocument(Document):

    books = fields.NestedField(
        properties={
            'title': fields.TextField(),
            'description': fields.TextField(),
            'created': fields.TextField(),
            'category': fields.ObjectField(
                properties={'name': fields.TextField()})
        })

    class Index:
        name = 'authors'
        related_models = [Book]

    class Django:
        model = Author
        fields = ['first_name', 'last_name']
class CarWithPrepareDocument(Document):
    manufacturer = fields.ObjectField(properties={
        "name": fields.TextField(),
        "country": fields.TextField(),
    })

    manufacturer_short = fields.ObjectField(properties={
        "name": fields.TextField(),
    })

    class Django:
        model = Car
        related_models = [Manufacturer]
        fields = [
            "name",
            "launched",
            "type",
        ]

    class Index:
        name = "car_with_prepare_index"

    def prepare_manufacturer_with_related(self, car, related_to_ignore):
        if car.manufacturer is not None and car.manufacturer != related_to_ignore:
            return {
                "name": car.manufacturer.name,
                "country": car.manufacturer.country(),
            }
        return {}

    def prepare_manufacturer_short(self, car):
        if car.manufacturer is not None:
            return {
                "name": car.manufacturer.name,
            }
        return {}

    def get_instances_from_related(self, related_instance):
        return related_instance.car_set.all()
class ManufacturerDocument(Document):
    country = fields.TextField()

    class Django:
        model = Manufacturer
        fields = [
            "name",
            "created",
            "country_code",
            "logo",
        ]

    class Index:
        name = "index_settings"
        settings = index_settings
class AdDocument(Document):
    description = fields.TextField(analyzer=html_strip,
                                   fields={"raw": fields.KeywordField()})

    class Django:
        model = Ad
        fields = [
            "title",
            "created",
            "modified",
            "url",
        ]

    class Index:
        name = "test_ads"
        settings = index_settings
class CarDocument(Document):
    # test can override __init__
    def __init__(self, *args, **kwargs):
        super(CarDocument, self).__init__(*args, **kwargs)

    manufacturer = fields.ObjectField(properties={
        "name": fields.TextField(),
        "country": fields.TextField(),
    })

    ads = fields.NestedField(
        properties={
            "description": fields.TextField(analyzer=html_strip),
            "title": fields.TextField(),
            "pk": fields.IntegerField(),
        })

    categories = fields.NestedField(
        properties={
            "title": fields.TextField(),
            "slug": fields.TextField(),
            "icon": fields.FileField(),
        })

    class Django:
        model = Car
        related_models = [Ad, Manufacturer, Category]
        fields = [
            "name",
            "launched",
            "type",
        ]

    class Index:
        name = "test_cars"
        settings = index_settings

    def get_queryset(self):
        return super(CarDocument,
                     self).get_queryset().select_related("manufacturer")

    def get_instances_from_related(self, related_instance):
        if isinstance(related_instance, Ad):
            return related_instance.car

        # otherwise it's a Manufacturer or a Category
        return related_instance.car_set.all()