package com.tashow.cloud.es.service;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.IndexResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ElasticsearchService {

    private final ElasticsearchClient client;

    public ElasticsearchService(ElasticsearchClient client) {
        this.client = client;
    }

    /**
     * 向 Elasticsearch 索引中插入数据
     *
     * @param indexName 索引名称
     * @param id        文档 ID
     * @param jsonData  JSON 格式的文档数据
     * @return 插入结果
     * @throws IOException 如果发生 I/O 错误
     */
    public IndexResponse insertDocument(String indexName, String id, String jsonData) throws IOException {
        return client.index(i -> i
                .index(indexName)
                .id(id)
                .document(jsonData)
        );
    }
}
