<?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: Set the index with a field when using collect command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/745166#M241374</link>
    <description>&lt;P&gt;How to understand: "report_to_map_through_indexes",&lt;BR /&gt;I tried to built a macro but got server error&amp;nbsp; or shall it become a custom command? or how tp implement?&lt;/P&gt;</description>
    <pubDate>Tue, 29 Apr 2025 09:11:20 GMT</pubDate>
    <dc:creator>Petermann</dc:creator>
    <dc:date>2025-04-29T09:11:20Z</dc:date>
    <item>
      <title>Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687113#M234356</link>
      <description>&lt;P&gt;Hello!&lt;/P&gt;&lt;P&gt;I'm looking to set the index parameter of the collect command with the value of a field from each event.&lt;/P&gt;&lt;P&gt;Here's an example.&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;| makeresults count=2
| streamstats count
| eval index = case(count=1, "myindex1", count=2, "myindex2")
| collect index=index testmode=true&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This search creates two events. Both events have the index field, one with "myindex1" as the value, and the other with "myindex2". I would like to use these values to set the index in the collect command.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2024 00:39:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687113#M234356</guid>
      <dc:creator>ejwade</dc:creator>
      <dc:date>2024-05-10T00:39:23Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687120#M234361</link>
      <description>&lt;P&gt;I don't believe it is possible to do - you can in theory do this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=_audit
| head 1
| eval message="hello"
| table user action message
| collect testmode=f [ | makeresults | fields - _time | eval index="main" | format "" "" "" "" "" ""]&lt;/LI-CODE&gt;&lt;P&gt;but you would need for the subsearch to know the index to select and that is run before the outer search, so you can't do what you are trying to do&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2024 01:29:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687120#M234361</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2024-05-10T01:29:34Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687127#M234362</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/160850"&gt;@ejwade&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;I'm with&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;&amp;nbsp;on this - I don't think it's possible to run | collect with multiple index locations.&lt;BR /&gt;&lt;BR /&gt;You could do this instead:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults count=2
| streamstats count
| eval index = case(count=1, "myindex1", count=2, "myindex2")
| appendpipe[| search index="myindex1"| collect index=myindex1]
| appendpipe[| search index="myindex2"| collect index=myindex2]&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;You will need an &lt;EM&gt;appendpipe&lt;/EM&gt; command for each index you want to export to, but you should know the destination indexes in advance anyway.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2024 05:15:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687127#M234362</guid>
      <dc:creator>danspav</dc:creator>
      <dc:date>2024-05-10T05:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687128#M234363</link>
      <description>&lt;P&gt;You can't. Even with output_format=hec you can specify some metadata fields like source or sourcetype (which can affect your license usage) but the destination index has to be provided explicitly with the collect command invocation.&lt;/P&gt;</description>
      <pubDate>Fri, 10 May 2024 05:34:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687128#M234363</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2024-05-10T05:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687351#M234437</link>
      <description>&lt;P&gt;After tooling with it more, I think the best approach uses the map command.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults count=2
| streamstats count
| eval index = case(count=1, "myindex1", count=2, "myindex2")
| outputlookup lookup_of_events
| stats
    count
    by index
| map report_to_map_through_indexes&lt;/LI-CODE&gt;&lt;P&gt;report_to_map_through_indexes&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| inputlookup lookup_of_events
    where index="$index$"
| collect index="$index$"&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 13 May 2024 15:44:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687351#M234437</guid>
      <dc:creator>ejwade</dc:creator>
      <dc:date>2024-05-13T15:44:17Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687366#M234439</link>
      <description>&lt;P&gt;Be aware that map is a potentially unsafe command.&lt;/P&gt;&lt;P&gt;Also your approach with both map and an intermediate lookup seems strange. That's what passing fields to the subsearch is for.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 17:51:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687366#M234439</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2024-05-13T17:51:22Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687392#M234446</link>
      <description>&lt;P&gt;It can be done with map, but the phrase &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;best approach uses the map command&lt;/FONT&gt;&amp;nbsp;&lt;/STRONG&gt;is not a phrase that would normally be used when considering the map command. As &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/231884"&gt;@PickleRick&lt;/a&gt;&amp;nbsp;indicates, it has to be used carefully.&lt;/P&gt;&lt;P&gt;In your pseudo example it's fine, but with real data remember that each result will initiate a new run of the saved search - if you have lots of results, as this runs collect for EACH and every row, it can place significant additional load on the server - and by default it will only run 10 iterations.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 May 2024 22:54:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687392#M234446</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2024-05-13T22:54:05Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687436#M234454</link>
      <description>&lt;P&gt;On top of that your use might simply be restricted from using such commands. And your dashboards may not run if powered by risky commands.&lt;/P&gt;&lt;P&gt;&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/Security/SPLsafeguards" target="_blank"&gt;https://docs.splunk.com/Documentation/Splunk/latest/Security/SPLsafeguards&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 14 May 2024 08:18:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687436#M234454</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2024-05-14T08:18:09Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687660#M234529</link>
      <description>&lt;P&gt;The lookup reduces the iterations of the map command. In a real world scenario, I have a field called "dept" that lists one of ten departments for each result. The map command only needs to iterate through each one (ten times total), so the output lookup saves off the data, then the stats separates each dept, and the map iterates through.&lt;/P&gt;</description>
      <pubDate>Wed, 15 May 2024 21:31:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/687660#M234529</guid>
      <dc:creator>ejwade</dc:creator>
      <dc:date>2024-05-15T21:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/745166#M241374</link>
      <description>&lt;P&gt;How to understand: "report_to_map_through_indexes",&lt;BR /&gt;I tried to built a macro but got server error&amp;nbsp; or shall it become a custom command? or how tp implement?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2025 09:11:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/745166#M241374</guid>
      <dc:creator>Petermann</dc:creator>
      <dc:date>2025-04-29T09:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/745174#M241378</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/309677"&gt;@Petermann&lt;/a&gt;&amp;nbsp;You have tried to piggy-back your question onto someone else's solved question without a clear indication as to how your question is related. Since this is already marked as solved, it is less likely to receive the attention you might wish. You would be better off starting your own question, clearly stating your usecase, providing sample data (anonymised as minimally as possible, of course), showing what your expected output would be, what you have tried, what errors/messages you are getting, and state why this is not what you want.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2025 12:10:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/745174#M241378</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2025-04-29T12:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: Set the index with a field when using collect command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/745198#M241380</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/225168"&gt;@ITWhisperer&lt;/a&gt;It wasn't obvious at first glance for me either but if you scroll back "report_to_map_through_indexes" was actually a name of a saved search used in the solution.&lt;/P&gt;&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/309677"&gt;@Petermann&lt;/a&gt;As you can see in the docs for the map command, it takes either a literal search as an argument or a name of a saved search. In this case &lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/160850"&gt;@ejwade&lt;/a&gt; used the latter option. The map command references a report_to_map_through_indexes report definition of which is shown below in the original solution.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Apr 2025 18:09:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Set-the-index-with-a-field-when-using-collect-command/m-p/745198#M241380</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2025-04-29T18:09:22Z</dc:date>
    </item>
  </channel>
</rss>

