简体   繁体   中英

Creating type inside index in elastic search through rest high level client

I am using rest high level client elastic search in my JAVA application. Document can be found here . In my application at startup I am deleting index named "posts" where Elasticsearch datas are stored and creating again Index "posts" following this link

CreateIndexRequest request = new CreateIndexRequest("posts");

But, Inside index I need to create one type named "doc". Which is not mentioned in the website. Temporary fix is when I am posting some data following this link it is creating type

Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "kimchy");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("posts", "doc", "1")
    .source(jsonMap); 

But, in this process when I am posting only then I can able to create type "doc". If I am not posting and trying to hit controller which calls data frmo index "posts" and type "doc". It gives error as "doc" type is not there.

Anyone hava any idea how to create type using rest high level client ES in java

By type you mean document type?

What about the second section Index Mappings in the link you provided? Does this not work for you?

我需要将 type 设置为“_doc”以使其与 ES7.6 一起使用

If you know how to insert documents through API then this way will much more easy for you to do anything similar API (DELETE,POST,PUT...) First, you will need RestHighLevelClient and all you have to do

String index = "/indexName/_doc"; <- Your path or type here
Request request = new Request("POST", index); <- Your method
request.setJsonEntity(
      "{ \"message\": \" example add insert\" }" <- Your request body
);

client.getLowLevelClient().performRequest(request); 

This will execute like how API does.

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