1

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

1
  • Similary if I don't do CreateIndex also it will create when I post data. Commented Feb 13, 2019 at 12:40

3 Answers 3

1

By type you mean document type?

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

Sign up to request clarification or add additional context in comments.

6 Comments

You are right this will create type "tweet" inside index "twitter", but here you need to map, but In my case I am not mapping. I want to store any type of Document
For now, I will try to keep second field in method blank like request.mapping("tweet", "", XContentType.JSON); Will see weather it will create and it will allow me to store anything
I think it is possible, as when creating index, type in elastic search or in kibana dev console We can give PUT index/doc like that, and rest client also parsing like that most probably
> I want to store any type of Document If you want to store different kinds of documents it's better to store them in different indices. In fact, types are deprecated and will be removed in a future version of Elasticsearch.
Got it. I will make different indices. But, how people were doing like in my use case. How they were doing. Any Idea? Just curious
|
1

I needed to set type to "_doc" to make it working with ES7.6

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.