在Spring Boot环境下,可以使用Elasticsearch的Java高级客户端(High Level REST Client)来创建两个索引,分别为门店索引和商品索引,并使用Parent-Child关系来关联它们。

首先,需要在pom.xml文件中添加以下依赖:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-high-level-client</artifactId>
    <version>7.10.2</version>
</dependency>

接下来,可以创建一个Elasticsearch的配置类,用于初始化高级客户端和创建索引:

import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class ElasticsearchConfig {

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Bean
    public RestHighLevelClient restHighLevelClient() {
        return new RestHighLevelClient(
                RestClient.builder(new HttpHost(host, port, "http")));
    }

    @Bean
    public void createIndices() throws IOException {
        RestHighLevelClient client = restHighLevelClient();

        // 创建门店索引
        CreateIndexRequest storeIndexRequest = new CreateIndexRequest("store_index");
        storeIndexRequest.settings(Settings.builder()
                .put("index.number_of_shards", 1)
                .put("index.number_of_replicas", 1));
        storeIndexRequest.mapping("_doc", "{\n" +
                "  \"properties\": {\n" +
                "    \"merchantId\": {\n" +
                "      \"type\": \"keyword\"\n" +
                "    }\n" +
                "  }\n" +
                "}", XContentType.JSON);
        CreateIndexResponse storeIndexResponse = client.indices().create(storeIndexRequest, RequestOptions.DEFAULT);

        // 创建商品索引
        CreateIndexRequest productIndexRequest = new CreateIndexRequest("product_index");
        productIndexRequest.settings(Settings.builder()
                .put("index.number_of_shards", 1)
                .put("index.number_of_replicas", 1));
        productIndexRequest.mapping("_doc", "{\n" +
                "  \"_routing\": {\n" +
                "    \"required\": true\n" +
                "  },\n" +
                "  \"_parent\": {\n" +
                "    \"type\": \"store\"\n" +
                "  },\n" +
                "  \"properties\": {\n" +
                "    \"merchantId\": {\n" +
                "      \"type\": \"keyword\"\n" +
                "    }\n" +
                "  }\n" +
                "}", XContentType.JSON);
        CreateIndexResponse productIndexResponse = client.indices().create(productIndexRequest, RequestOptions.DEFAULT);

        client.close();
    }

}

在上述配置类中,通过@Value注解将Elasticsearch的主机和端口号配置为application.properties文件中的属性。

restHighLevelClient()方法用于创建一个RestHighLevelClient实例。

createIndices()方法用于创建门店索引和商品索引。在创建商品索引时,通过_routing_parent字段来指定父子关系,其中父索引为store类型。

最后,可以在Spring Boot的启动类或其他需要使用索引的地方调用createIndices()方法来创建索引。

注意:在实际使用中,需要根据实际需求调整索引的设置和映射,例如字段类型、分片数、副本数等

在springboot环境下 es创建两个索引分别为门店索引和商品索引它们之间merchantId关联使用Parent-Child关系:将门店索引作为父索引商品索引作为子索引

原文地址: https://www.cveoy.top/t/topic/h33S 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录