# Cannot escape special characters in query using Java API

**URL:** <https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301>\
**Category:** Elasticsearch\
**Created:** [July 10, 2015, 9:08am UTC](https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301 "2015-07-10T09:08:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaKro](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@MaKro](https://discuss.elastic.co/u/MaKro)\
**Post date:** [July 10, 2015, 9:08am UTC](https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301/1 "2015-07-10T09:08:47Z")

</div>

Hello.

I have a field which contains forward slashes (it's a usual path). I'm trying to execute this Query:

```
QueryBuildres.termQuery("pageId", QueryParser.escape("/my/field/val"))

```

and I cannot get any results. When I'm looking for 'val' only, then I get the proper results. Any ideas why is that happening? Of course without escaping it also doesn't return the results.

QueryParser.escape parses string properly( it contains "/" for every slash), but when request goes to elasticsearch it's double escaped ("\/)  
Here is the log:

```
[2015-07-10 01:53:00,063][WARN][index.search.slowlog.query] [Aaa AA] [index_name][4] took[420.8micros], took_millis[0], types[page], stats[], search_type[QUERY_THEN_FETCH], total_shards[5], source[{"query":{"term":{"pageId":"\\/path\\/and\\/testestest"}}}], extra_source[],

```

Also I have noticed that It works when I'm using querystring:

```
QueryBuilders.queryString("pageId:" + QueryParser.escape("/my/field/val"))

```

, but I wouldn't like to use it like that and type everything by hand.

I also have checked if it works from the console (using cURL):

```
curl -XGET 'http://localhost:9200/index/_search?q=pageId:\-path\/test'
```

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [July 10, 2015, 9:35am UTC](https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301/2 "2015-07-10T09:35:09Z")

</div>

What is the mapping definition and the analyzer for the field `pageId`?

When sending `termQuery`, the field is queried without analyzing. No escape needed. This is for exact match.

`queryString` is Lucene-based and needs escaping. It analyzes by best effort.

---

<div class="post-metadata">

**Author:** ![MaKro](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@MaKro](https://discuss.elastic.co/u/MaKro)\
**Post date:** [July 10, 2015, 10:27am UTC](https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301/3 "2015-07-10T10:27:04Z")

</div>

Hi jprant, thank you for answer.

I didn't added any mapping definition and analyzer for the field pageId.  
It's default one.  
So if it's not needed then I still get zero results.

Do I have to add a mapping and analuzer always? I assume I should do that when I have created index, I haven't added it separately,  
Index was created when i was indexing content.

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [July 10, 2015, 10:44am UTC](https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301/4 "2015-07-10T10:44:58Z")

</div>

You are indexing paths, like `a/b/c`. For this, you need to set up an analyzer. Default one can not understand paths.

---

<div class="post-metadata">

**Author:** ![MaKro](https://avatars.discourse-cdn.com/v4/letter/m/bbe5ce/32.png) [@MaKro](https://discuss.elastic.co/u/MaKro)\
**Post date:** [July 10, 2015, 11:09am UTC](https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301/5 "2015-07-10T11:09:06Z")

</div>

Solution for the issue:

1. Add mapping to index for pageId. Setting "index": "not\_analyzed" in pageId mapping is enough.
2. Use filters to query pageId.

---

<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:** [July 6, 2017, 12:01am UTC](https://discuss.elastic.co/t/cannot-escape-special-characters-in-query-using-java-api/25301/6 "2017-07-06T00:01:59Z")

</div>


