Navid_pdp11
Navid_pdp11

Reputation: 4032

calculate count of distinct value in child array by elasticSearch?

I have an index by this structure:

class Note {
    public string Text {get; set;}
    public string[] Tags {get; set;}
}

I want to get the count of the usage of each distinct tag that is assigned to all notes. for example on this data :

[
    {
        "_id" : 1
        "text":"first text",
        "tags" : ["TagA", "TagB"]
    },

    {
       "_id" : 2
       "text": "second text",
       "tags" : ["TagA", "TagC"]
    }
]

I expect some result like this:

[
    {
      "Tag":"TagA",
      "count":2,
    },
   
   {
      "Tag":"TagB",
      "count":1,
   },
   
   {
      "Tag":"TagC",
      "count":1,
   }

]

Can I generate this result by ElasticSearch? and if the answer is 'YES' please guide me. also, I want to filter tags by some word that the user enters.

Update: this is mapping of my index:

{
  "Nots" : {
    "mappings" : {
      "properties" : {
        "tags" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "text" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
} 

Update 2:

I filtered the Entries by this code:

POST publishers_inventories/_search
{
  "size": 0, 
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "default_field": "tags.keyword",
            "query": "*تگ*"
          }
        }
      ]
    }
  },
  "aggs": {
    "distinct_tags": {
      "terms": {
        "field": "tags.keyword",
        "size": 200
      }
    }
  }
}

but now the result contains all tags that are included in the filtered docs. for example, if I search for the "Win" phrase it returns all docs that have "Win" in their tags but also all other phrases are placed beside "Win" in result docs.

Upvotes: 0

Views: 211

Answers (1)

Val
Val

Reputation: 217474

Yes, you can simply use a terms aggregation like this:

{
  "size": 0,
  "query": {
    "match": {
      "tags": "win"
    }
  },
  "aggs": {
    "distinct_tags": {
      "terms": {
        "field": "tags.keyword",
        "size": 10
      }
    }
  }
}

Upvotes: 3

Related Questions