0

I'm using a query as shown below, however no matter what I try, I'm unable to insert a string variable into the query string (string variable is 'searchString'). It just won't compile.

I've tried various suggestions for inserting string variables but nothing works for me.

    var searchResponse = await _elasticLowLevelClient.SearchAsync<StringResponse>("webapp-razor-*", @"
    {
         ""from"": 0,
         ""size"": 10,
         ""query"": {
             ""match"": {
                 ""_metadata.log_event_type"": {
                     ""query"": """ + searchString + """
                     }
             }
          }
    }"); 

enter image description here

The above method in the Elastic docs:

enter image description here

Vy Do
  • 46,709
  • 59
  • 215
  • 313
OJB1
  • 2,245
  • 5
  • 31
  • 63

1 Answers1

0

Well what a marathon to find the answer to this.

What we're dealing with here is knowing how to insert a variable into a string literal, which is in essence what the query body is. The following SO post helped me solve this here: Adding string to verbatim string literal

From the method I showed in my original question, I re-wrote the query body into a single line:

var vertabimString = @"{ ""from"": 0, ""size"": 10, ""query"": { ""match"": { ""_metadata.log_event_type"": { ""query"": """ + searchString + @"""}}}}";
var searchResponse = await _elasticLowLevelClient.SearchAsync<StringResponse>("webapp-razor-*", vertabimString);

I'm now able to run the query and get the required results back. So the issue I had wasn't something related to using the Elastic Low Level Client, but more as a consequence in trying to use the low level client with the flexibility to build up my own queries using my string variables in the mix.

Hopefully this saves someone else ripping their hair out as I have none left!

OJB1
  • 2,245
  • 5
  • 31
  • 63