Painless: match a string


I am new to painless and wanted to know what is the proper way to match a string.

For my specific example I want to perform two different manipulations to two keywords and then combine them at the end. The first manipulation involves taking only the string before the '/', which I have done. The second manipulation involves finding a particular word in a long string. For example, finding the word "cat" or "dog" in the phrase doc['sentence.keyword'] = "I_am_a_(dog or cat)_pet". If the phrase found is cat then add that to the first manipulation so that the final result becomes: "This_is_a_cat", if the phrase found is dog then it becomes "This_is_a_dog". There are no other combinations than cat or dog.

Looking at the Painless scripting blog:

I see that there are two examples, one which matches a string using regex and another which matches a string using .matcher.

For my problem, what is the best way to match the keyword, either cat or dog, and then add that to the first manipulation. Please include example code.

Thank you in advance,

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.