2

I try to modify existing query string to filter specified condition: now query string is below:

String bodycontent="{"
                +"  \"_source\": [\"@timestamp\",\"eqpid\",\"lotid\",\"stageid\",\"srvmethod\",\"parm_1\",\"apcack\",\"description\",\"host\",\"path\",\"action\"],"
                +"  \"query\": {"
                +"    \"query_string\" : {"
                +"        \"query\":\" lotid:"+q_lotid+" AND "+host_type+"\"}"
                +"  },"
                +"  \"sort\" : [{\"@timestamp\" : { \"order\" : \"desc\" }}]"
                +"}";

I want to change the query condition below:

query :
type:ams_log AND (alm_source:K*) AND (alm_id:TCS00004 OR alm_id:TCS00005 OR alm_id:TCS00007 OR alm_id:TCS00008 OR alm_id:TCS00009 OR alm_id:TCS00010 OR alm_id:TCS00011 OR alm_id:TCS00012 OR alm_id:TCS00013 OR alm_id:TCS00020 OR alm_id:TCS00024 OR alm_id:TCS00032)

but I am confused how to modify the query string with slash \ and Double quotes ". I can't find the rule, thanks!

Howard
  • 143
  • 1
  • 1
  • 12

1 Answers1

2
  • Slash - \\
  • Quote - \"

System.out.println("\\ \"hello\"");

Output:

\ "hello"


Regarding your query, seems like both key and value must be in between quotes "key":"value"

String lotId="2020_A_88";
String type="apples";
String queryPart = "\"lotId\":\""+lotId+"\" AND \"type\":\""+type+"\"";
System.out.println(queryPart);    

"lotId":"2020_A_88" AND "type":"apples"


String example = String.format("Slash-> %s , Quotes-> %s", "\\" ,"\"name\""); 
System.out.println(example);

Slash-> \ , Quotes-> "name"


String s = "\\";
String q = "\"name\"";
System.out.println("Slash-> "+s+" , Quotes-> "+q);

Slash-> \ , Quotes-> "name"

aran
  • 10,978
  • 5
  • 39
  • 69
  • +" \"query\":\" lotid:"+q_lotid+" AND "+host_type+"\"}" could you explain this line? thanks! – Howard Dec 28 '20 at 12:16
  • It is incorrect, as includes a last quote in the host_type but not at the start. so the output would be something like: `"query":" lotid:12 AND name"}` – aran Dec 28 '20 at 12:18
  • `+ "\"query\":\" lotid:"+q_lotid+" AND \""+host_type+"\" \"}"` --> output --> `"query":" lotid:12 AND "name" "}` – aran Dec 28 '20 at 12:25
  • note that you may need the last quote after "name" – aran Dec 28 '20 at 12:25
  • it seems not correct, I try to change this line as you provide to JAVA file, and that's wrong. – Howard Dec 28 '20 at 12:29
  • 1
    I forgot to include the first \" from query, updated the comment. Try `+ "\"query\":\" lotid:"+q_lotid+" AND \""+host_type+"\" \"}"` -- anyway, why is the last } needed? – aran Dec 28 '20 at 12:29
  • Also, does the query need to be in between quotes? I don't get what's your desired output, could you tell? – aran Dec 28 '20 at 12:33
  • I need to use restful api to query a remote database(in JAVA), so I need to process the query string with \ and " – Howard Dec 28 '20 at 12:36
  • it need } because it mapping the previous { – Howard Dec 28 '20 at 12:38
  • Could you modify my query string which I inquire >< I am very appreciated thanks. Because I have no idea how to modify. – Howard Dec 28 '20 at 12:41
  • updated the answer, but note that some databases may require single quotes when working with string literals, so I'll add that as well – aran Dec 28 '20 at 12:43
  • I mean this query string-> query: type:ams_log AND (alm_source:K*) AND (alm_id:TCS00004 OR alm_id:TCS00005 OR alm_id:TCS00007 OR alm_id:TCS00008 OR alm_id:TCS00009 OR alm_id:TCS00010 OR alm_id:TCS00011 OR alm_id:TCS00012 OR alm_id:TCS00013 OR alm_id:TCS00020 OR alm_id:TCS00024 OR alm_id:TCS00032) – Howard Dec 28 '20 at 12:44
  • yes, but I want to change the original query string to the upper query string. – Howard Dec 28 '20 at 12:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/226514/discussion-between-aran-and-howard). – aran Dec 28 '20 at 12:49