1

With ElasticSearchTemplate I can easily create index out of a simple entity class. Say I want to save the Book.java:

@Document(indexName = "bookshop", type = "book", shards = 2, replicas = 2, refreshInterval = "-1")
public class Book {

    @Id
    private String id;
    @Field(type = FieldType.String, store = true)
    private String title;
}

Its enough just to make:

    elasticsearchTemplate.createIndex(Book.class);
    elasticsearchTemplate.putMapping(Book.class);
    elasticsearchTemplate.refresh(Book.class);

Can this be achieved with pure ES Java API without the spring-data-elasticsearch and operations on Strings(JSON)?

1 Answer 1

1

Actually Java API requires the JSON however there are ES helpers so the code can be as follows:

CreateIndexResponse createIndexRequestBuilder = client().admin().indices()
                .prepareCreate(INDEX_NAME)
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                        .field("title", "My Title 1")
                        .endObject()
                )
                .setSettings(
                        Settings.settingsBuilder()
                                .put("index.number_of_shards", 2)
                                .put("index.number_of_replicas", 2)
                )
                .execute()
                .actionGet();
Sign up to request clarification or add additional context in comments.

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.