# Sort script input needed

**URL:** <https://discuss.elastic.co/t/sort-script-input-needed/309527>\
**Category:** Elasticsearch\
**Created:** [July 13, 2022, 11:41am UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527 "2022-07-13T11:41:53Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [July 13, 2022, 11:41am UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/1 "2022-07-13T11:41:53Z")

</div>

I have a dataset like this (sometimes `Source` field does not exist):

```auto
Id Source Sender
--------------------------------------------------------------------
1 test@abc.com
2 amy@abc.com
3 Amy's channel amy@abc.com
4 Avery's channel test@abc.com
5 Bill's channel xxx@abc.com

```

I want to sort it by the `Source` field first, but if it doesn't exist, take the `Sender` field, the order should be 3 -\> 2 -\> 4 -\> 5 -\> 1.

Tried:

```auto
GET emailmessages-sort/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "script": "if (doc['Source'].size() != 0) doc['Sender'].value; else doc['Sender'].value",
      "type": "string",
      "order": "asc"
    }
  }
}

```

But the last document is **5** (it should be 1) (because it's sorted by `Sender` which is `xxx@test.com` instead of `Source` which is `Bill`.

It's **3 -\> 2 -\> 4 -\> 1 -\> 5** and it should be **3 -\> 2 -\> 4 -\> 5 -\> 1**

How can I sort by both fields combined, is this even possible?

---

<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:** [July 13, 2022, 1:57pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/2 "2022-07-13T13:57:57Z")

</div>

```auto
"sort": [
  {"Source": "asc"},
  {"Sender": "asc"}
]

```

does not work?

---

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [July 13, 2022, 2:03pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/3 "2022-07-13T14:03:01Z")

</div>

Nope, this will sort first by Source, and after that by Sender, it will be 3 -\> 4 -\> 5 -\> 2 -\> 1, which means `"Source = Bill's channel"` will be before `"Sender = amy@abc.com"`

---

<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:** [July 13, 2022, 2:04pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/4 "2022-07-13T14:04:07Z")

</div>

How about the script below?

```auto
if (doc.containKey('Source')){doc['Source'].value}else{doc['Sender'].value}

```

---

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [July 13, 2022, 2:09pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/5 "2022-07-13T14:09:04Z")

</div>

This is not working because member method [java.util.Map, containKey/1] is not found, but I guess, by the logic, your answer would sort it in the same way ( 3 - 4 -5 - 2 - 1) which is not good..

---

<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:** [July 13, 2022, 2:15pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/6 "2022-07-13T14:15:23Z")

</div>

Really? `doc.containsKey` also appears in the NOTE of the [official document](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-fields.html#modules-scripting-doc-vals).

and by the logic this script will sort in 3, 2, 4, 5, 1.

---

<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:** [July 13, 2022, 2:15pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/7 "2022-07-13T14:15:57Z")

</div>

Oh, sorry. there was typo.

```auto
if (doc.containsKey('Source')){doc['Source'].value}else{doc['Sender'].value}

```

is correct.

---

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [July 13, 2022, 2:23pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/8 "2022-07-13T14:23:26Z")

</div>

> [@Tomo\_M](#):
>
> ```auto
> if (doc.containsKey('Source')){doc['Source'].value}else{doc['Sender'].value}
> 
> ```

reason" : "A document doesn't have a value for a field! Use doc[].size()==0 to check if a document is missing a field!

bot fields are mapped as a `keyword` btw

---

<div class="post-metadata">

**Author:** ![ansamHox](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@ansamHox](https://discuss.elastic.co/u/ansamHox)\
**Post date:** [July 14, 2022, 10:40pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/10 "2022-07-14T22:40:15Z")

</div>

Ok, I found the issue, it wasn't working because of Capitalized letters, I just put on both fields `"normalizer" : "lowercase"`, and it works now as:

```auto
GET emailmessages-sort2/_search
{
  "query": {
    "match_all": {}
  },
  "sort": {
    "_script": {
      "script": "if (doc['Source'].size() != 0) doc['Source'].value; else doc['Sender'].value",
      "type": "string",
      "order": "asc"
    }
  }
}

```

---

<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:** [August 11, 2022, 10:40pm UTC](https://discuss.elastic.co/t/sort-script-input-needed/309527/11 "2022-08-11T22:40:56Z")

</div>

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