question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question: count all and count in specific date range

See original GitHub issue

Hello, sorry for the noise.I’ve not been able to add a new metric for a specific date range. I need to count two things. The first one is count by month and I could do it. But the second one is to count in the last five months.

The code that is workings is:

now = timezone.now()
five_month_ago = now - relativedelta(months=5)
 
query_banks = Q("terms", bank__id=ids_of_banks)
aggs_by_month = A('date_histogram', field='created_at', interval='month', format='YYYY-MM')
search = search.query("nested", path="bank", query=[1, 2, 3])     
search.aggs.metric('count', 'cardinality', field='id')
search.aggs.bucket('by_month', aggs_by_month)
result = search.execute()

I tried a lot of things, for counting the last five month without success.

aggs_five_month = A('filter', Q('range', created_at={'gt': five_month_ago, 'lt': now}))
search.aggs.bucket('last_five_months', aggs_five_month).metric('count', 'cardinality', field='id')

Is it possible to do both things with one query?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
honzakralcommented, Aug 28, 2018

This should do the trick:

from elasticsearch_dsl import Search
s = Search()
s.aggs.bucket('per_month', 'date_histogram', field='created_at', interval='month')\
    .metric('count', 'cardinality', field='id')
s.aggs.bucket('last_5_months', 'filter', Q('range', created_at={'gt': 'now-5m'}))\
    .metric('count', 'cardinality', field='id')

It will give you a count per month and also for the last 5 months.

Note that you also have some weird constructs with the nested query. If bank is a nested field then you need to query for the bank ids as:

s = s.query('nested', path='bank', query=Q('terms', bank__id=[1, 2, 3]))

if not then just:

s = s.query('terms', bank__id=[1, 2, 3])

Hope this helps!

0reactions
eduzencommented, Aug 30, 2018

👍 😃 Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Query to Count Items Within Date Range? - Stack Overflow
This assumes that all dates are valid. If some dates are special, you need to take that into account in the order by...
Read more >
SQL count(*) grouped by date range - DBA Stack Exchange
You can use the APPLY operator to count the number of employees active at the termination date. As I looked at the question...
Read more >
Count cells between dates - Excel formula - Got It AI
Excel allows us to count cells between dates by using the COUNTIFS function. The COUNTIFS function counts the number of cells that meet...
Read more >
Excel COUNTIF function examples - not blank, greater than ...
Count dates greater than or equal to a date in another cell, minus x days. =COUNTIF(B2:B10,">="&B2-"7"), Count the number of cells in the...
Read more >
Count the number of times a month appears within in a range!
Do you know how to count the number of times a specific month appears in a range of dates ? This scenario is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found