python - 我们如何使用弹性搜索在 django rest 框架中获得热门搜索列表?
问题描述
我正在使用 django 2.1、弹性搜索 6.1 和 django-elasticsearch-dsl-drf 0.17.6。
视图.py
class SchoolViewSet(DocumentViewSet):
document = SchoolDocument
serializer_class = SchoolDocumentSerializer
permission_classes = (AllowAny,)
lookup_field = 'id'
filter_backends = [
FilteringFilterBackend,
CompoundSearchFilterBackend,
FacetedSearchFilterBackend
]
# Define ordering fields
ordering_fields = {
'name': 'name',
}
# Specify default ordering
ordering = ('name',)
faceted_search_fields = {
'top_name_hit': {
'field': 'name',
'facet': TermsFacet,
'enabled': True,
'options': {
'size': 4, # Override default number of suggestions
"order": {"_count": "desc"},
'show_term_doc_count_error': True,
},
},
}
文件.py
@INDEX.doc_type
class InstituteDocument(DocType):
id = fields.StringField(attr='id_str')
name = fields.StringField(
analyzer=html_strip,
fields={
'raw': KeywordField(),
'suggest': fields.CompletionField(),
'edge_ngram_completion': fields.StringField(
analyzer=edge_ngram_completion
),
},
fielddata=True
)
class Meta:
model = School
我想获得搜索量最高的学校的结果。我怎样才能获得热门列表的列表?
解决方案
如果你想得到top size的文件,你需要按你的字段排序,如果你想得到一些学校文件,你需要使用学校过滤。如果你找不到这些方法,试试看django es dsl drf源代码 。 https://github.com/sabricot/django-elasticsearch-dsl
因为不允许我写commnet,所以我在这里写。
推荐阅读
- javascript - 如何更改chrome默认副本号
- python - 如何定义起始位置和结束位置,然后告诉海龟连接它们
- java - 如何为 ListAdapter 实现方法 getFilter
- c++ - 错误:C++ 中“]”标记之前的预期主表达式
- eiffel - 如何在 Eiffel 中测试正式泛型类型的对象是否相等?
- html - 如何从我的网站中删除多余的宽度?
- c# - VSTO 项目上的 XSLT 处理问题。它在 WPF 项目上运行良好
- python - 按另一个数组的列对一个数组进行排序 - Python
- javascript - 专注于 jquery 数据表的行并在重点行上按 enter 键应该将行传递到另一个数据表
- python - 为什么只打印第一项