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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions fmatch/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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



# pylint: disable=import-error
import pandas as pd
from elasticsearch_dsl import Search, Q
from elasticsearch_dsl import Search, Q, A


class Matcher:
Expand All @@ -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.

"""
Function for performing a generic search using the specified queries, buckets, metric name, aggregation type, and field.
Args:
queries (list): List of queries to be used in the search.
buckets (list): List of buckets to be used in the aggregation.
metric_name (str): Name of the metric to be aggregated.
agg_type (str): Type of aggregation to be performed.
field (str): Field on which the aggregation will be applied.
Returns:
dict: Dictionary containing the results of the search and aggregation.
"""
s = Search(using=self.es, index=self.index)
q = None
for query in queries:
if not q:
q = Q("match", **(query))
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))

for bucket in buckets:
if x is None:
a = A("terms", field=buckets[0])
x = s.aggs.bucket(buckets[0], a)
else:
a = A("terms", field=bucket)
x = x.bucket(bucket, a)
x.bucket(metric_name, A(agg_type, field=field))
s = s[:0]
return s.execute().to_dict()


def get_metadata_by_uuid(self, uuid, index=None):
"""Returns back metadata when uuid is given

Expand Down
Loading