elasticsearch-dsl-py Sorting by Text() field

删除回忆录丶 提交于 2019-12-11 13:24:55

问题


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:


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



来源:https://stackoverflow.com/questions/45220352/elasticsearch-dsl-py-sorting-by-text-field

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