如果不可用(如果数据库为空),如何在elasticsearch中的节点中自动创建索引

时间:2018-04-17 06:40:28

标签: elasticsearch spring-data-elasticsearch

以下是我现在正在做的示例代码:

SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(query)
                .withIndices(organization+"-"+index_name)                   .addAggregation(AggregationBuilders.terms("Dept").field(QueryConstants.INTERNAL_DEPT).size(size)
                        .showTermDocCountError(true))
                .build();
        return elasticsearchTemplate.query(searchQuery, new ResultsExtractor<JsonNode>() {

            @Override
            public JsonNode extract(SearchResponse response) {
                Terms t = response.getAggregations().get("Dept");
                ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
                ObjectNode objectNode = null;
                for (Terms.Bucket b : t.getBuckets()) {
                    objectNode = JsonNodeFactory.instance.objectNode();
                    objectNode.put("department", b.getKey());
                    objectNode.put("hits", b.getDocCount());
                    arrayNode.add(objectNode);
                }
                return arrayNode;
            }
        })

问题是当我运行这个时,我没有数据时会出现异常

org.elasticsearch.client.transport.NoNodeAvailableException: None of the configured nodes are available: []

修改

这就是我设置弹性客户端的方法:

@Bean
public Client client() {
    Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", esClusterName).build();
    Client client = new TransportClient(settings)
            .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
    return client;
}

我想问一下,有没有办法在运行时创建缺失的索引?

0 个答案:

没有答案