Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experiment: Generic ElasticSearch query #20

Closed
wants to merge 1 commit into from

Conversation

rsevilla87
Copy link
Member

@rsevilla87 rsevilla87 commented Feb 19, 2024

Type of change

  • Refactor
  • New feature
  • Bug fix
  • Optimization
  • Documentation Update

Description

Adding a new generic_search method to retrieve data from arbitrary queries, arbitrary buckets and arbitrary aggregations. Example of result after running a testing script against our aws dev instance

https://gist.github.com/rsevilla87/ea63357d1243e1614a2c276eb09c032c

Some benefits:

  • Just one query get data broken down by a metadata field.
  • Permits nested buckets/metrics, they can be specified with a dot notation, example metadata.ocpMajorVersion.keyword

It's major downside can be the format of the data returned, it's a single JSON document with lots of nested fields, (one per term/metric aggregation. Good thing is that the value of the metric aggregation is returned at the lowest level and the key names are predictable, in this case:

 "aggregations": {
    "metadata.platform.keyword": {
      "buckets": [
        {
          "key": "BareMetal",
          "metadata.sdnType.keyword": {
            "buckets": [
              {
                "key": "OVNKubernetes",
                "metadata.ocpMajorVersion.keyword": {
                  "buckets": [
                    {
                      "key": "4.14",
                      "avg-latency": {
                        "value": 2260.4151167980954
                      }
                    },
                    {
                      "key": "4.13",
                      "avg-latency": {
                        "value": 11727.016764266851
                      }
                    }
                  ]
                }
              }
            ]
          }
        },

Thoughts??

Signed-off-by: Raul Sevilla <[email protected]>
@rsevilla87
Copy link
Member Author

cc: @shashank-boyapally @jtaleric

@@ -7,12 +7,12 @@
import logging

# pylint: disable=import-error
from elasticsearch import Elasticsearch
from elasticsearch7 import Elasticsearch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curious about this change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think elasticsearch7 can be installed using elasticsearch==7.x.x, this has been a previous issue for me too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, if you don't specify the module version, the python interpreter just picks the latest one available

else:
q = q & Q("match", **query)
s.query = q
x = None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
x = None
x = None
for bucket in buckets:
a = A("terms", field=bucket)
if x is None:
x = s.aggs.bucket(bucket, a)
else:
x = x.bucket(bucket, a)
x.bucket(metric_name, A(agg_type, field=field))

@@ -38,6 +38,39 @@ def __init__(
self.es = Elasticsearch([self.es_url], timeout=30)
self.data = None

def generic_search(self, queries: list, buckets: list, metric_name: str, agg_type: str, field: str):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I like this idea. Should be useful in reducing multiple queries to one on a single ES index atleast.

@rsevilla87 rsevilla87 closed this Jul 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants