Django elasticsearch DSL DRF 建议问题
Django elasticsearch DSL DRF suggetions issue
我正在我的项目中实施 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
您应该继承 DocumentViewSet
而不是 BaseDocumentViewSet
。
我正在我的项目中实施 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
您应该继承 DocumentViewSet
而不是 BaseDocumentViewSet
。