# The problem about ordering in terms

**URL:** <https://discuss.elastic.co/t/the-problem-about-ordering-in-terms/122840>\
**Category:** Elasticsearch\
**Created:** [March 7, 2018, 7:42am UTC](https://discuss.elastic.co/t/the-problem-about-ordering-in-terms/122840 "2018-03-07T07:42:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PENG\_ZHANG](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@PENG\_ZHANG](https://discuss.elastic.co/u/PENG_ZHANG)\
**Post date:** [March 7, 2018, 7:42am UTC](https://discuss.elastic.co/t/the-problem-about-ordering-in-terms/122840/1 "2018-03-07T07:42:15Z")

</div>

### 1. FunctionDescription

* * *

1. Use terms to generate multiple buckets.
2. Sort the buckets using the aggregated result.

### 2. My Previous Solution

* * *

1. At first, I use terms-\>order to sort buckets.

2. Then, part of the aggregate result must be achieved using bucket\_script and need to support the paging function. The terms-\>sort can not be used. I use the bucket\_sort to achieve the functions.

3. But soon discovered a problem.

### 3. Please Help

* * *

- For the above problem, I need your good idea. Thanks a lot.

### 4. Code

* * *

```auto
"pv3": {
              "terms": {
                "field": "scene.cutpid",
                "size": 20,
                "shard_size": 1000,
                "order": {
                  "probability_exit": "desc"
                }
              },
              "aggs": {
                "vi": {
                  "reverse_nested": {},
                  "aggs": {
                    "visits": {
                      "filter": {
                        "exists": {
                          "field": "vid"
                        }
                      },
                      "aggs": {
                        "visits_value": {
                          "value_count": {
                            "field": "vid"
                          }
                        }
                      }
                    },
                    "new_visits": {
                      "filter": {
                        "term": {
                          "nv": true
                        }
                      },
                      "aggs": {
                        "new_visits_value": {
                          "value_count": {
                            "field": "vid"
                          }
                        }
                      }
                    },
                    "bounce": {
                      "filter": {
                        "term": {
                          "bounce": true
                        }
                      },
                      "aggs": {
                        "bounce_value": {
                          "value_count": {
                            "field": "vid"
                          }
                        }
                      }
                    }
                  }
                },
                "url_title_pid_cutpath": {
                  "top_hits": {
                    "from": 0, 
                    "size": 1,
                    "_source": {
                      "includes": [
                        "scene.fullpath",
                        "scene.title",
                        "scene.pid",
                        "scene.cutpath"
                      ]
                    }
                  }
                },
                "st": {
                  "filter": {
                    "range": {
                      "scene.staytm": {
                        "gte": 0
                      }
                    }
                  },
                  "aggs": {
                    "stay_time": {
                      "sum": {
                        "field": "scene.staytm"
                      }
                    }
                  }
                },
                "page_visits": {
                  "filter": {
                    "exists": {
                      "field": "scene.pid"
                    }
                  },
                  "aggs": {
                    "page_visits_value": {
                      "value_count": {
                        "field": "scene.pid"
                      }
                    }
                  }
                },
                "land": {
                  "filter": {
                    "term": {
                      "scene.is_land": true
                    }
                  },
                  "aggs": {
                    "land_value": {
                      "value_count": {
                        "field": "scene.pid"
                      }
                    }
                  }
                },
                "exit": {
                  "filter": {
                    "term": {
                      "scene.is_exit": true
                    }
                  },
                  "aggs": {
                    "exit_value": {
                      "value_count": {
                        "field": "scene.pid"
                      }
                    }
                  }
                },
                "r_bucket_sort": {
                  "bucket_sort": {
                    "from": 10,
                    "size": 10
                  }
                },
                "probability_new_visits": {
                  "bucket_script": {
                    "buckets_path": {
                      "visits": "vi>visits>visits_value",
                      "new_visits": "vi>new_visits>new_visits_value"
                    },
                    "script": "(params.visits==0)?0:(params.new_visits/params.visits)"
                  }
                },
                "probability_exit": {
                  "bucket_script": {
                    "buckets_path": {
                      "exit": "exit>exit_value",
                      "page_visits": "page_visits>page_visits_value"
                    },
                    "script": "(params.page_visits==0)?0:(params.exit/params.page_visits)"
                  }
                },
                "probability_bounce": {
                  "bucket_script": {
                    "buckets_path": {
                      "bounce": "vi>bounce>bounce_value",
                      "land": "land>land_value"
                    },
                    "script": "(params.land==0)?0:(params.bounce/params.land)"
                  }
                },
                "avg_staytime": {
                  "bucket_script": {
                    "buckets_path": {
                      "page_visits": "page_visits>page_visits_value",
                      "staytime": "st>stay_time"
                    },
                    "script": "(params.page_visits==0)?0:(params.staytime/(params.page_visits*1000))"
                  }
                },
                "cutpid_count": {
                  "cardinality": {
                    "field": "scene.cutpid",
                    "precision_threshold": 10000
                  }
                }
              }
            }

```

---

<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:** [April 4, 2018, 7:42am UTC](https://discuss.elastic.co/t/the-problem-about-ordering-in-terms/122840/2 "2018-04-04T07:42:19Z")

</div>

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