Django elasticsearch DSL DRF建议问题 [英] Django elasticsearch DSL DRF suggetions issue

查看:124
本文介绍了Django elasticsearch DSL DRF建议问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的项目中实现Django elasticsearch DSL DRF,以创建用于Elasticsearch的其余API.弹性搜索工作正常,但搜索建议中存在问题.根据文档,如果我在URL中使用建议,那么它将显示错误屏幕.但是我没有添加,然后我得到了错误的答复.我正在附上我的代码的屏幕截图.

I am implementing Django elasticsearch DSL DRF in my project to create rest API for elasticsearch. Elastic search is working fine but having the issue in search suggestions. As per documentation if I use the suggest in URL then it gives error screen. But I don't add that then I got an incorrect response. I am attaching screenshots of my code.

在此处输入图片描述

在此处输入图片描述文件代码在此处输入图片描述查看代码在此处输入图片描述

查看代码

class ProductDocumentView(BaseDocumentViewSet):
"""The ProductDocument view."""

document = ProductDocument
serializer_class = ProductListSearchSerializer
pagination_class = LimitOffsetPagination
lookup_field = 'id'
filter_backends = [
    FilteringFilterBackend,
    IdsFilterBackend,
    OrderingFilterBackend,
    DefaultOrderingFilterBackend,
    CompoundSearchFilterBackend,
]
# Define search fields
search_fields = (
    'title',
    'product_type',
    'description',
    'other_desc',
)
# 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,
        ],
    },
    '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,
        ],
    },


}
# Define ordering fields
ordering_fields = {
    'id': 'id',
    'price': 'price.raw',
}
# Specify default ordering
ordering = ('id', 'price',)
suggester_fields = {
    'title_suggest': {
        'field': 'title.suggest',
        'suggesters': [
            SUGGESTER_TERM,
            SUGGESTER_PHRASE,
            SUGGESTER_COMPLETION,
        ],
        'options': {
            'size': 5,
            'skip_duplicates':True,
        },
    },

}

文档代码

 INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])

# See Elasticsearch Indices API reference for available settings
INDEX.settings(
    number_of_shards=1,
    number_of_replicas=1
)


html_strip = analyzer(
    'html_strip',
    tokenizer="standard",
    filter=["lowercase", "stop", "snowball"],
    char_filter=["html_strip"]
)

@INDEX.doc_type
class ProductDocument(Document):
    """Product Elasticsearch document."""
id = fields.IntegerField(attr='id')

title = StringField(
    attr='product_title_indexing',
    analyzer=html_strip,
    fields={
        'raw': KeywordField(),
        'suggest': fields.CompletionField(),

    }

)

product_type = StringField(
    attr='product_type_indexing',
    analyzer=html_strip,
    fields={
        'raw': KeywordField(),
    }
)

description = StringField(
    attr='product_desc_indexing',
    analyzer=html_strip,
    fields={
        'raw': KeywordField(),
    }
)

price = StringField(
    attr='product_price_indexing',
    fields={
        'raw': fields.FloatField(),
    }
)

image = StringField(
    attr='product_image_indexing',
    analyzer=html_strip,
    fields={
        'raw': KeywordField(),
    }
)

other_desc = StringField(
    attr='product_other_desc_indexing',
    analyzer=html_strip,
    fields={
        'raw': KeywordField(),
    }
)

class Django(object):
    """Inner nested class Django."""

    model = ProductModel  # The model associate with this Document

推荐答案

您应该继承自 DocumentViewSet ,而不是 BaseDocumentViewSet .

You should inherit from DocumentViewSet instead of BaseDocumentViewSet.

这篇关于Django elasticsearch DSL DRF建议问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆