For example, if the url is "https://pixabay.com/images/search/planet/", what I need is the domain "pixabay.com".
We can try scripted filed to solve this problem with the help of this documentation.
Attention: Never forget using def
or int
or String
to define a new variable.
Firstly, we use the method indexOf('/')
to prase out "pixabay.com/images/search/planet/". This method would return the position of the first '/' of each string, which is 6 in this url.
After getting the position of the first '/', we need the substring of this url, starting from 8th of the string 'p' to the end. So we use path.substring(fSlashIndex+2)
.
Secondly, to parse out substring "pixabay.com" before the first '/' of "pixabay.com/images/search/planet/", we reuse the method indexOf('/')
. But what we need to use path.substring(0,lSlashIndex)
. The starting position is 0 and the end position is before "lSlashIndex".
def path = doc['url.keyword'].value; if (path != null) { int fSlashIndex = path.indexOf('/'); if (fSlashIndex > 0) { def path_new = path.substring(fSlashIndex+2); if (path_new != null) { int lSlashIndex = path_new.indexOf('/'); if (lSlashIndex > 0) { return path_new.substring(0,lSlashIndex); } else { return path_new; } } } } return "";