# Sort dynamic created keyword multi-field

**URL:** <https://discuss.elastic.co/t/sort-dynamic-created-keyword-multi-field/132301>\
**Category:** Elasticsearch\
**Created:** [May 17, 2018, 9:53am UTC](https://discuss.elastic.co/t/sort-dynamic-created-keyword-multi-field/132301 "2018-05-17T09:53:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mix359](https://avatars.discourse-cdn.com/v4/letter/m/0ea827/32.png) [@mix359](https://discuss.elastic.co/u/mix359)\
**Post date:** [May 17, 2018, 9:53am UTC](https://discuss.elastic.co/t/sort-dynamic-created-keyword-multi-field/132301/1 "2018-05-17T09:53:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [May 25, 2018, 10:08pm UTC](https://discuss.elastic.co/t/sort-dynamic-created-keyword-multi-field/132301/2 "2018-05-25T22:08:10Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_ignoring_unmapped_fields)
2. [Script based sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_script_based_sorting), where in a painless script you can check if a document has a particular field `doc['field_name'].length == 0`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 22, 2018, 10:08pm UTC](https://discuss.elastic.co/t/sort-dynamic-created-keyword-multi-field/132301/3 "2018-06-22T22:08:11Z")

</div>

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