3

Within a Python project I have an elastic search index that throws the following error when I sort on a given field: elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'Fielddata is disabled on text fields by default. Set fielddata=true on [my_field] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.')

Originally this was a text field and so I did what the message recommended and tried changing it to a keyword field or setting fielddata=True. Unfortunately, those changes still result in the exact same error message and so now I am confused.

Here are the different ways I have tried defining the field in my document index. All result in the same error.

from elasticsearch_dsl import DocType, Index, field, Keyword

class MyDoc(DocType):
    my_field = field.Text(fields={'raw': field.Keyword()})
    my_field = field.Keyword(fielddata=True)
    my_field = field.Keyword()
    my_field = Keyword()
    my_field = Keyword(fields={'raw': field.Keyword()})

My index mapping:

{
my-index: {
mappings: {
doc: {
properties: {
code: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
},
my_field: {
type: "text",
fields: {
keyword: {
type: "keyword",
ignore_above: 256
}
}
}
}
}
}
}
}

My Query:

{'query': {'bool': {'filter': [{'bool': {'must_not': [{'terms': {'code': []}}]}}]}}, 'sort': ['my_field'], 'from': 0, 'size': 6}
rurp
  • 1,376
  • 2
  • 14
  • 22
  • Can you provide the mapping of the index as it currently exists, and show the code with the `query` and `sort` that is throwing the exception? – James Pittiglio Apr 10 '19 at 21:08
  • Sure thing, I added them to the question. – rurp Apr 10 '19 at 21:32
  • 4
    did you try `'sort': ['my_field.keyword']` ? – Polynomial Proton Apr 10 '19 at 21:42
  • @TheUknown That works! I now have some django_elasticsearch_dsl indices that can be sorted with field_name, and some elasticsearch_dsl that need to be sorted with field_name.keyword. Any idea how I index the elasticsearch ones to be filtered just off of the field_name? – rurp Apr 10 '19 at 21:53
  • @rurp Your best bet is to update the index mapping in a new index so that `my_field` is of type `keyword`, and a multi-field of `my_field.text` as type `text` - you will need to reindex the data for the new mappings. – James Pittiglio Apr 12 '19 at 12:32
  • @PolynomialProton i have a similar issue, and would like try your suggestion as well. Can you please explain if this should be ran in python/kibana/commandprompt? Talking about this btw: "'sort': ['my_field.keyword']" – Cornelis Oct 24 '22 at 21:12

0 Answers0