Empower Example Python and Elasticsearch Query

The following are sample Python and Elasticsearch queries.

import pandas as pd
import requests

# Search all empower injections
def get_all_empower_projects_injections(api_endpoint, token, org_slug):
    query = {
        "size": 10000,
        "query": {
            "term": {"data.project.name": "project\name"}
        }
    }

    query_string = json.dumps(query, indent=2)

    headers = {
        "Content-Type": "application/json",
        "ts-auth-token": token,
        "x-org-slug": org_slug
    }

    response = requests.request("POST", searchEQL_url, data=query_string, headers=headers)
    response.raise_for_status()
    return response
  

# find the sample set names that have injections with multiple results 
def get_sample_sets_with_injections_having_multiple_results(df):
    # with multiple results
    grouped = df.groupby("_source.fileId")
    
    series = grouped["result.id"].count()
    fileIds = series.gt(1).index.tolist()
    df = df[df["_source.fileId"].isin(fileIds)]
    return df["_source.data.sample.set.name"].unique()


api_endpoint = "https://api.tetrascience.com/v1/datalake/searchEql"
token = "<your service user token>"
org_slug = "<your org slug>"
  
list_of_injections = get_all_empower_projects_injections(api_endpoint, token, org_slug)
df = pd.json_normalize(list_of_injections, record_prefix='result.', record_path=['_source','data', 'results'], meta=[['_source', 'fileId'],['_source', 'data','sample', 'set', 'name']])
print(get_sample_sets_with_injections_having_multiple_results(df))

Did this page help you?