node.js elasticsearch响应空白_source

发布于 2025-02-11 11:14:19 字数 2124 浏览 1 评论 0原文

我已经使用Elasticsearch和node.js创建了一个示例索引,并使用以下代码设置创建了一个样本索引。

const { Client } = require('@elastic/elasticsearch');
const { ELASTIC_SEARCH } = require('../config');

// Elastic Search Cloud Client Setup
const elasticClient = new Client({
    cloud: { id: ELASTIC_SEARCH.CLOUDID },
    auth: {
        apiKey: ELASTIC_SEARCH.API_KEY
    }
});


async function prepareIndex() {
const merchantIndexExists = await elasticClient.indices.exists({ index: 'index2' }); 
        if (merchantIndexExists) return;
        await elasticClient.indices.create({
            index: 'index2',
            body: {
                mappings: {
                    dynamic: 'strict',
                    properties: {
                        company_name: { type: 'text' },
                        company_email: { type: 'keyword' },
                        name: { type: 'text' },
                        price: { type: 'scaled_float', scaling_factor: 10 },
                        created_date: { type: 'date' },
                        is_delete: { type: 'boolean', doc_values: false },
                        merchant: { type: 'keyword', index: 'true' }
                    }
                }
            }
        });
  }

索引创建后,我添加了以下代码的文档:

const { company_name, company_email, price } = req.body;
        const response = await elasticClient.index({
            index: 'index2',
            document: {
                company_email,
                company_name,
                price
            }
        });

现在,当我从Kibana Cloud Console调用搜索API时,它将返回所有归档的确切搜索结果。喜欢

但是当我通过Postman中的代码击中同一搜索查询时,它将返回空白_source。这是Postman响应

const response = await elasticClient.search({
            index: 'index2',
            query: {
                match_all: {}
            }
        });

I have created one sample index using elasticsearch and node.js with below code setup.

const { Client } = require('@elastic/elasticsearch');
const { ELASTIC_SEARCH } = require('../config');

// Elastic Search Cloud Client Setup
const elasticClient = new Client({
    cloud: { id: ELASTIC_SEARCH.CLOUDID },
    auth: {
        apiKey: ELASTIC_SEARCH.API_KEY
    }
});


async function prepareIndex() {
const merchantIndexExists = await elasticClient.indices.exists({ index: 'index2' }); 
        if (merchantIndexExists) return;
        await elasticClient.indices.create({
            index: 'index2',
            body: {
                mappings: {
                    dynamic: 'strict',
                    properties: {
                        company_name: { type: 'text' },
                        company_email: { type: 'keyword' },
                        name: { type: 'text' },
                        price: { type: 'scaled_float', scaling_factor: 10 },
                        created_date: { type: 'date' },
                        is_delete: { type: 'boolean', doc_values: false },
                        merchant: { type: 'keyword', index: 'true' }
                    }
                }
            }
        });
  }

After index creation i have added document with below code:

const { company_name, company_email, price } = req.body;
        const response = await elasticClient.index({
            index: 'index2',
            document: {
                company_email,
                company_name,
                price
            }
        });

Now when I'm calling search API from my kibana cloud console it's returning the exact search results with all the fileds. like
enter image description here

But when I'm hitting same search query via code in postman it's returning blank _source. Here is the search query with postman response

const response = await elasticClient.search({
            index: 'index2',
            query: {
                match_all: {}
            }
        });

enter image description here

Can anyone please help me out of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

当梦初醒 2025-02-18 11:14:19

使用node.js elasticsearch客户端时,您必须将查询包装到身体属性中并将其传递给搜索。

const response = await elasticClient.search({
  index: 'index2',
  body: {
    query: {
      match_all: {}
    }
  }
});

When using the Node.js ElasticSearch client, you have to wrap the query into a body property and pass it to the search.

const response = await elasticClient.search({
  index: 'index2',
  body: {
    query: {
      match_all: {}
    }
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文