Dashboards & Visualizations

Dynamically creating SearchManager

dreadangel
Path Finder

Using the gorgeous answer from this discussion as a basis, I've started to develop my solution, but due to lack of documentation I'm struggling on how to map field search of a SearchManager instance, created in JavaScript, to a token :

Setting the token somewhere in code:

  function setQueryStringInToken(){
         var queryValue = {creating the querystring} ; 
         defaultTokenModel.set("searchQueryString_tk", queryValue);
         submittedTokens.set(defaultTokenModel.toJSON());
}

now need to inject the token to SearchManager instance:

   new SearchManager({
        id: "example-search",
        cache: false,
        search: [here I need to inject data from searchQueryString_tk]
 });

Is possible to inject searchQueryString_tk to SearchManager's search field? How to achieve that?

0 Karma
1 Solution

harshpatel
Contributor

Hi @dreadangel,

You can place tokens in the search string and have tokens: true option passed in the search manager which will replace token with its value in your search:

new SearchManager({
         id: "example-search",
         cache: false,
         search: "$searchQueryString_tk$"
  }, {tokens: true});

View solution in original post

harshpatel
Contributor

Hi @dreadangel,

You can place tokens in the search string and have tokens: true option passed in the search manager which will replace token with its value in your search:

new SearchManager({
         id: "example-search",
         cache: false,
         search: "$searchQueryString_tk$"
  }, {tokens: true});

dreadangel
Path Finder

Thank you. One more ahead - if the search string I'm injecting in searchQueryString_tk token contains tokens - ex. index=main user=$userName$ - how to achieve that **userName token value to be injected in that expression? Or I need to extract the token value and to build the search string from the scratch?

0 Karma

harshpatel
Contributor

I believe if you write it as follows it will work.. please try this and give feedback:

new SearchManager({
          id: "example-search",
          cache: false,
          search: defaultTokenModel.get("searchQueryString_tk")
   }, {tokens: true});

This will allow the search to have your final string with tokens which will be replaced later because of token: true.

Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...