# I need to show a catalog containing only the latest version of each app, sorted by app title

**URL:** <https://discuss.elastic.co/t/i-need-to-show-a-catalog-containing-only-the-latest-version-of-each-app-sorted-by-app-title/237989>\
**Category:** Elasticsearch\
**Created:** [June 22, 2020, 3:26am UTC](https://discuss.elastic.co/t/i-need-to-show-a-catalog-containing-only-the-latest-version-of-each-app-sorted-by-app-title/237989 "2020-06-22T03:26:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RNA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rna/32/70771_2.png) [@RNA](https://discuss.elastic.co/u/RNA)\
**Post date:** [June 22, 2020, 3:26am UTC](https://discuss.elastic.co/t/i-need-to-show-a-catalog-containing-only-the-latest-version-of-each-app-sorted-by-app-title/237989/1 "2020-06-22T03:26:23Z")

</div>

Dear Community,

Please help me out. I have an `app` catalogue page running with ES query. Each `app` has a semantic `version` and `title` in multiple `language`, and the `app title` can be different per `version`, then finally, I need to show a catalog containing only the `latest version` of each `apps` sorted by the `app title` of requested `language`.

So here is my sample data:

```
PUT trial_index/_doc/1
{   
    "version": "1.2.10",                    
    "major_version": 1.0,
    "minor_version": 2.0,                                        
    "patch_version": 10.0,
    "default_language": "english",                    
    "language_data": {
        "french": {                            
            "app_title": "app_1_title_revised_in_french"
        },                        
        "english": {                            
            "app_title": "app_1_title_revised"
        },                        
        "spanish": {                          
            "app_title": "app_1_title_revised_in_spanish"
        }
    },                    
    "unique_app_id": "app_no_1"
}

PUT trial_index/_doc/2
{   
    "version": "1.2.3",                    
    "major_version": 1.0,
    "minor_version": 2.0,                                        
    "patch_version": 3.0,
    "default_language": "english",                    
    "language_data": {
        "french": {                            
            "app_title": "app_1_title_in_french"                            
        },                        
        "english": {                            
            "app_title": "app_1_title"
        },                        
        "spanish": {                          
            "app_title": "app_1_title_in_spanish"
        }
    },                    
    "unique_app_id": "app_no_1"
}

```

You can see the `version` 1.2.3 and 1.2.10 have all different `title`. I need to show the latest `version` 1.2.10 in the catalogue, however, my closest sorting effort below generates a bucket per `title`, so it returns each `version` as if they are two different `apps`. How do I fix this?

```
GET trial_index/_search
{
    "from": 0,
    "size": 0,
    "aggregations": {
        "my_pager": {
            "composite": {
                "size": 100,
                "sources": [
                    {
                        "sort_by_title": {
                            "terms": {
                                "script": {
                                    "source": "def source = params['_source'];\nif (source['language_data'].containsKey(params.requestedCode)) {\n\treturn source['language_data'][params.requestedCode]['app_title']\n} else {\n\treturn source['language_data'][doc['default_language.keyword'].value]['app_title']\n}",
                                    "lang": "painless",
                                    "params": {
                                        "requestedCode": "french"
                                    }
                                },
                                "missing_bucket": true,
                                "order": "asc"
                            }
                        }
                    },
                    {
                        "page_key": {
                            "terms": {
                                "field": "unique_app_id.keyword",
                                "missing_bucket": false,
                                "order": "asc"
                            }
                        }
                    }
                ]
            },
            "aggregations": {
                "application": {
                    "terms": {
                        "field": "unique_app_id.keyword",
                        "size": 10,
                        "min_doc_count": 1,
                        "shard_min_doc_count": 0,
                        "show_term_doc_count_error": false,
                        "order": [
                            {
                                "_count": "desc"
                            },
                            {
                                "_key": "asc"
                            }
                        ]
                    },
                    "aggregations": {
                        "latest_version": {
                            "top_hits": {
                                "from": 0,
                                "size": 1,
                                "version": false,
                                "seq_no_primary_term": false,
                                "explain": false,
                                "_source": {
                                    "includes": [
                                        "unique_app_id",
                                        "version"
                                    ],
                                    "excludes": []
                                },
                                "script_fields": {
                                    "default_language": {
                                        "script": {
                                            "source": "[params['_source']['language_data'].containsKey(params.requestedCode) ? (params['_source']['language_data'][params.requestedCode]['app_title']) : (params['_source']['language_data'][doc['default_language.keyword'].value]['app_title'])]",
                                            "lang": "painless",
                                            "params": {
                                                "requestedCode": "french"
                                            }
                                        },
                                        "ignore_failure": false
                                    }
                                },
                                "sort": [
                                    {
                                        "major_version": {
                                            "order": "desc"
                                        }
                                    },
                                    {
                                        "minor_version": {
                                            "order": "desc"
                                        }
                                    },
                                    {
                                        "patch_version": {
                                            "order": "desc"
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            }
        }
    }
}
```

---

<div class="post-metadata">

**Author:** ![RNA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rna/32/70771_2.png) [@RNA](https://discuss.elastic.co/u/RNA)\
**Post date:** [June 22, 2020, 10:39am UTC](https://discuss.elastic.co/t/i-need-to-show-a-catalog-containing-only-the-latest-version-of-each-app-sorted-by-app-title/237989/2 "2020-06-22T10:39:59Z")

</div>

Please kindly help

---

<div class="post-metadata">

**Author:** ![RNA](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rna/32/70771_2.png) [@RNA](https://discuss.elastic.co/u/RNA)\
**Post date:** [June 23, 2020, 8:44am UTC](https://discuss.elastic.co/t/i-need-to-show-a-catalog-containing-only-the-latest-version-of-each-app-sorted-by-app-title/237989/3 "2020-06-23T08:44:35Z")

</div>

Anyone? Please let me know if there is anything to improve my question.

---

<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:** [July 21, 2020, 8:44am UTC](https://discuss.elastic.co/t/i-need-to-show-a-catalog-containing-only-the-latest-version-of-each-app-sorted-by-app-title/237989/4 "2020-07-21T08:44:39Z")

</div>

Closed on July 21, 2020, 8:44am UTC

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