# Painless sorting removing documents from results

**URL:** <https://discuss.elastic.co/t/painless-sorting-removing-documents-from-results/193215>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 31, 2019, 8:32pm UTC](https://discuss.elastic.co/t/painless-sorting-removing-documents-from-results/193215 "2019-07-31T20:32:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![santiaguzz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santiaguzz/32/51379_2.png) [@santiaguzz](https://discuss.elastic.co/u/santiaguzz)\
**Post date:** [July 31, 2019, 8:32pm UTC](https://discuss.elastic.co/t/painless-sorting-removing-documents-from-results/193215/1 "2019-07-31T20:32:19Z")

</div>

Hi there;

I'm trying to query three different indexes at a time, one of them have two mappings, as follows:

```
price: {
          type: 'float'
        },
        offer_price: {
          type: 'float'
        }

```

The next one have only one important mapping for prices:

```
 price: {
          type: 'float'
        }

```

And the last one have no prices mapped. when I query elasticsearch for the three indexes at a time the results are properly returned and filtered. However I would like to sort the results the following way: If there is an "offer\_price" present take this value, else if there is a "price" present take this value, otherwise return 0. I'm trying to use the following painless script to that purpose:

```
 _script: {
          type: 'number',
          script: {
            lang: 'painless',
            source:
              "if(!doc['offer_price'].empty && doc['offer_price'].value != 0) { return doc['offer_price'].value; } else { if(!doc['price'].empty) { return doc['price'].value; } else { return 0; }}"
          },
          order: "asc" //or "desc" (this works fine for the returned docs)
        }

```

When running the query only documents with the "offer\_price" field mapped are returned, documents from the two other indexes are missing in the return set. I tried with a ternary operator with only one statement with no luck. Can anyone point me in the right direction please?

Thanks in advance.

Szz.

EDIT: Tried with:  
\_script: {  
type: 'number',  
script: {  
lang: 'painless',  
source:  
"if(doc['offer\_price'] != null && doc['offer\_price'].value != 0) { return doc['offer\_price'].value; } else { if(doc['price'] != null) { return doc['price'].value; } else { return 0; }}"  
},  
order: "asc"/"desc"  
}

With no luck neither.

P.D: using elastic search 7.2.0

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 2, 2019, 11:23pm UTC](https://discuss.elastic.co/t/painless-sorting-removing-documents-from-results/193215/2 "2019-08-02T23:23:42Z")

</div>

If you check the response from elasticsearch, do you see a section about shard responses? Something like this:

```auto
"_shards":{
  "total" : 3,
  "successful" : 3,
  "skipped" : 0,
  "failed" : 0
},

```

In that, do you have failed shards? Based on your description, I will bet painless is blowing up with an exception like `"No field found for [offer_price] in mapping"` and causing all the requests to shards for your two indexes that do not have `offer_price` to fail. You need to check if `doc` has the field in question in the mappings, not check for null. To do this, use `doc.containsKey('offer_price')`.

---

<div class="post-metadata">

**Author:** ![santiaguzz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santiaguzz/32/51379_2.png) [@santiaguzz](https://discuss.elastic.co/u/santiaguzz)\
**Post date:** [August 3, 2019, 11:09am UTC](https://discuss.elastic.co/t/painless-sorting-removing-documents-from-results/193215/3 "2019-08-03T11:09:03Z")

</div>

Hi Ryan;

Thanks for your response, as you said there are failed shards in the response, didn't realize so since there is no error thrown!

```
shards:
{ total: 3,
successful: 1,
skipped: 0,
failed: 2, [...] }

```

Using `doc.containsKey('offer_price')` as you suggested the documents are properly returned and sorted.

Thanks again!

---

<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 31, 2019, 11:09am UTC](https://discuss.elastic.co/t/painless-sorting-removing-documents-from-results/193215/4 "2019-08-31T11:09:04Z")

</div>

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