nodejs elasticsearch查询默认行为

发布于 2025-02-10 19:44:32 字数 1141 浏览 0 评论 0原文

每天,我将数据(Time_Series)推向Elasticsearch。我创建了一个索引模式,并且我的索引具有名称:myIndex_ *,今天 *的日期(已设置了索引模式)。因此,一周后,我有:myIndex_2022-06-20myIndex_2022-06-21 ... myIndex_2022-06-27

假设我的指数是索引产品的价格。因此,在每个myIndex _*中,我得到了:

    myindex_2022-06-26 is including many products prices like this:
{
    "reference_code": "123456789",
    "price": 10.00
},
...
myindex_2022-06-27:
{
    "reference_code": "123456789",
    "price": 12.00
},

我正在使用此查询来获取参考代码和相应的价格。而且效果很好。

const data = await elasticClient.search({
                    index: myindex_2022-06-27,
                    body: {
                        query: {
                            match: {
                               "reference_code": "123456789"
                                }
                        }
                    }
                });

但是,我想查询一个查询,如果在2022-06-27的索引中,没有数据,则在上一个索引2022-06-26中检查,等等(直到EG 10X) 。

不确定,但是当我用myIndex_*替换MyIndex_2022-06-27时,似乎正在这样做(不确定这是默认行为)。

问题是,当我使用这种方式时,我从其他索引中获得了价格,但似乎使用了最古老的索引。我想取得最新的方式,从而相反。

我应该如何继续?

On a daily basis, I'm pushing data (time_series) to Elasticsearch. I created an index pattern, and my index have the name: myindex_* , where * is today date (an index pattern has been setup). Thus after a week, I have: myindex_2022-06-20, myindex_2022-06-21... myindex_2022-06-27.

Let's assume my index is indexing products' prices. Thus inside each myindex_*, I have got:

    myindex_2022-06-26 is including many products prices like this:
{
    "reference_code": "123456789",
    "price": 10.00
},
...
myindex_2022-06-27:
{
    "reference_code": "123456789",
    "price": 12.00
},

I'm using this query to get the reference code and the corresponding prices. And it works great.

const data = await elasticClient.search({
                    index: myindex_2022-06-27,
                    body: {
                        query: {
                            match: {
                               "reference_code": "123456789"
                                }
                        }
                    }
                });

But, I would like to have a query that if in the index of the date 2022-06-27, there is no data, then it checks, in the previous index 2022-06-26, and so on (until e.g. 10x).

Not sure, but it seems it's doing this when I replace myindex_2022-06-27 by myindex_* (not sure it's the default behaviour).

The issue is that when I'm using this way, I got prices from other index but it seems to use the oldest one. I would like to get the newest one instead, thus the opposite way.

How should I proceed?

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

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

发布评论

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

评论(1

似梦非梦 2025-02-17 19:44:33

如果您使用索引通配符查询,则应返回文档列表,其中每个文档将以_INDEX_id的方式包含一些元字段。

您可以按_index进行排序,以使弹性搜索返回列表中[0]位置的最新文档。

const data = await elasticClient.search({
  index: myindex_2022-*,
  body: {
      query: {
          match: {
            "reference_code": "123456789"
              }
      }
      sort : { "_index" : "desc" },
  }
});

If you query with index wildcard, it should return a list of documents, where every document will include some meta fields as _index and _id.

You can sort by _index, to make elastic search return the latest document at position [0] in your list.

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