0

I have a string query which looks like this

{"query":{"bool":{"should":[{"terms":{"user.id":[#users_to_follow],"minimum_match":1}},{"terms":{"tweets":[#keywords_to_track],"minimum_match":1}}]}},"filter":{"range":{"publishedDate":{"from":#Unix_timestamp}}},"size":#sizelength}"

I am trying to replace certain strings in the query with another string using:

query.replace("#users_to_follow",usersToFollow);
query.replace("#keywords_to_track", keyworsToTrack);
query.replace("#Unix_timestamp","1325930428000" );
query.replace("#sizelength",Integer.toString(SMLApplicationProperties.ES_RESULTSET_SIZE));

However when I run this thing, it does not actually replace the given strings from query.

0

3 Answers 3

5

You need to understand that String class is immutable. Any operation on String returns a new String. You need to assign it back to query variable:

query = query.replace("#users_to_follow",usersToFollow); 
query = query.replace("#keywords_to_track", keyworsToTrack); 
query = query.replace("#Unix_timestamp","1325930428000" ); 
query = query.replace("#sizelength",Integer.toString(SMLApplicationProperties.ES_RESULTSET_SIZE));
Sign up to request clarification or add additional context in comments.

1 Comment

so when i do query=query.replace(); it does not returns the new changed string?
2

You need to get the result of the replace operation :

query = query.replace("#users_to_follow",usersToFollow);

That's because query.replace doesn't change the string you pass, it builds and returns a new one (string in java are immutable).

2 Comments

so i need to assign my query.replace() to a new string?
It IS a new String. You can use the same variable, as in my example. Simply add query = before the query.replace
0

You can optimize (not accepted by everybody) :

String query = query.replace("#users_to_follow",usersToFollow).replace("#keywords_to_track", keyworsToTrack).replace("#Unix_timestamp","1325930428000" ).replace("#sizelength",Integer.toString(SMLApplicationProperties.ES_RESULTSET_SIZE));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.