简体   繁体   中英

Elastic search 6.2 with java API, how to have auto generated ID?

I want to insert data with an autogenerated ID. I insert an "user" like this :

java.util.Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put("username", user.username);
        jsonMap.put("password", user.password);
        jsonMap.put("mail", user.mail);
        jsonMap.put("friends", user.friends);
        jsonMap.put("maps", user.maps);
        IndexRequest indexRequest = new IndexRequest("users", "doc",user.username)
                .source(jsonMap)
                .opType(DocWriteRequest.OpType.CREATE);

Here, user.username is my ID. And if I remove this argument, I have an error on execution because I don't have any ID for my user. (I tried that because I read that if I don't put any ID, I should have an auto incremented ID). I didn't find anything on the documentation to help me (or maybe I didn't understood it).

I resolved my problem. I needed to put the opType to "INDEX" instead of "CREATE". Then I can use the id() function

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