Sort dynamic created keyword multi-field

Hi to all,

I've started using ES 6 for a project where I'm indexing the objects of an inventory.
Those object have some fixed fields and some additional fields that I have indexed in a dynamic object.
I have also used the dynamic template to map all the field string in that object as a text + keyword (using multi-field).

{
"properties": {
    "object_type_id": {
        "type": "integer"
    },
    "inventory_id": {
        "type": "integer"
    },
    "label": {
        "type": "text",
        "fields": {
            "raw": {
                "type": "keyword",
                "normalizer": "keyword_lowercase"
            }
        }
    },
 	...
    "place.name": {
        "type": "text",
        "fields": {
            "raw": {
                "type": "keyword",
                "normalizer": "keyword_lowercase"
            }
        }
    },
    "extra_data": {
        "type": "object",
        "dynamic": true
    }
},
"dynamic_templates": [{
    "strings": {
        "match_mapping_type": "string",
        "mapping": {
            "type": "text",
            "fields": {
                "raw": {
                    "type": "keyword",
                    "normalizer": "keyword_lowercase"
                }
            }
        }
    }
}]
}

That all work for filtering, but I have some trouble with the sorting:
I don't know which of the field is transformed as text + keyword, so I don't know on which to use the internal .raw value.

There's any way I can instruct elasticsearch to use the internal field for sorting? Or any way to conditionally switch the field?
Any other solution?

Thanks to all
Cheers Mix

I am not sure I completely understood your question, a specific example what you are trying to do would help here.
Looking at your dynamic_templates, every field with string value, should be transformed as text + keyword.

About conditionally switching fields, two things may help you:

  1. Ignoring unmapped fields
  2. Script based sorting, where in a painless script you can check if a document has a particular field doc['field_name'].length == 0

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.