首页 > 解决方案 > 在 DRF 中使用 elasticsearch 的正确方法

问题描述

我在这里使用弹性搜索的测试示例,文档链接https://django-elasticsearch-dsl-drf.readthedocs.io/en/0.1.8/quick_start.html与 django rest。

我做了和文档一样的事情,但它不适用于我的案例。此外,在文档中,如果我正在运行带有此端口Connection error failed错误的服务器,则会在 8000 端口上运行服务器,但如果我在 9200 端口上运行它可以工作,但它会在下图中返回 404

在此处输入图像描述

这是我的代码:

书本.py

@BOOK_INDEX.doc_type
@registry.register_document
class BookDocument(Document):
    """Book Elasticsearch document."""

    id = fields.IntegerField(attr='id')

    title = fields.TextField(
        analyzer=html_strip,
        fields={
            'raw': fields.TextField(
                analyzer='keyword'
            )
        }
    )

    description = fields.TextField(
        analyzer=html_strip,
        fields={
            'raw': fields.TextField(
                analyzer='keyword'
            )
        }
    )

    summary = fields.TextField(
        analyzer=html_strip,
        fields={
            'raw': fields.TextField(
                analyzer='keyword'
            )
        }
    )

    publisher = fields.TextField(
        attr='publisher_indexing',
        analyzer=html_strip,
        fields={
            'raw': fields.TextField(
                analyzer='keyword'
            )
        }
    )

    publication_date = fields.DateField()

    state = fields.TextField(
        analyzer=html_strip,
        fields={
            'raw': fields.TextField(
                analyzer='keyword'
            )
        }
    )

    isbn = fields.TextField(
        analyzer=html_strip,
        fields={
            'raw': fields.TextField(
                analyzer='keyword'
            )
        }
    )

    price = fields.FloatField()

    pages = fields.IntegerField()

    stock_count = fields.IntegerField()

    tags = fields.TextField(
        attr='tags_indexing',
        analyzer=html_strip,
        fields={
            'raw': fields.TextField(
                analyzer='keyword',
                multi=True
            )
        },
        multi=True
    )

    class Django:
        """Meta options."""

        model = Book  # The model associate with this DocType

视图.py

class BookDocumentView(DocumentViewSet):
    """The BookDocument view."""

    document = BookDocument
    serializer_class = BookDocumentSerializer
    lookup_field = 'id'
    filter_backends = [
        FilteringFilterBackend,
        IdsFilterBackend,
        OrderingFilterBackend,
        SearchFilterBackend,
    ]
    # Define search fields
    search_fields = (
        'title',
        'description',
        'summary',
    )
    # Define filter fields
    filter_fields = {
        'id': {
            'field': 'id',
            # Note, that we limit the lookups of id field in this example,
            # to `range`, `in`, `gt`, `gte`, `lt` and `lte` filters.
            'lookups': [
                LOOKUP_FILTER_RANGE,
                LOOKUP_QUERY_IN,
                LOOKUP_QUERY_GT,
                LOOKUP_QUERY_GTE,
                LOOKUP_QUERY_LT,
                LOOKUP_QUERY_LTE,
            ],
        },
        'title': 'title.raw',
        'publisher': 'publisher.raw',
        'publication_date': 'publication_date',
        'state': 'state.raw',
        'isbn': 'isbn.raw',
        'price': {
            'field': 'price.raw',
            # Note, that we limit the lookups of `price` field in this
            # example, to `range`, `gt`, `gte`, `lt` and `lte` filters.
            'lookups': [
                LOOKUP_FILTER_RANGE,
                LOOKUP_QUERY_GT,
                LOOKUP_QUERY_GTE,
                LOOKUP_QUERY_LT,
                LOOKUP_QUERY_LTE,
            ],
        }...

请问有人可以帮我解决这个问题吗?一般来说,我在 9200 端口上运行服务器是否正确?提前致谢!

标签: python-3.xdjangoelasticsearchdjango-rest-framework

解决方案


您正在使用 django-elasticsearch-dsl-drf 来获得更好的弹性搜索用户体验,请注意您应该在设备上配置和运行弹性搜索引擎。检查此链接以安装 elasticsearch https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html。安装完成后,打开终端并通过elasticsearch命令运行 elasticsearch。请注意,您应该在 Django 设置文件中配置 elasticsearch-dsl 设置,默认配置为:

ELASTICSEARCH_DSL = {
    'default': {
        'hosts': 'localhost:9200'
    },
}

现在在端口 9200 上运行 elasticsearch 服务器,你现在已经将你的 django-elasticsearch-dsl-drf 连接到 elasticsearch 搜索引擎,我不会深入这个话题,希望你自己做剩下的。


推荐阅读