按 django_id 筛选 Haystack (SOLR) 结果
Filtering Haystack (SOLR) results by django_id
使用 Django/Haystack/SOLR,我希望能够将搜索结果限制为 django_ids 特定范围内的那些记录。获取这些 ID 不是问题,但尝试按它们进行过滤会产生一些意想不到的效果。代码如下所示(为清晰起见,删减了无关代码):
def view_results(request,arg):
# django_ids list is first calculated using arg...
sqs = SearchQuerySet().facet('example_facet') # STEP_1
sqs = sqs.filter(django_id__in=django_ids) # STEP_2
view = search_view_factory(
view_class=SearchView,
template='search/search-results.html',
searchqueryset=sqs,
form_class=FacetedSearchForm
)
return view(request)
在标记为 STEP_1 的位置,我获得了所有数据库记录。在 STEP_2,记录已成功缩小到我期望的 django_ids 列表的数量。当用户在表单中指定搜索词的情况下显示搜索结果时,就会出现问题。我没有返回 STEP_2 中与该词匹配的所有记录,而是从 STEP_2 中获取所有记录以及来自 STEP_1 中与该词匹配的所有记录。
据推测,因此,我需要在 haystack/views.py 中重写 for SearchView 中的 one/some 方法,但是什么?任何人都可以提出一种实现此处要求的方法吗?
经过深思熟虑,我找到了解决此问题的方法。在上面的代码中,问题出现在 view = search_view_factory...
行,所以我需要创建自己的 SearchView class 并重写 get_results(self)
方法以便在搜索完成后应用过滤运行 用户的搜索字词。结果是这些行的代码:
class MySearchView(SearchView):
def get_results(self):
search = self.form.search()
# The ID I need for the database search is at the end of the URL,
# but this may have some search parameters on and need cleaning up.
view_id = self.request.path.split("/")[-1]
view_query = MyView.objects.filter(id=view_id.split("&")[0])
# At this point the django_ids of the required objects can be found.
if len(view_query) > 0:
view_item = view_query.__getitem__(0)
django_ids = []
for thing in view_item.things.all():
django_ids.append(thing.id)
search = search.filter_and(django_id__in=django_ids)
return search
在最后使用 search.filter_and
而不是 search.filter
是另一件被证明是必不可少的事情,但是在到达搜索视图。
使用 Django/Haystack/SOLR,我希望能够将搜索结果限制为 django_ids 特定范围内的那些记录。获取这些 ID 不是问题,但尝试按它们进行过滤会产生一些意想不到的效果。代码如下所示(为清晰起见,删减了无关代码):
def view_results(request,arg):
# django_ids list is first calculated using arg...
sqs = SearchQuerySet().facet('example_facet') # STEP_1
sqs = sqs.filter(django_id__in=django_ids) # STEP_2
view = search_view_factory(
view_class=SearchView,
template='search/search-results.html',
searchqueryset=sqs,
form_class=FacetedSearchForm
)
return view(request)
在标记为 STEP_1 的位置,我获得了所有数据库记录。在 STEP_2,记录已成功缩小到我期望的 django_ids 列表的数量。当用户在表单中指定搜索词的情况下显示搜索结果时,就会出现问题。我没有返回 STEP_2 中与该词匹配的所有记录,而是从 STEP_2 中获取所有记录以及来自 STEP_1 中与该词匹配的所有记录。
据推测,因此,我需要在 haystack/views.py 中重写 for SearchView 中的 one/some 方法,但是什么?任何人都可以提出一种实现此处要求的方法吗?
经过深思熟虑,我找到了解决此问题的方法。在上面的代码中,问题出现在 view = search_view_factory...
行,所以我需要创建自己的 SearchView class 并重写 get_results(self)
方法以便在搜索完成后应用过滤运行 用户的搜索字词。结果是这些行的代码:
class MySearchView(SearchView):
def get_results(self):
search = self.form.search()
# The ID I need for the database search is at the end of the URL,
# but this may have some search parameters on and need cleaning up.
view_id = self.request.path.split("/")[-1]
view_query = MyView.objects.filter(id=view_id.split("&")[0])
# At this point the django_ids of the required objects can be found.
if len(view_query) > 0:
view_item = view_query.__getitem__(0)
django_ids = []
for thing in view_item.things.all():
django_ids.append(thing.id)
search = search.filter_and(django_id__in=django_ids)
return search
在最后使用 search.filter_and
而不是 search.filter
是另一件被证明是必不可少的事情,但是在到达搜索视图。