elasticsearch配置注入索引

微妙的人生發表於2020-11-20
@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(indexName = "#{appConfig.trial}", type = "trialData")
public class EsTrialData {

    @Id
    String id;

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    Date updateTime;
}


@Data
@Configuration
public class AppConfig {
    @Value("${mongo.address}")
    private String mongoDbAddress;
    @Value("${mongo.database}")
    private String mongoDbDatabase;
    @Value("${es.address}")
    private String esAddress;
    @Value("${es.port}")
    private Integer esPort;
    @Value("${entity.es.doctor}")
    private String doctor;
    @Value("${entity.es.hospital}")
    private String hospital;
    @Value("${entity.es.site}")
    private String site;
    @Value("${entity.es.trial}")
    private String trial;


    public @Bean
    MongoClient mongoClient() {
        return new MongoClient(mongoDbAddress);
    }

    public @Bean
    MongoTemplate mongoTemplate() {
        return new MongoTemplate(mongoClient(), mongoDbDatabase);
    }


    @SneakyThrows
    @Bean
    Client client() {
        Settings settings = Settings.builder()
                .put("cluster.name", "elasticsearch")
                .build();
        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(new TransportAddress(InetAddress.getByName(esAddress)
                , esPort));
        return client;
    }
}

相關文章