10
votes

I am playing with spring-boot-sample-data-elastcisearch project. I've changed the pom and added:

SampleElasticsearchApplicationWebXml extends SpringBootServletInitializer 

to run with Tomcat embedded. My application.properties has

spring.data.elasticsearch.http-enabled=true
spring.data.elasticsearch.local=true

I want to be able to connect to localhost:9200 in order to use elasticsearch-head or other JS client. What am I missing? Thanks, Milan

2
You might try reading the documentation for the spring elasticsearch client. It looks like they support various ways of connecting to elasticsearch. I doubt setting elasticsearch properties will cause it to magically do what you want. If you want to start elasticsearch embedded, you need to do that yourself probably. For that, read the elasticsearch documentation. They do indeed support running an embedded client node but I doubt that they would start the http transport for that since it is not needed. You would still need a proper elasticsearch node for it to connect to.Jilles van Gurp

2 Answers

17
votes

According to this ticket, you can now simply add this line to your configuration files:

spring.data.elasticsearch.properties.http.enabled=true
7
votes

you should define this for yourself, because NodeClientFactoryBean has an option for http.enabled but ElasticSearchAutoConfiguration does not (yet) set it.

@Configuration
@EnableConfigurationProperties(ElasticsearchProperties.class)
public class ElasticsearchConfiguration implements DisposableBean {

    private static Log logger = LogFactory.getLog(ElasticsearchConfiguration.class);

    @Autowired
    private ElasticsearchProperties properties;

    private NodeClient client;

    @Bean
    public ElasticsearchTemplate elasticsearchTemplate() {
        return new ElasticsearchTemplate(esClient());
    }

    @Bean
    public Client esClient() {
        try {
            if (logger.isInfoEnabled()) {
                logger.info("Starting Elasticsearch client");
            }
            NodeBuilder nodeBuilder = new NodeBuilder();
            nodeBuilder
                    .clusterName(this.properties.getClusterName())
                    .local(false)
            ;
            nodeBuilder.settings()
                    .put("http.enabled", true)
            ;
            this.client = (NodeClient)nodeBuilder.node().client();
            return this.client;
        }
        catch (Exception ex) {
            throw new IllegalStateException(ex);
        }
    }

    @Override
    public void destroy() throws Exception {
        if (this.client != null) {
            try {
                if (logger.isInfoEnabled()) {
                    logger.info("Closing Elasticsearch client");
                }
                if (this.client != null) {
                    this.client.close();
                }
            }
            catch (final Exception ex) {
                if (logger.isErrorEnabled()) {
                    logger.error("Error closing Elasticsearch client: ", ex);
                }
            }
        }
    }
}