node.js - elasticsearch 检查索引是否存在在第一次运行时返回 true
问题描述
我有一个docker-compose
正在运行的 2 个容器,每个容器都有自己的服务,node
并且elasticsearch
.
应用程序.js
...
const isElasticReady = await elastic.checkConnection();
if (isElasticReady) {
const elasticIndex = await elastic.esclient.indices.exists({index:elastic.index});
if (!elasticIndex.body) {
await elastic.createIndex(elastic.index);
await elastic.setMapping();
await data.populateDatabase();
}
}
...
每当我运行docker-compose up
时,esclient.indices.exists
总是返回false
,即使索引已经存在。结果,我总是被抛出一个resource_already_exists_exception
.
奇怪的是我正在使用nodemon
开发,每当我在开发中进行更改时esclient.indices.exists
都会返回true
。所以问题只发生在我运行时docker-compose up
。我怀疑某些事情正在异步发生,但我不确定是什么。
*docker-compose.yml -depends_on
已设置。
version: '3.6'
services:
api:
image: nodeservice/node:10.15.3-alpine
container_name: nodeservice
build: .
ports:
- 3000:3000
environment:
- NODE_ENV=local
- ES_HOST=elasticsearch
- NODE_PORT=3000
- ELASTIC_URL=http://elasticsearch:9200
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
command: npm run dev
links:
- elasticsearch
depends_on:
- elasticsearch
networks:
- esnet
elasticsearch:
container_name:my_elasticsearch
image: docker.elastic.co/elasticsearch/elasticsearch:7.0.1
volumes:
- esdata:/usr/share/elasticsearch/data
environment:
- bootstrap.memory_lock=true
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- discovery.type=single-node
logging:
driver: none
ports:
- 9300:9300
- 9200:9200
networks:
- esnet
volumes:
esdata:
networks:
esnet:
有什么提示吗?
解决方案
推荐阅读
- magento - 组产品 - Shopify
- amazon-web-services - 如何在区域中创建 lambda 函数
- python - 将 Flask 的 api 更改为 Fastapi
- unity3d - 放大 Unity 的另一种方法
- list - 如何在列表中打印响应数据?
- python - 如何制作一组从 [0, 0] 到 [6, 6] 且不重复的子列表
- json - 在显示的josn中传递html,无效的控制字符错误未处理的实体
- wordpress - 本地主机上的 Wordpress 多站点与 Xampp
- sharepoint - 验证两个复选框
- python - 如何根据图的模块化改变networkx图中节点的颜色