Splunk Search

How to call a function every time I run a search?

bkeif
Path Finder

I have written a small app on the django framework. One of its dashboards has a search bar that should accept either all numbers or a mix of numbers and letters. Based on the user's input (which is the "searchquery" token) it should then assign the "search_by" token to its appropriate value to be used by a search manager.

Here is the JS logic I am using.

    var test = function() {
             var userInput = tokens.get("searchquery");
            bool = isNaN(userInput)
            console.log(userInput + ' is not a number:' + bool);
            if (bool) {
                    tokens.set('search_by', 'email');
            }
            else {
                    tokens.set('search_by', 'id');
            }
    };

I can then call this function when the search bar is clicked but this is not ideal as the user can just press enter when in the search bar instead of clicking the search icon. This will run the search but return no results because the 'search_by' token has not been correctly set.

How can i call this function anytime I run or update a search?

Thanks!

Also:
Any information/links on the Splunk Web Framework's JavaScript would be appreciated. I have of course read the available docs and done some light goggling. There must be some listener already present to update panels when the search is run, etc. If I could just attach my function to this lister that would be great.

0 Karma
1 Solution

lquinn
Contributor

You can add a listener to the search like this:

var user_search = splunkjs.mvc.Components.getInstance("user_search");
user_search.on("search:done", function(){
    test_function();
});

You can also do this when a search is started, cancelled, when there are results etc. Look at the events section on this page for other methods.
http://docs.splunk.com/Documentation/WebFramework

View solution in original post

lquinn
Contributor

You can add a listener to the search like this:

var user_search = splunkjs.mvc.Components.getInstance("user_search");
user_search.on("search:done", function(){
    test_function();
});

You can also do this when a search is started, cancelled, when there are results etc. Look at the events section on this page for other methods.
http://docs.splunk.com/Documentation/WebFramework

bkeif
Path Finder

Thank you for taking the time to answer this rather old question, hopefully it will help others in the community.

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

.conf25 Global Broadcast: Don’t Miss a Moment

Hello Splunkers, .conf25 is only a click away.  Not able to make it to .conf25 in person? No worries, you can ...

Observe and Secure All Apps with Splunk

 Join Us for Our Next Tech Talk: Observe and Secure All Apps with SplunkAs organizations continue to innovate ...

What's New in Splunk Observability - August 2025

What's New We are excited to announce the latest enhancements to Splunk Observability Cloud as well as what is ...