0

I have Elasticsearch documents for products with tags. Following is the structure of the documents:

{
    "id": 'id-1',
    "name: "prod-1",
    "tags": [
        {
            "id": 'tag-id-1',
            "name": 'tag-name-1'
        },
        {
            "id": 'tag-id-2',
            "name": 'tag-name-2'
        }
    ]
}

What I want to do is:

  1. Get all products with maximum overlapping tags (something like related questions in stackoverflow, assuming they have stored questions in Elasticsearch with tags). Output something like this:

    {
        "aggregations": {
            "products": [
                {
                    "key": "product-name-1",
                    "tags": [
                        {
                            "key": "tag-name-1",
                        }
                        {
                            "key": "tag-name-2"
                        }
                    ]
                },
                {
                    "key": "product-name-2",
                    "tags": [
                        {
                            "key": "tag-name-2",
                        }
                        {
                            "key": "tag-name-3"
                        }
                    ]
                },
            ]
        }
    }
    
  2. Get all tags grouped together with a tag maximum number of times.

    {
        "aggregations": {
            "tags": [
                {
                    "key": "tag-name-1",
                    "tags": [
                        {
                            "key": "tag-name-2",
                        }
                        {
                            "key": "tag-name-3"
                        }
                    ]
                },
                {
                    "key": "product-name-2",
                    "tags": [
                        {
                            "key": "tag-name-1",
                        }
                        {
                            "key": "tag-name-5"
                        }
                    ]
                },
            ]
        }
    }
    

I am not very sure which type of aggregation methods to use. Any help will be useful.

Thanks.

khushalbokadey
  • 1,132
  • 1
  • 11
  • 25
  • Can you please share the sample output you want – Richa Jul 07 '16 at 06:37
  • @Richa: I have updated the question. – khushalbokadey Jul 07 '16 at 07:00
  • I don't think your output sample explains much.. What do you mean by overlapping tags? In second output, you have one tag key and one product key. This is not possible. – Richa Jul 07 '16 at 07:33
  • @Richa 2nd one I thought that it is not possible. Example of overlapping tags: This questions is having two tags `elasticsearch` and `elasticsearch-2.0`. So all questions which are having both the tags should come first, then questions having either of the tags should come (along with the tags). I hope its clear. – khushalbokadey Jul 07 '16 at 07:40

0 Answers0