Geetha
Geetha

Reputation: 107

Elasticsearch in python project

I have indexed my module to elastic search but it throws the error:

All 'SearchIndex' classes must use the same 'text' fieldname for the 'document=True' field.
Offending index is '<advertisement.search_indexes.ProductIndex object at 0x7f42f57e4db8>'.

I gave my code below:

Modles.py

class Product(models.Model):
user = models.ForeignKey(User)
photo = models.ImageField(upload_to='static/img',blank=True)
category = models.ForeignKey(Category,null=False)
subcategory = models.ForeignKey(SubCategory,null=False)
title = models.CharField(max_length=200)
condition = models.CharField(max_length=25,choices=condition,default='used') 
description = models.TextField(max_length=20, verbose_name="Describe what makes your ad unique:*")
price = models.FloatField(default='$',help_text=".00") 
addtype = models.CharField(max_length=25,choices=STATE_CHOICES,default="seller") 
you_are = models.CharField(max_length=20,choices=you,default="individual")
you_name = models.CharField(max_length=20)
you_email = models.CharField(max_length=30)
you_phone = models.CharField(max_length=12)
timestamp = models.DateTimeField(auto_now=True)
city=models.ForeignKey(City)
locality=models.ForeignKey(Locality)
cars=models.OneToOneField(Cars,null=True,blank=True)
mobile = models.OneToOneField(Mobiles,null=True,blank=True)
tablets = models.OneToOneField(Tablets,null=True,blank=True)
access = models.OneToOneField(Access,null=True,blank=True)

def __unicode__(self):
    return self.title

forms.py

class ProductSearchForm(SearchForm):

def no_query_found(self):
    return self.searchqueryset.all()

search_index.py

lass ProductIndex(indexes.SearchIndex, indexes.Indexable):
title = indexes.CharField(document=True,model_attr='title')
timestamp = indexes.DateTimeField(model_attr='timestamp')

def get_model(self):
    return Product

def index_queryset(self, using=None):
    """Used when the entire index for model is updated."""
    return self.get_model().objects.filter(timestamp__lte=timezone.now())

note_text.txt

{{ object.title }}
{{ object.price }}
{{ object.description }}

Upvotes: 0

Views: 193

Answers (1)

Kaushal
Kaushal

Reputation: 409

In search_index.py replace the line title = indexes.CharField(document=True,model_attr='title') with text = indexes.CharField(document=True,model_attr='title')

This should make it work

Upvotes: 2

Related Questions