简体   繁体   中英

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: 

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, eg http://localhost:9200/index/_type/%2FEACVBSDSFASFA, note / -> %2F conversion.

In any case - prefer url-safe IDs, eg:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM