python - 使用 haystack 索引和搜索相关对象

标签 python django django-models django-haystack whoosh

我对搜索实现还很陌生,在我学习的过程中请多多包涵!

所以我的宠物项目是一个食谱网站,每个食谱可以有 n 个步骤。该模型看起来像:

class Recipe(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    hotness = models.ForeignKey(Hotness)
    recipe_diet = models.ManyToManyField(DietType)
    ingredients = models.ManyToManyField(Ingredient, through="RecipeIngredient")

class DietType(models.Model):
    diet_type = models.CharField(max_length=50)
    description = models.TextField(null=True, blank=True)

class RecipeIngredient(models.Model):
    recipe = models.ForeignKey(Recipe)
    ingredient = models.ForeignKey(Ingredient)
    quantifier = models.ForeignKey(Quantifier)
    quantity = models.FloatField()

class RecipeSteps(models.Model):
    step_number = models.IntegerField()
    description = models.TextField()
    recipe = models.ForeignKey(Recipe)

(为简洁起见缩短)

我想索引所有这些:Recipe、RecipeIngredient、DietType 和 Steps... DietType 和 RecipeIngredient 似乎工作正常,但步骤不是。我认为这与“RelatedSearchQuerySet”的使用有关?

这是我的 search_indexes.py:

from haystack import indexes
from recipes.models import Recipe

class RecipeIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)
    title = indexes.CharField(model_attr='title')
    ingredients = indexes.MultiValueField(indexed=True, stored=True)
    description = indexes.CharField(model_attr='description')
    hotness = indexes.CharField(model_attr='hotness')
    diet_type = indexes.MultiValueField(indexed=True, stored=True)
    recipesteps = indexes.MultiValueField(indexed=True, stored=True)

    def prepare_steps(self, object):
        return [step.description for step in object.recipesteps.all()]

    def get_model(self):
        return Recipe

    def load_all_queryset(self):
        # Pull all objects related to the Note in search results.
        return Recipe.objects.all().select_related()

这是模板 recipe_text.txt:

{{ object.title }}
{{ object.cuisine }}
{% for ingr in object.ingredients.all %}
  {{ ingr.title }}
{% endfor %}
{{ object.description }}
{% for dt in object.recipe_diet.all %}
  {{ dt.diet_type }}
{% endfor %}
{{ object.user }}
{{ object.hotness }}
{% for step in object.recipesteps.all %}
  {{ step.description }}
{% endfor %}
{{ object.body }}

我可以搜索成分、标题、描述、饮食类型 - 一切正常,除了 RecipeSteps。

最后,我现在只通过 shell 进行查询:

#producing results:
sq = SearchQuerySet().filter(content='onion') #ingredient
sq = SearchQuerySet().filter(content='bolognese') #title
sq = SearchQuerySet().filter(content='bologna') #description
#not producing any results:
sq = SearchQuerySet().filter(content='chop') #step
sq = RelatedSearchQuerySet().filter(content='chop').load_all() #assuming this does the expanded search

有什么想法吗?

最佳答案

我发现了两个问题:

  1. RecipeIndex 中的名称 prepare_steps 是错误的,它应该是 prepare_{field_name} 所以将其更改为 prepare_recipesteps

  2. 您正在尝试以错误的方式访问相关步骤 object.recipesteps.all 对象 recipe_text.txt,它应该是 object.recipesteps_set .all.或者继续使用 recipesteps 但将其添加为 related_nameRecipeSteps 模型中为 ForeignKey Recipe 例如

    class RecipeSteps(models.Model):
        # //
        recipe = models.ForeignKey(Recipe, related_name='recipesteps')
    

关于python - 使用 haystack 索引和搜索相关对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44768044/

相关文章:

python - Plotly python offline - 点击访问 url?

python - 如何将 django-rest-swagger 2.0 与现有 DRF 应用程序集成(初学者)

Django URL 模式 : How to check if all url-names are distinct?

python - Django:ModelMultipleChoiceField 不选择初始选择

python - "No installed app with label ' 管理员 '"运行 Django 迁移。该应用程序已正确安装

python - ProactorEventLoop - 值错误 : loop argument must agree with Future

python - 是什么导致 Python "Interpreter not Initialized (version mismatch?)"错误?

python - 并行 django 运行迁移?

Django:限制一对多关系中的关系数

python - Django 中的 MYSQL 查询等效项