0

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.

1 Answer 1

3

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
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

and can I add some filter on this result? for example tags, those contain "win" phrase?
I tested it but the filter section did not work :(
Please update your question with the details of your index mapping
I filter the result by ** "query": { "bool": { "must": [ { "query_string": { "default_field": "tags.keyword", "query": "تگ" } } ] } }**
if you use query_string you need to query on the tags field, not the tags.keyword field.

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.