# Help with query please

**URL:** <https://discuss.elastic.co/t/help-with-query-please/338191>\
**Category:** Elasticsearch\
**Created:** [July 12, 2023, 8:59am UTC](https://discuss.elastic.co/t/help-with-query-please/338191 "2023-07-12T08:59:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lakhr034](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lakhr034/32/123326_2.png) [@lakhr034](https://discuss.elastic.co/u/lakhr034)\
**Post date:** [July 12, 2023, 8:59am UTC](https://discuss.elastic.co/t/help-with-query-please/338191/1 "2023-07-12T08:59:04Z")

</div>

```auto
POST user_info,user_auth_cards_info/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        {
          "multi_match": {
            "query": "test",
            "fields": [
              "e_name.autocomplete",
              "cards_user_name.autocomplete"
            ]
          }
        },
        {
          "script": {
            "script": {
              "source": "def value; if(doc['e_name.raw'].size() != 0){value = doc['e_name.raw'].value} else if(doc['cards_user_name.raw'].size() != 0 ){value = doc['cards_user_name.raw'].value} return (value.startsWith(params['e_name']) || value.contains(params['e_name'])) ",
              "params": {
                "e_name" : "test"
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "pagination": {
      "composite": {
        "size": 2,
        "sources": [
          { "user_id": { "terms": { "field": "user_id" } } }
        ],
        "after": {"user_id" : "2"}
      },
      "aggregations": {
        "top_names": {
          "top_hits": {
            "size": 2,
            "_source": {
              "includes": [
                "e_name",
                "cards_user_name",
                "status",
                "del_status",
                "user_id",
                "frog_id",
                "telphone"
              ]
            },
            "sort": [
              {
                "_script": {
                  "type": "number",
                  "script": {
                    "source": "if (doc['e_name.raw'].size() != 0) { return doc['e_name.raw'].value.length() } else if (doc['cards_user_name.raw'].size() != 0) { return doc['cards_user_name.raw'].value.length() }",
                    "params": {
                      "e_name": "test"
                    }
                  },
                  "order": "asc"
                }
              }
            ]
          }
        },
        "top_names_sort": {
          "min": {
            "script": {
              "source": "if (doc['e_name.raw'].size() != 0) { return doc['e_name.raw'].value.length() } else if (doc['cards_user_name.raw'].size() != 0) { return doc['cards_user_name.raw'].value.length() }",
              "params": {
                "e_name": "test"
              }
            }
          }
        }
      }
    }
  }
}

```

I have this elasticsearch query, I'm just trying to group by id and then sort the data by the smallest strings for e\_name or cards\_user\_name

This is the results:

```auto
"aggregations": {
    "pagination": {
      "after_key": {
        "user_id": 5
      },
      "buckets": [
        {
          "key": {
            "user_id": 1
          },
          "doc_count": 1,
          "top_names": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": null,
              "hits": [
                {
                  "_index": "user_info",
                  "_id": "8",
                  "_score": null,
                  "_source": {
                    "e_name": "test1211",
                    "user_id": 1,
                    "frog_id": 1,
                    "telphone": "12345",
                    "status": 1
                  },
                  "sort": [
                    8
                  ]
                }
              ]
            }
          },
          "top_names_sort": {
            "value": 8
          }
        },
        {
          "key": {
            "user_id": 2
          },
          "doc_count": 1,
          "top_names": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": null,
              "hits": [
                {
                  "_index": "user_info",
                  "_id": "9",
                  "_score": null,
                  "_source": {
                    "e_name": "test21212",
                    "user_id": 2,
                    "frog_id": 1,
                    "telphone": "12345",
                    "status": 1
                  },
                  "sort": [
                    9
                  ]
                }
              ]
            }
          },
          "top_names_sort": {
            "value": 9
          }
        },
        {
          "key": {
            "user_id": 3
          },
          "doc_count": 2,
          "top_names": {
            "hits": {
              "total": {
                "value": 2,
                "relation": "eq"
              },
              "max_score": null,
              "hits": [
                {
                  "_index": "user_info",
                  "_id": "10",
                  "_score": null,
                  "_source": {
                    "e_name": "test3",
                    "user_id": 3,
                    "frog_id": 1,
                    "telphone": "12345",
                    "status": 1
                  },
                  "sort": [
                    5
                  ]
                },
                {
                  "_index": "user_auth_cards_info",
                  "_id": "21",
                  "_score": null,
                  "_source": {
                    "user_id": 3,
                    "del_status": 0,
                    "cards_user_name": "test345"
                  },
                  "sort": [
                    7
                  ]
                }
              ]
            }
          },
          "top_names_sort": {
            "value": 5
          }
        },
        {
          "key": {
            "user_id": 4
          },
          "doc_count": 1,
          "top_names": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": null,
              "hits": [
                {
                  "_index": "user_info",
                  "_id": "11",
                  "_score": null,
                  "_source": {
                    "e_name": "test44",
                    "user_id": 4,
                    "frog_id": 1,
                    "telphone": "12345",
                    "status": 1
                  },
                  "sort": [
                    6
                  ]
                }
              ]
            }
          },
          "top_names_sort": {
            "value": 6
          }
        },
        {
          "key": {
            "user_id": 5
          },
          "doc_count": 1,
          "top_names": {
            "hits": {
              "total": {
                "value": 1,
                "relation": "eq"
              },
              "max_score": null,
              "hits": [
                {
                  "_index": "user_info",
                  "_id": "12",
                  "_score": null,
                  "_source": {
                    "e_name": "test55",
                    "user_id": 5,
                    "frog_id": 1,
                    "telphone": "12345",
                    "status": 1
                  },
                  "sort": [
                    6
                  ]
                }
              ]
            }
          },
          "top_names_sort": {
            "value": 6
          }
        }
      ]
    }
  }
}

```

I'm expecting test 3 to be at the top but it isn't. Not sure how I can go about grouping this query, sorting but also maintaining pagination.

---

<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 9, 2023, 8:59am UTC](https://discuss.elastic.co/t/help-with-query-please/338191/2 "2023-08-09T08:59:20Z")

</div>

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