1

有了ElasticSearchTemplate我可以很容易地从一个简单的实体类中创建索引。假设我想保存 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;
}

它足以使:

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

这可以通过纯 ES Java API 实现,而无需spring-data-elasticsearch对字符串(JSON)进行 and 操作吗?

4

1 回答 1

1

实际上Java API需要JSON但是有ES助手所以代码可以如下:

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();
于 2016-08-27T10:33:29.550 回答