Django elasticsearch DSL DRF suggetions issue

余生长醉 提交于 2020-06-29 04:58:09

问题


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.

enter image description here

enter image description here document code enter image description here view code enter image description here

Code for View

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,
        },
    },

}

Code for document

 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

回答1:


You should inherit from DocumentViewSet instead of BaseDocumentViewSet.



来源:https://stackoverflow.com/questions/61769076/django-elasticsearch-dsl-drf-suggetions-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!