简体   繁体   中英

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.

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
      }
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM