Elastic Search grouping search results based on a field

34 Views Asked by At

Hi I want to group elastic search query result bases on a specific field. I have gone through collapse and aggregation doc but cant find how to achieve this. Here is an example Lets say I have three documents:-

  {
"id":"1"
"sub":"1_1a"
"name" : "TV",
"make" : "samsung",
"model":"plasma"}

{
"id":"2"
"subid":"1_2a"
"name" : "TV",
"make" : "samsung",
"model":"plasma_different_model"}

{
"id":"3"
"subid":"1_3a"
"name" : "TV",
"make" : "samsung",
"model":"plasma_another_different_model"}

I want to group my query result by subid with only 1st part of sub-id="1" splitting it from the underscore.

So my aim is to get only one doc as search result with "sub-id"="1" according to relevance. How can i achieve this?

1

There are 1 best solutions below

3
G0l0s On BEST ANSWER

You can do it by a runtime field in such query

GET /runtime_field_with_split/_search?filter_path=hits.hits
{
  "runtime_mappings": {
    "subid_prefix": {
      "type": "keyword",
      "script": """
        String[] subParts = /_/.split(doc['subid.keyword'].value);
        emit(subParts[0]);
      """
    }
  },
  "query": {
    "term": {
      "subid_prefix": "1"
    }
  },
  "fields": ["subid_prefix"]
}

Response

{
  "hits" : {
    "hits" : [
      {
        "_index" : "runtime_field_with_split",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "id" : "1",
          "subid" : "1_1a",
          "name" : "TV",
          "make" : "samsung",
          "model" : "plasma"
        },
        "fields" : {
          "subid_prefix" : [
            "1"
          ]
        }
      },
      {
        "_index" : "runtime_field_with_split",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "id" : "2",
          "subid" : "1_2a",
          "name" : "TV",
          "make" : "samsung",
          "model" : "plasma_different_model"
        },
        "fields" : {
          "subid_prefix" : [
            "1"
          ]
        }
      },
      {
        "_index" : "runtime_field_with_split",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "id" : "3",
          "subid" : "1_3a",
          "name" : "TV",
          "make" : "samsung",
          "model" : "plasma_another_different_model"
        },
        "fields" : {
          "subid_prefix" : [
            "1"
          ]
        }
      }
    ]
  }
}