我正在使用django-彈性搜索-dsl包,并且有點(diǎn)進(jìn)退兩難。這是我的代碼:models.pyclass Source(models.model): name = models.CharField(max_length=50)class Posting(models.Model): title = models.CharField(max_length=250) sources = models.ManyToMany(Sources, related_name="postings", through="PostingSource")class PostingSource(models.Model): posting = models.ForeignKey(Posting, related_name="posting_sources", on_delete=models.CASCADE) source = models.ForeignKey(Source, related_name="posting_sources", on_delete=models.CASCADE)documents.pyclass PostingDocument(Document): sources = fields.ObjectField(properties={"name": fields.KeywordField()}) class Index: name = "posting" settings = {"all the settings stuff"} class Django: model = Posting fields = ["title"] related_models = [PostingSource] def get_queryset(self): return super().get_queryset().select_related("sources") def get_instance_from_related(self, related_instance): if isinstance(related_instance, PostingSource): return related_instance.posting我的問題是,當(dāng)我更新帖子上的來源時(shí),由于某種原因,彈性搜索索引pre_save更新,而不是post_save。我基本上必須使用相同的來源進(jìn)行2次放置請(qǐng)求,以使更改反映在我的索引中。我在文檔中添加了 一個(gè),它似乎可以正常工作,但感覺它以后會(huì)導(dǎo)致性能問題。任何幫助或指導(dǎo)將不勝感激。def prepare_sources(self, instance):
1 回答

30秒到達(dá)戰(zhàn)場(chǎng)
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超6個(gè)贊
經(jīng)過幾個(gè)月的測(cè)試,我通過添加一個(gè)我將我的多對(duì)多關(guān)系非規(guī)范化的地方解決了我的第一個(gè)問題。我也通過簡(jiǎn)單地?fù)碛杏兄谛阅艿慕鉀Q了我的第二個(gè)問題。def prepare_sources(self, instance):
.select_related("sources")
添加回答
舉報(bào)
0/150
提交
取消