# Sort on object part that exist

**URL:** <https://discuss.elastic.co/t/sort-on-object-part-that-exist/293354>\
**Category:** Elasticsearch\
**Created:** [January 3, 2022, 2:03pm UTC](https://discuss.elastic.co/t/sort-on-object-part-that-exist/293354 "2022-01-03T14:03:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrDeliK](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MrDeliK](https://discuss.elastic.co/u/MrDeliK)\
**Post date:** [January 3, 2022, 2:03pm UTC](https://discuss.elastic.co/t/sort-on-object-part-that-exist/293354/1 "2022-01-03T14:03:59Z")

</div>

Hi guys, sorry i have another question but this time concerning the sorting of a request.

Considering this mapping

```json
{
  "sample_data": {
      "mappings": {
          "properties": {
              "title": {
                  "properties": {
                      "fr": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "nl": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      },
                      "poly": {
                          "type": "text",
                          "fields": {
                              "keyword": {
                                  "type": "keyword",
                                  "ignore_above": 256
                              }
                          }
                      }
                  }
              }
          }
      }
  }
}

```

The restrictions here are if the `poly` field is set, the `fr` and `nl` must be empty. But if the `fr` is set then the `nl` must be set and the `poly` must be empty.  
The issue here is when i try to sort on the `title` i need to use the `poly` when it is set or the `fr`/`nl` depending on the user language.

How can i do that ?

I tried the multiple sort but when the poly is empty it use null and not the fr/nl value

```json
{
    "query": {
        "query_string": {
            "query": "**",
            "fields": ["title.poly", "title.fr"]
        }
    },
    "sort": [{"title.poly.keyword": {"order": "desc"}}, {"title.fr.keyword": {"order": "desc"}}, {"title.nl.keyword": {"order": "desc"}}],
    "_source": ["title"]
}

```

```json
[
    {
        "_index": "sample_data",
        "_type": "_doc",
        "_id": "elem0",
        "_version": 2,
        "_score": null,
        "_source": {
            "title": {
                "poly": "Mi Casa"
            }
        },
        "sort": [
            "Mi Casa",
            null,
            null
        ]
    },
    {
        "_index": "sample_data",
        "_type": "_doc",
        "_id": "elem1",
        "_version": 1,
        "_score": null,
        "_source": {
            "title": {
                "fr": "test with fr",
                "nl": "test with nl"
            }
        },
        "sort": [
            null,
            "test with fr",
            "test with nl"
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 3, 2022, 3:41pm UTC](https://discuss.elastic.co/t/sort-on-object-part-that-exist/293354/2 "2022-01-03T15:41:18Z")

</div>

One possible recommendation is make other fields for sort and use copy\_to mapping.

```auto
"properties": {
    "title": {
        "properties": {
            "fr": {
                "type": "text",
                "copy_to": "title_sort_fr",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "nl": {
                "type": "text",
               "copy_to": "title_sort_nl",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "poly": {
                "type": "text",
               "copy_to": ["title_sort_fr", "title_sort_nl"],
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            }
        }
     },
     "title_sort_fr":{
       "type": "keyword"
     },
     "title_sort_nl":{
       "type": "keyword"
     },

```

> **[copy\_to | Elasticsearch Guide \[7.16\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html)**

---

<div class="post-metadata">

**Author:** ![MrDeliK](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MrDeliK](https://discuss.elastic.co/u/MrDeliK)\
**Post date:** [January 3, 2022, 4:35pm UTC](https://discuss.elastic.co/t/sort-on-object-part-that-exist/293354/3 "2022-01-03T16:35:06Z")

</div>

unfortunately right now i don't have access to the back code.  
I did success with a script sorting but i'm not sure if it's a good solution or not.

```json
{
    "query": {
        "multi_match": {
            "query": "Mi ca",
            "type": "phrase_prefix",
            "fields": ["title.poly", "title.fr"]
        }
    },
    "sort" : {
        "_script": {
            "type": "string",
            "script": {
                "lang": "painless",
                "source": "if(doc['title.poly.keyword'].size() != 0) { return doc['title.poly.keyword'].value.toLowerCase(); } return doc['title.fr.keyword'].value.toLowerCase();"
            },
            "order": "desc"
        }
    },
    "_source": ["title"]
}

```

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [January 3, 2022, 5:18pm UTC](https://discuss.elastic.co/t/sort-on-object-part-that-exist/293354/4 "2022-01-03T17:18:39Z")

</div>

I suppose there may be only a performance concern. If the response is quick enough for you, it's a good solution!

---

<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:** [January 31, 2022, 5:18pm UTC](https://discuss.elastic.co/t/sort-on-object-part-that-exist/293354/5 "2022-01-31T17:18:43Z")

</div>

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