Getting Data In

Why Return items not present in a subsearch?

psimoes
Loves-to-Learn

Given the simple scenario:

I have users in a platform that have actions, I want to return all the users that haven't performed a specific action. For example, I want to return all users that have been doing the action "View", but haven't performed any action "Purchase". The index would be the same.

Pseudo query:

index=A action=view [ search index=A action=purchase | stats count by user ] | where count by user = 0 

Any idea how could I write such query?

Labels (1)
Tags (1)
0 Karma

woodcock
Esteemed Legend

Like this:
| makeresults
| eval _raw="
user action
A view
A purchase
B view
C purchase"
| multikv forceheader=1
| stats count(eval(action="view")) AS views count(eval(action="purchase")) AS purchases BY user
| where views>0 AND purchases==0

So like this:
index="A" AND (action="view" OR action="purchase")
| stats count(eval(action="view")) AS views count(eval(action="purchase")) AS purchases BY user
| where views>0 AND purchases==0

0 Karma

manjunathmeti
Champion

hi @psimoes ,

As @yeahnah and @gcusello mentioned, you don't need a sub-search for this unless your action=purchage events are in a different index/sourcetype.

Try this.

| makeresults 
| eval _raw="user,action
A,view
B,view
C,view
C,purchase
D,purchase" 
| multikv forceheader=1 
| fields - _raw, linecount 
| eventstats count(eval(action="view")) as view_count, count(eval(action="purchase")) as purchase_count by user 
| where purchase_count=0


 

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @psimoes,

as @yeahnah said, this is an incorrect way to use subsearches and anyway, you don't need a subsearch for your purpose.

Please try something like this:

index=A (action=view OR action=purchase)
| stats dc(action) AS action_count values(action) AS action BY user 
| where action_count=1 AND action=view

 Ciao.

Giuseppe

0 Karma

yeahnah
Motivator

Hi @psimoes 

No need for a subsearch to do this - avoid subsearches where possible as less efficient.

Try this...

index=A action=view OR action=purchase
| stats count values(action) AS actions BY user
| eval purchase_made=if(isnotnull(mvfilter(match(actions, "purchase"))), "yes", "no")
| where purchase_made="no"

The actions field is a multivalue field and the if statement tests whether this field contains the purchase value or not, before the where filter is applied.

Hope it helps

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...

Splunkbase Unveils New App Listing Management Public Preview

Splunkbase Unveils New App Listing Management Public PreviewWe're thrilled to announce the public preview of ...

Leveraging Automated Threat Analysis Across the Splunk Ecosystem

Are you leveraging automation to its fullest potential in your threat detection strategy?Our upcoming Security ...