# Sort Based on text Position from multiple fields

**URL:** <https://discuss.elastic.co/t/sort-based-on-text-position-from-multiple-fields/229830>\
**Category:** Elasticsearch\
**Created:** [April 26, 2020, 9:29am UTC](https://discuss.elastic.co/t/sort-based-on-text-position-from-multiple-fields/229830 "2020-04-26T09:29:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![psramkumar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/psramkumar/32/31393_2.png) [@psramkumar](https://discuss.elastic.co/u/psramkumar)\
**Post date:** [April 26, 2020, 9:29am UTC](https://discuss.elastic.co/t/sort-based-on-text-position-from-multiple-fields/229830/1 "2020-04-26T09:29:34Z")

</div>

```auto
  {
      "size": 100,
      "_source": {
        "includes": [
          "id",
          "name",
          "type",
          "acc_names"
        ]
      },
      "query": {
        "match_phrase_prefix": {
          "name_factor": {
            "query": "text"
          }
        }
      },
      "sort": {
        "_script": {
          "type": "number",
          "script": {
//Make source in one line
            "source": "def text='text';
                       if (doc['name'].value.toLowerCase().indexOf(text)!=-1){
                         doc['name'].value.toLowerCase().indexOf(text)
                       }else{
                          for(an in doc['acc_names'].values) {
                            if (an.toLowerCase().indexOf(text)!=-1) 
                                return an.toLowerCase().indexOf(text)}}"
          },
          "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:** [May 24, 2020, 9:40am UTC](https://discuss.elastic.co/t/sort-based-on-text-position-from-multiple-fields/229830/2 "2020-05-24T09:40:01Z")

</div>

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