Search code examples
javaelasticsearchaws-elasticsearchresthighlevelclient

Java RestHighLevelClient - Elastic Search - How to add special character '/' in id field value


Getting exception while inserting field value with '/' in elastic search

Code:

String id = "/EACVBSDSFASFA";

IndexRequest indexRequest = new IndexRequest(INDEX, TYPE, id).source(objectsMap);
                    IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);

Exception:

Path part [/EACVBSDSFASFA] couldn't be encoded: java.lang.IllegalArgumentException
java.lang.IllegalArgumentException: Path part [/EACVBSDSFASFA] couldn't be encoded
at org.elasticsearch.client.RequestConverters$EndpointBuilder.encodePart(RequestConverters.java:1142)
at org.elasticsearch.client.RequestConverters$EndpointBuilder.addPathPart(RequestConverters.java:1104)
at org.elasticsearch.client.RequestConverters.endpoint(RequestConverters.java:704)
at org.elasticsearch.client.RequestConverters.index(RequestConverters.java:309)
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1761)
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1735)
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1697)
at org.elasticsearch.client.RestHighLevelClient.index(RestHighLevelClient.java:929)
at 
.....
.....
Caused by: java.net.URISyntaxException: Illegal character in port number at index 30: 

Solution

  • The issue is not because of the field, but because you're using / as part of the id.

    I'd guess this is a restriction to prevent resources with where IDs would have to be encoded, e.g. http://localhost:9200/index/_type/%2FEACVBSDSFASFA, note / -> %2F conversion.

    In any case - prefer url-safe IDs, e.g: