How to include foregin key attribute in Elastic Search Django?

18 views Asked by At

I have two models Products and VendorProducts model as follows :

class Product(models.Model):
    brand = models.ForeignKey(BrandName,related_name="brand_products",  on_delete=models.CASCADE)
    item_name_title = models.CharField(max_length=10000)

class VendorProducts(BaseModel):
    product = models.ForeignKey(Product , on_delete=models.PROTECT , null=True ,related_name="vendor_products")
    vendor_selling_price = models.FloatField(default=0, null=True, blank=True)

Now I want to add this VendorProduct model in ElasticSearch. And I have done something like this


@registry.register_document
class ProductDocument(Document):
   product = fields.ObjectField(properties={
        "item_name_title" : fields.TextField()})

    class Index:
        name = "vendor_products"
    class Django:
        model = VendorProducts
        fields = [
            "id",
        ]
        related_models = [Product]

   def get_queryset(self):
        """Not mandatory but to improve performance we can select related in one SQL 
  request"""
        return super(ProductDocument, self).get_queryset().select_related(
            'product'
        )
    

First, this is how we add the foreign key model in Elastic search. Now the issue is I am not able to get any of the results. Although I  can see the results in elastic search when I tryto access it by port 9200 I can see the search results. But when querying I am getting empty responses. 

This code I am using 

s = ProductDocument.search().query("match", description=search)


0

There are 0 answers