# Here is one solution of how to parse domain name from url

**URL:** <https://discuss.elastic.co/t/here-is-one-solution-of-how-to-parse-domain-name-from-url/189271>\
**Category:** Kibana\
**Created:** [July 8, 2019, 1:04am UTC](https://discuss.elastic.co/t/here-is-one-solution-of-how-to-parse-domain-name-from-url/189271 "2019-07-08T01:04:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Havana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/havana/32/47091_2.png) [@Havana](https://discuss.elastic.co/u/Havana)\
**Post date:** [July 8, 2019, 1:04am UTC](https://discuss.elastic.co/t/here-is-one-solution-of-how-to-parse-domain-name-from-url/189271/1 "2019-07-08T01:04:15Z")

</div>

For example, if the url is "[https://pixabay.com/images/search/planet/](https://pixabay.com/images/search/planet/)", what I need is the domain "[pixabay.com](http://pixabay.com)".  
We can try scripted filed to solve this problem with the help of this [documentation](https://www.elastic.co/blog/using-painless-kibana-scripted-fields).

**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/](http://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](http://pixabay.com)" before the first '/' of "[pixabay.com/images/search/planet/](http://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 "";`

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [July 8, 2019, 1:05am UTC](https://discuss.elastic.co/t/here-is-one-solution-of-how-to-parse-domain-name-from-url/189271/2 "2019-07-08T01:05:37Z")

</div>

Thanks for sharing this!

Scripted fields can be expensive if you have a lot of them, we would always recommend extracting this during the ingestion process.

---

<div class="post-metadata">

**Author:** ![Havana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/havana/32/47091_2.png) [@Havana](https://discuss.elastic.co/u/Havana)\
**Post date:** [July 8, 2019, 1:45am UTC](https://discuss.elastic.co/t/here-is-one-solution-of-how-to-parse-domain-name-from-url/189271/3 "2019-07-08T01:45:20Z")

</div>

Yeah, as I have to wait for a "long" time to see the result of each field.

---

<div class="post-metadata">

**Author:** ![Havana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/havana/32/47091_2.png) [@Havana](https://discuss.elastic.co/u/Havana)\
**Post date:** [July 8, 2019, 2:41am UTC](https://discuss.elastic.co/t/here-is-one-solution-of-how-to-parse-domain-name-from-url/189271/4 "2019-07-08T02:41:34Z")

</div>

Here is another simpler script with the help of this [doc](https://www.geeksforgeeks.org/java-string-indexof/) on how to use indexOf() method.  
`def path = doc['url.keyword'].value; if (path != null) { int n = path.indexOf('/'); int m = path.indexOf('/', n+2); if (m > 0) { return path.substring(n+2,m); } } return "";`

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 5, 2019, 2:41am UTC](https://discuss.elastic.co/t/here-is-one-solution-of-how-to-parse-domain-name-from-url/189271/5 "2019-08-05T02:41:43Z")

</div>

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