PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to get aggregate result in ElasticSearch?

 October 25, 2022     aggregation, elasticsearch     No comments   

Issue

I need to get distinct values matching some condition in ElasticSearch where the data in this format. The SQL version will be.

select distinct kind_id from xxx where timestamp < date_add(now(), interval -1 day)

The data in elasticsearch is

{
    "id": "19504ec6bacd46aca302dc7e848aa8a1",
    "@kind": "some_data",
    "@kind_id": 4,
    "timestamp": "2022-09-06T00:02:36.697Z",
    "data": "some data"
}

Solution

You can use the terms aggregation on your kind_id field to get the required results.

{
    "size":0,
    "query": {
        "range": {
            "timestamp": {
                "gte": "now-1d/d",
                "lt": "now"
            }
        }
    },
    "aggs": {
        "distinct_kind": {
            "terms": {
                "field": "@kind_id"
            }
        }
    }
}
   


Answered By - Amit
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing