首页 > 解决方案 > 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:

有什么提示吗?

标签: node.jsdockerelasticsearch

解决方案


推荐阅读