Code Monkey home page Code Monkey logo

Comments (2)

barseghyanartur avatar barseghyanartur commented on June 11, 2024

@k-slash:

As it says, you are not allowed to have suggestions on nullable fields. Thus, you should make sure that the value contains at least something. I think an empty space would do as well.

So, in your document (taken from closed source project, but should be enough for understanding):

from django_elasticsearch_dsl_drf.compat import get_elasticsearch_version

MAJOR = get_elasticsearch_version()[0]

# Why do we need to have an empty value for ``VALUE_NOT_SPECIFIED``? As of
# Elastic 5.x, all fields that are used in completion do not allow empty
# values. Therefore, we have to use this trick.
if MAJOR > 2:
    VALUE_NOT_SPECIFIED = " " 
else:
    VALUE_NOT_SPECIFIED = ""
from django_elasticsearch_dsl import DocType, Index, fields
from django_elasticsearch_dsl_drf.compat import KeywordField, StringField

@INDEX.doc_type
class ArticleDocument(DocType):
    """Article Elasticsearch document."""

    title = StringField(
        analyzer=html_strip_lowercase,
        fields={
            'raw': KeywordField(),
            'suggest': fields.CompletionField(),
            'lowercase': StringField(analyzer=keyword_lowercase),
            'dutch': StringField(analyzer='dutch'),
        }
    )

    def prepare_title(self, instance):
        """Prepare title."""
        return instance.title if instance.title else VALUE_NOT_SPECIFIED

from django-elasticsearch-dsl-drf.

k-slash avatar k-slash commented on June 11, 2024

It works like a charm ! 👍

Thanks a lot !

from django-elasticsearch-dsl-drf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.