首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何选择在SearchQuerySet中使用的索引?

如何选择在SearchQuerySet中使用的索引?
EN

Stack Overflow用户
提问于 2014-07-24 22:37:22
回答 1查看 2.6K关注 0票数 4

我一直在查看多指标上的Hay堆栈文档,但我不知道如何确切地使用它们。

本例中的主要模型是Proposal。我希望有两个返回建议列表的搜索索引:一个只搜索提案本身,另一个搜索提案及其评论。我已经建立了这样的search_indexes.py

代码语言:javascript
复制
class ProposalIndexBase(indexes.SearchIndex, indexes.Indexable)
    title = indexes.CharField(model_attr="title", boost=1.1)
    text = indexes.NgramField(document=True, use_template=True)
    date = indexes.DateTimeField(model_attr='createdAt')

    def get_model(self):
        return Proposal


class ProposalIndex(ProposalIndexBase):
    comments = indexes.MultiValueField()

    def prepare_comments(self, object):
        return [comment.text for comment in object.comments.all()]


class SimilarProposalIndex(ProposalIndexBase):
    pass

这是我在views.py上的搜索

代码语言:javascript
复制
def search(request):
    if request.method == "GET":
        if "q" in request.GET:
            query = str(request.GET.get("q"))
            results = SearchQuerySet().all().filter(content=query)
    return render(request, "search/search.html", {"results": results})

如何设置从特定索引获取SearchQuerySet的单独视图?

EN

回答 1

Stack Overflow用户

发布于 2014-07-25 07:08:44

Hay堆栈(和其他自动生成的)文档不是清晰性的好例子,它和阅读电话簿一样令人兴奋。我认为你提到的关于“多个索引”的部分实际上是关于访问不同的后端搜索引擎(比如whoosh,solr等等)。用于查询。

但是,您的问题似乎是如何查询不同模型的"SearchIndexes“。在您的示例中,如果我正确理解了您的问题,您希望有一个搜索“建议”,另一个查询“建议”+“注释”。

我想您想看看SearchQuerySet API,它描述了如何过滤搜索返回的查询集。有一个名为http://django-haystack.readthedocs.org/en/latest/searchqueryset_api.html#models的方法,允许您提供模型类或模型类列表来限制查询集结果。

例如,在搜索视图中,您可能希望有一个查询字符串参数,用于“内容”,该参数指定搜索是“建议”还是“所有内容”(建议和注释)。因此,在调用视图时,前端需要提供额外的内容参数(或者可以为不同的搜索使用单独的视图)。

查询字符串需要如下所示:

代码语言:javascript
复制
/search/?q=python&content=proposal
/search/?q=python&content=everything

您的视图应该解析内容查询字符串参数,以获得过滤搜索查询结果的模型:

代码语言:javascript
复制
# import your model classes so you can use them in your search view
# (I'm just guessing these are what they are called in your project)
from proposals.models import Proposal
from comments.models import Comment

def search(request):
    results = None
    if request.method == "GET":
        if "q" in request.GET:
            query = str(request.GET.get("q"))
            # Add extra code here to parse the "content" query string parameter...
            # * Get content type to search for
            content_type = request.GET.get("content")
            # * Assign the model or models to a list for the "models" call
            search_models = []
            if content_type is "proposal":
                search_models = [Proposal]
            elif content_type is "everything":
                search_models = [Proposal, Comment]
            # * Add a "models" call to limit the search results to the particular models
            results = SearchQuerySet().all().filter(content=query).models(*search_models)
    return render(request, "search/search.html", {"results": results})

如果您有很多搜索索引(即来自许多模型的大量内容),您可能不想硬编码视图中的模型,而是使用django.db中的django.db函数动态地获取模型类。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24945161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档