python - python项目中的Elasticsearch

标签 python django elasticsearch

我已将我的模块索引到 Elasticsearch ,但它会引发错误:

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

我在下面给出了我的代码:

模型.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

表格.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 }}

最佳答案

在 search_index.py 中替换该行
title = index.CharField(document=True,model_attr='title')

text = index.CharField(document=True,model_attr='title')

这应该使它工作

关于python - python项目中的Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28212007/

相关文章:

python - 正则表达式中的嵌套文本

django - Django 的 Comet 框架

python - Django TastyPie ToManyField 关系提示 "has no data and doesn' t 允许空值”在新项目的 POST 上

python - 如何向 exec 的运行代码发送值

python - 在计算共振峰频率特征时如何确定线性预测系数(LPC)中的滤波器阶数?

python - Matplot Numpy 值错误 : setting an array element with a sequence

elasticsearch - 所有页面的ElasticSearch聚合

django - 无法从同一台 AWS EC2 Linux 机器中调用 Django REST API

sorting - 在 Elasticsearch 中如何按两个日期中较小的一个进行排序?

elasticsearch - 无论如何在查询搜索中创建别名?