Skip to content

Instantly share code, notes, and snippets.

@markjhoy
Created January 24, 2021 20:10
Show Gist options
  • Save markjhoy/7bcbb987461ff9ee511a59796c1a0684 to your computer and use it in GitHub Desktop.
Save markjhoy/7bcbb987461ff9ee511a59796c1a0684 to your computer and use it in GitHub Desktop.
Workplace Search - Analytics - Query Stats Transform
{
"source": {
"index": ".ent-search-workplace-search-analytics-ecs-ilm-logs-*"
},
"dest": {
"index": "workplace-search-analytics_query-stats"
},
"pivot": {
"group_by": {
"query_id": {
"terms": {
"field": "SearchMetrics.query.id"
}
}
},
"aggregations": {
"query_hash": {
"scripted_metric": {
"init_script": "state.query_hash=''",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.query') { state.query_hash = doc['SearchMetrics.query.query_hash'].value }",
"combine_script": "return state.query_hash",
"reduce_script": "return states[0]"
}
},
"query_value": {
"scripted_metric": {
"init_script": "state.query_value=''",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.query') { state.query_value = doc['SearchMetrics.query.value'].value }",
"combine_script": "return state.query_value",
"reduce_script": "return states[0]"
}
},
"query_refinements": {
"scripted_metric": {
"init_script": "state.query_refinements=''",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.query' && doc['SearchMetrics.query.refinements'].size()!=0) { state.query_refinements = doc['SearchMetrics.query.refinements'].value }",
"combine_script": "return state.query_refinements",
"reduce_script": "return states[0]"
}
},
"query_refinement_count": {
"scripted_metric": {
"init_script": "state.query_refinement_count=0",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.query') { state.query_refinement_count = doc['SearchMetrics.query.refinements'].size() }",
"combine_script": "return state.query_refinement_count",
"reduce_script": "return states[0]"
}
},
"query_filters": {
"scripted_metric": {
"init_script": "state.query_filters=''",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.query' && doc['SearchMetrics.query.filters'].size()!=0) { state.query_filters = doc['SearchMetrics.query.filters'].value }",
"combine_script": "return state.query_filters",
"reduce_script": "return states[0]"
}
},
"query_filter_count": {
"scripted_metric": {
"init_script": "state.query_filter_count=0",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.query') { state.query_filter_count = doc['SearchMetrics.query.filters'].size() }",
"combine_script": "return state.query_filter_count",
"reduce_script": "return states[0]"
}
},
"click_count": {
"scripted_metric": {
"init_script": "state.query_clicks=[];",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.click') { state.query_clicks.add(1) }",
"combine_script": "int click_counts = 0; for (c in state.query_clicks) { click_counts += c } return click_counts",
"reduce_script": "int click_counts = 0; for (q in states) { click_counts += q; } return click_counts"
}
},
"query_start_timestamp": {
"min": {
"field": "@timestamp"
}
},
"query_end_timestamp": {
"max": {
"field": "@timestamp"
}
},
"time_to_first_click_seconds": {
"scripted_metric": {
"init_script": "state.query_timestamp = 0; state.click_timestamps=[];",
"map_script": "if (doc['event.action'].value == 'SearchMetrics.click') { state.click_timestamps.add(doc['@timestamp'].value.toEpochSecond()) } if (doc['event.action'].value == 'SearchMetrics.query') { state.query_timestamp = doc['@timestamp'].value.toEpochSecond() }",
"combine_script": "ArrayList delta_values=[]; for (c in state.click_timestamps) { delta_values.add(c - state.query_timestamp) } return delta_values",
"reduce_script": "ArrayList delta_values=[]; for (t in states[0]) { delta_values.add(t) } if (delta_values.length==0) { return 0 } else { return delta_values[0] }"
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment