1

I have problem with .sort() method. For example I have Index with Text() field:

FILTER = token_filter(
    'FILTER', 'edge_ngram', min_gram=3, max_gram=40)
ANALYZER = analyzer(
    'ANALYZER', tokenizer='standard', type='custom', filter=[
        'standard', 'lowercase', 'stop', 'asciifolding',FILTER])

class Article(DocType):
    title = Text(analyzer=ANALYZER)
    body = Text(analyzer='snowball')
    tags = Keyword()

search = Article.search().sort('title')
search.execute()

when I try to execute search query with sort I get an error:

elasticsearch.exceptions.RequestError: TransportError(400, 'search_phase_execution_exception', 'Fielddata is disabled on text fields by default. Set fielddata=true on [title] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory.')

How can I sort by title field properly in this case without setting fieldata=true?

1 Answer 1

4

You cannot sort on a text field, that is a limitation in elasticsearch. It needs to be a type keyword.

Unfortunately type keyword cannot have an analyzer, it can, however, have a normalizer which performs similar, albeit a bit limited, function. Essentially the difference is that you cannot specify a tokenizer since then any sorting would not make much sense (which token would you use for sorting when you have multiple?)

hope this helps

Sign up to request clarification or add additional context in comments.

3 Comments

how about String() field? Can I sort on a string field?
String is deprecated, String(index="not_analyzed") was replaced by keyword and all other String instances are analogous to text so same restrictions apply.
Note that you don't have to choose, you can use both field types (text and keyword) by using the fields parameter - elastic.co/guide/en/elasticsearch/reference/current/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.