Skip to content

Instantly share code, notes, and snippets.

@markjhoy
Last active January 24, 2021 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markjhoy/51e849189ef480abd5dd010abe195726 to your computer and use it in GitHub Desktop.
Save markjhoy/51e849189ef480abd5dd010abe195726 to your computer and use it in GitHub Desktop.
Elastic Workplace Search - Vega Configuration for Top Queries With No Clicks
{
$schema: https://vega.github.io/schema/vega-lite/v4.json
title: Top Queries With No Clicks
data: {
url: {
index: .ent-search-workplace-search-analytics-ecs-ilm-logs-*
body: {
query: {
bool: {
must: [
"%dashboard_context-must_clause%"
{
range: {
@timestamp: {
"%timefilter%": true
}
}
}
]
}
},
aggs: {
"queries_no_clicks": {
"scripted_metric": {
"params": {
"return_count": 5
},
"init_script": "state.queries=new HashMap(); state.query_clicks=new HashMap(); ",
"map_script": "String query_id = doc['SearchMetrics.query.id'].value; if (state.queries[query_id] == null) { state.queries[query_id] = [ 'query_value':'', 'click_count':0 ] } if (doc['event.action'].value == 'SearchMetrics.query') { state.queries[query_id]['query_value'] = doc['SearchMetrics.query.value'].value } else { state.queries[query_id]['click_count'] += 1 }",
"combine_script": "for (q in state.queries.entrySet()) { String query_value = q.getValue()['query_value']; int click_count = q.getValue()['click_count']; if (state.query_clicks[query_value] == null) { state.query_clicks[query_value] = [ 'click_count': click_count, 'query_count': 1] } else { state.query_clicks[query_value]['click_count'] += click_count; state.query_clicks[query_value]['query_count'] += 1; } } return state.query_clicks",
"reduce_script": "HashMap final_counts=new HashMap(); for (c in states[0].entrySet()) { String query_value = c.getKey(); int click_count = c.getValue()['click_count']; int query_count = c.getValue()['query_count']; if (final_counts[query_value] == null) { final_counts[query_value] = [ 'click_count': click_count, 'query_count': query_count] } else { final_counts[query_value]['click_count'] += click_count; final_counts[query_value]['query_count'] += query_count; } } def final_output=[]; for (c in final_counts.entrySet()) { String query_value = c.getKey(); int click_count = c.getValue()['click_count']; int query_count = c.getValue()['query_count']; if (click_count == 0) { final_output.add(['query': query_value, 'count': query_count]) } } final_output.sort((x,y) -> y['count'] - x['count']); return final_output.subList(0, params['return_count'])"
}
}
},
size: 0
}
}
format: {property: "aggregations.queries_no_clicks.value"}
}
mark: bar
encoding: {
x: {
field: count
type: quantitative
axis: {title: "Query Count"}
}
y: {
field: query
type: nominal
axis: {title: "Query"},
sort: -x
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment