Home > Software design >  How to get word count in docs as a aggregate over time in elastic search?
How to get word count in docs as a aggregate over time in elastic search?

Time:05-15

I am trying to get word count trends in docs as aggregate result . Although using the following approach I am able to get the doc count aggregation result but I am not able to find any resources using which I can get word count for the month of jan , feb & mar

PUT test/_doc/1
{
  "description" : "one two three four",
  "month" : "jan"

}
PUT test/_doc/2
{
  "description" : "one one test test test",
  "month" : "feb"

}

PUT test/_doc/3
{
  "description" : "one one one test",
  "month" : "mar"

}

GET test/_search
{
  "size": 0,
  "query": {
    "match": {
      "description": {
        "query": "one"
      }
    }
  },
  "aggs": {
    "monthly_count": {
      "terms": {
        "field": "month.keyword"
      }
    }
  }
}

OUTPUT

{
  "took" : 706,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "monthly_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "feb",
          "doc_count" : 1
        },
        {
          "key" : "jan",
          "doc_count" : 1
        },
        {
          "key" : "mar",
          "doc_count" : 1
        }
      ]
    }
  }
}

EXPECTED WORD COUNT OVER MONTH

"aggregations" : {
    "monthly_count" : {
      "buckets" : [
        {
          "key" : "feb",
          "word_count" : 2
        },
        {
          "key" : "jan",
          "word_count" : 1
        },
        {
          "key" : "mar",
          "word_count" : 3
        }
      ]
    }
  }

CodePudding user response:

Maybe this query can help you:

GET test/_search
{
  "size": 0,
  "aggs": {
    "monthly_count": {
      "terms": {
        "field": "month.keyword"
      },
      "aggs": {
        "count_word_one": {
          "terms": {
            "script": {
              "source": """
              def str = doc['description.keyword'].value;
              def array = str.splitOnToken(' ');
              int i = 0;
              for (item in array) {
                if(item == 'one'){
                  i  
                }
              }
              return i;
              """
            }, 
            "size": 10
          }
        }
      }
    }
  }
}

Response:

"aggregations" : {
    "monthly_count" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "feb",
          "doc_count" : 1,
          "count_word_one" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "2",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "jan",
          "doc_count" : 1,
          "count_word_one" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "1",
                "doc_count" : 1
              }
            ]
          }
        },
        {
          "key" : "mar",
          "doc_count" : 1,
          "count_word_one" : {
            "doc_count_error_upper_bound" : 0,
            "sum_other_doc_count" : 0,
            "buckets" : [
              {
                "key" : "3",
                "doc_count" : 1
              }
            ]
          }
        }
      ]
    }
  }
  • Related