【问题标题】:How can we get top trending searched listing in django rest framework using elastic search?我们如何使用弹性搜索在 django rest 框架中获得热门搜索列表?
【发布时间】:2019-10-02 17:24:08
【问题描述】:

我正在使用 django 2.1、elastic search 6.1 和 django-elasticsearch-dsl-drf 0.17.6。

view.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

我想获得搜索量最高的学校的结果。 如何获取热门榜单?

【问题讨论】:

    标签: python django elasticsearch search django-rest-framework


    【解决方案1】:

    如果你想得到top size的文件,你需要按你的字段排序,如果你想得到一些学校的文件,你需要使用学校过滤。如果你找不到这些方法,试试看django es dsl drf 源代码。 https://github.com/sabricot/django-elasticsearch-dsl

    因为不能写commnet,所以写在这里。

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2013-10-10
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多