【问题标题】:Django elasticsearch DSL DRF suggetions issueDjango elasticsearch DSL DRF 建议问题
【发布时间】:2020-08-29 07:38:15
【问题描述】:

我正在我的项目中实现 Django elasticsearch DSL DRF 来为 elasticsearch 创建 rest API。 弹性搜索工作正常,但在搜索建议中存在问题。根据文档,如果我在 URL 中使用建议,那么它会给出错误屏幕。但是我没有添加,然后我得到了不正确的回复。我附上我的代码截图。

enter image description here

enter image description here 文件代码 enter image description here 查看代码 enter image description here

查看代码

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

【问题讨论】:

  • 请以文本格式发布您的代码
  • @bmons 我已将代码添加为文本

标签: django elasticsearch django-rest-framework elasticsearch-dsl


【解决方案1】:

您应该继承自 DocumentViewSet 而不是 BaseDocumentViewSet

【讨论】:

  • 在文档示例中,他们使用 BaseDocumentViewSet。他们应该纠正这一点。 @artur-barseghyan
  • @rajinder:这已经被别人和corrected提出来了。
猜你喜欢
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 2021-02-08
  • 2019-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-08
相关资源
最近更新 更多