gpt4 book ai didi

django - Django elasticsearch DSL DRF建议问题

转载 作者:行者123 更新时间:2023-12-03 02:23:26 27 4
gpt4 key购买 nike

我在我的项目中实现Django elasticsearch DSL DRF,以创建用于Elasticsearch的其余API。
Elasticsearch 工作正常,但搜索建议中存在问题。根据文档,如果我在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

最佳答案

您应该继承DocumentViewSet而不是BaseDocumentViewSet

关于django - Django elasticsearch DSL DRF建议问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61769076/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com