<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to count distinct items in nested fields? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591814#M205994</link>
    <description>&lt;P&gt;Is there any way to do this with rex? For example, I've tried&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex (?&amp;lt;opr&amp;gt;"(?&amp;lt;=op:) \w+")&lt;/LI-CODE&gt;&lt;P&gt;which should match any word after "op: ", no? But it's not returning any results for some reason.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 31 Mar 2022 15:55:36 GMT</pubDate>
    <dc:creator>aj_54321</dc:creator>
    <dc:date>2022-03-31T15:55:36Z</dc:date>
    <item>
      <title>How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591621#M205951</link>
      <description>&lt;P&gt;Hi! I can't seem to figure out how to get a count of each operation in a document like below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;{ [-]
   request_id: 12345
   revision: 123
   other_field: stuff
   my_precious: { [-]
     1648665400.774453: { [-]
       keys: [ [-]
         key:key1
       ]
       op: operation_1
     }
     1648665400.7817056: { [-]
       keys: [ [-]
         key:key2
       ]
       op: operation_2
     }
     1648665400.7847242: { [-]
       keys: [ [-]
         key:key4
       ]
       op: operation_1
     }
     1648665400.7886434: { [-]
     	keys: [ [-]
         key:key5
       ]
       op: operation_3
     }
     1648665400.7932374: { [-]
     	keys: [ [-]
         key:key3
       ]
       op: operation_2
     }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I want to be able to see the count of each operation. For example, the above would yield:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;operation_1: 2&lt;/P&gt;&lt;P&gt;operation_2: 2&lt;/P&gt;&lt;P&gt;operation_3: 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've tried with the following rex, which is unreliable tbh, as there could be other documents with " op: ". But not even the following works...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex (?&amp;lt;opr&amp;gt;"(?&amp;lt;= op: )\w+") |stats count by opr |&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated!&lt;/P&gt;</description>
      <pubDate>Wed, 30 Mar 2022 20:21:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591621#M205951</guid>
      <dc:creator>aj_54321</dc:creator>
      <dc:date>2022-03-30T20:21:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591627#M205955</link>
      <description>&lt;P&gt;This looks like it might be JSON except that you have shown the formatted version. Is it actually JSON? If so, can you share the raw JSON from the event in a code block (as you did with the formatted JSON)?&lt;/P&gt;</description>
      <pubDate>Wed, 30 Mar 2022 21:25:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591627#M205955</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2022-03-30T21:25:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591814#M205994</link>
      <description>&lt;P&gt;Is there any way to do this with rex? For example, I've tried&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex (?&amp;lt;opr&amp;gt;"(?&amp;lt;=op:) \w+")&lt;/LI-CODE&gt;&lt;P&gt;which should match any word after "op: ", no? But it's not returning any results for some reason.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 15:55:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591814#M205994</guid>
      <dc:creator>aj_54321</dc:creator>
      <dc:date>2022-03-31T15:55:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591834#M206000</link>
      <description>&lt;P&gt;rex works (by default) on the _raw field. What you have shared appears to be formatted JSON. Please can you confirm that your events have unformatted JSON or formatted JSON?&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 18:07:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591834#M206000</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2022-03-31T18:07:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591873#M206028</link>
      <description>&lt;P&gt;The event is formatted as JSON&lt;/P&gt;</description>
      <pubDate>Thu, 31 Mar 2022 20:53:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591873#M206028</guid>
      <dc:creator>aj_54321</dc:creator>
      <dc:date>2022-03-31T20:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591874#M206029</link>
      <description>&lt;P&gt;Give this a try&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rex max_match=0 "op:\s*(?&amp;lt;opr&amp;gt;\w+)" |stats count by opr

OR

| rex max_match=0 "\"op\":\s*\"(?&amp;lt;opr&amp;gt;[^\"]+)" |stats count by opr&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 31 Mar 2022 21:00:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591874#M206029</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2022-03-31T21:00:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591919#M206046</link>
      <description>&lt;P&gt;What&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;&amp;nbsp;suggests is that if event is conformant JSON, you would have already fields like&amp;nbsp;my_precious.1648665400.774453.op,&amp;nbsp;my_precious.1648665400.7817056.op, etc; if not, apply "| spath". (C.f.,&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath" target="_blank" rel="noopener"&gt;spath&lt;/A&gt;. &amp;nbsp;It is more reliable to use builtin functions to extract structured data.)&lt;/P&gt;&lt;P&gt;To count by op values, use&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Mvexpand" target="_blank" rel="noopener"&gt;mvexpand&lt;/A&gt;. &amp;nbsp;Because &amp;nbsp;your op fields have deep paths, you also need to flatten them with rename, then use&amp;nbsp;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Foreach" target="_blank" rel="noopener"&gt;foreach&lt;/A&gt;&amp;nbsp;to consolidate them into a single field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| rename my_precious.*.*.op as *_*_op ``` flatten paths ```
| foreach *_op ``` consolidate op ```
    [eval my_op=mvappend(my_op, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')]
| mvexpand my_op
| stats count by my_op&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;With sample data, output is&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;my_op&lt;/TD&gt;&lt;TD&gt;count&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;operation_1&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;operation_2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;operation_3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Fri, 01 Apr 2022 09:03:53 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/591919#M206046</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2022-04-01T09:03:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/592232#M206141</link>
      <description>&lt;P&gt;Thanks for this. Is there a way to avoid empty values? Empty values are accounting for most of the results.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Apr 2022 15:04:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/592232#M206141</guid>
      <dc:creator>aj_54321</dc:creator>
      <dc:date>2022-04-04T15:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to count distinct items in nested fields?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/592234#M206142</link>
      <description>&lt;P&gt;What do you mean by "empty" values? If it is an empty string (as opposed to the field not being there), you could add an if condition to the eval&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| foreach *_op ``` consolidate op ```
    [eval my_op=if('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'=="",my_op,mvappend(my_op, '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;'))]&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 04 Apr 2022 15:13:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-count-distinct-items-in-nested-fields/m-p/592234#M206142</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2022-04-04T15:13:00Z</dc:date>
    </item>
  </channel>
</rss>

