0

I have created mapping using elasticsearch. Here is the mapping properties

"properties": {
         "userPermissions": {
            "type": "nested",
            "properties": {
                "prm": {
                    "type": "string"
                },
                "id": {
                    "type": "string"
                }
            }
           },
         "pSPermissions": {
            "type": "nested",
            "properties": {
                "prm": {
                "type": "string"
                },
                "id": {
                "type": "string"
                }
            }
         }
      }

I want to retrieve overall distinct items from these fields: userPermissions.id, pSPermissions.id.

I can achieve distinct values of multiple fields under a single path. We need to use a script to retrieve terms from multiple fields.

GET /permissions/perm/_search?pretty=true&search_type=count

    {
      "aggs": {
        "Parents": {
          "nested": {
            "path": "userPermissions"
          },
          "aggs": {
            "permCount": {
              "terms": {
                "script": "[doc['userPermissions.id'].value,doc['userPermissions.prm'].value]",
                "size": 5000
              }
            }
          }
        }
      }
    }

But I have no idea how to achieve across different paths userPermissions and pSPermissions. Is it achievable?

1 Answer 1

1

You can achieve it using the following script:

"script": "doc['userPermissions.id'].values + doc['userPermissions.prm'].values",

What it does is retrieve all the userPermissions.id values and all the userPermissions.prm values and then concatenate them into a single array using groovy's + operator.

UPDATE

Following up on your comment, you can achieve what you want with this script in a similar way as you're already doing for fields under the same path:

"script": "doc['userPermissions.id'].values + doc['pSPermissions.id'].values",
Sign up to request clarification or add additional context in comments.

1 Comment

I have achieved already what you are suggesting. The problem what I have is, I want concatenated values of userPermissions.id and pSPermissions.id into a single array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.