Tôi khá mới để thực hiện tìm kiếm, chịu với tôi trong khi tôi đang học!Lập chỉ mục và tìm kiếm các đối tượng liên quan với haystack
Vì vậy, dự án thú cưng của tôi là một trang web công thức và mỗi công thức có thể có n bước. mô hình trông giống như sau:
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)
(rút ngắn cho ngắn gọn)
Tôi muốn đánh chỉ mục tất cả của nó: Recipe, RecipeIngredient, DietType và bước ... Các DietType và RecipeIngredient dường như làm việc tốt , nhưng các bước thì không. Tôi cho rằng điều này liên quan đến việc sử dụng 'RelatedSearchQuerySet'?
Đây là search_indexes.py tôi:
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()
Đây là mẫu 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 }}
Tôi có thể tìm kiếm các thành phần, tiêu đề, mô tả, loại chế độ ăn uống - mọi thứ hoạt động, ngoại trừ RecipeSteps.
Cuối cùng, tôi đang làm cho truy vấn thông qua vỏ duy nhất tại thời điểm này:
#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
Bất kỳ ý tưởng?
Tôi muốn có thể để có được bí kíp, chúng chứa một bước phù hợp với truy vấn tìm kiếm. IE: Tôi muốn tự tìm kiếm các bước nhưng lấy lại công thức. – dengar81
'RecipeSteps' có chỉ mục riêng của nó không? –
Không, tôi chưa lập chỉ mục này cho tìm kiếm. Đó có phải là chìa khóa? – dengar81