我可以为ElasticSearch指定URI中的数据库吗?

e0bqpujr  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(2)|浏览(119)

这样行吗?
.环境:
ELASTIC_SEARCH_URI=http://localhost:9200/
文档只是说使用ELASTIC_SEARCH_URI=http://localhost:9200/,但我有两个不同的应用程序在同一个专用服务器上使用ElasticSearch。我想为每个应用程序指定一个唯一的数据库。

const es = new ElasticsearchClient({
  node: "http://localhost:9200",
  /*
    auth: {
        username: 'elastic',
        password: 'password'
    }
    */
});
zynd9foi

zynd9foi1#

如果你想要Elasticsearch的多个示例,你可以创建2个不同的容器,例如一个将监听:9200,另一个监听:9201
如果你所指的specifying database是不同的索引,那么你可以根据自己的需要封装必要的函数。

// search func in app 1
const esSearch = (body) => {
  const response = await es.search({
    target: "my-app1",
    body,
  });
}

// search func in app 2
const esSearch = (body) => {
  const response = await es.search({
    target: "my-app2",
    body,
  });
}

因为Elasticsearch本身是一个数据库,所以没有指定数据库。您可以为不同的数据创建不同的索引。

gev0vcfq

gev0vcfq2#

您可以为两个不同的应用程序使用以下代码:

应用程序1

假设APP1的数据源名称为example-dev

const es = new ElasticsearchClient({
  node: "http://localhost:9200",
  /*
    auth: {
        username: 'elastic',
        password: 'password'
    }
    */
});

const result = await es.search({
    index: 'example-dev',
    query: {
      match: {
        quote: 'winter'
      }
    }
  })

应用程序2

假设APP2的数据源名称为example-dev-app2

const es = new ElasticsearchClient({
  node: "http://localhost:9200",
  /*
    auth: {
        username: 'elastic',
        password: 'password'
    }
    */
});

const result = await es.search({
    index: 'example-dev-app2',
    query: {
      match: {
        quote: 'winter'
      }
    }
  })

相关问题