Help with query please

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:

"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.

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