【问题标题】:Elasticsearch max document count for each index每个索引的 Elasticsearch 最大文档数
【发布时间】:2019-10-27 17:17:58
【问题描述】:

我有两个独立的索引 A 和 B,别名为 X。两个索引具有相同的文档结构。 当我在别名 X 上使用 size = 20 进行搜索时,我想为索引 B 设置最大文档大小 5。搜索结果应包含索引 B 中的最多 5 个文档。如果索引 B 中没有文档,则搜索结果应包含 20 个文档来自索引 A。

是否有任何解决方案来设置每个索引的最大文档数,以便使用别名跨多个索引进行搜索?

【问题讨论】:

    标签: elasticsearch elasticsearch-indices


    【解决方案1】:

    您可以使用_msearch API 来实现。

    下面是一个示例查询,说明如何应用它:

    POST myalias/_msearch
    {"index" : "myindex_news_sports"}
    {"query" : {"match_all" : {}}, "from" : 0, "size" : 1}
    {"index" : "myindex_news_economics"}
    {"query" : {"match_all" : {}}, "from" : 0, "size" : 1}
    

    基本上,_msearch 允许您使用相同的 API 搜索多个请求。有两个不同的查询,您可以在其中为两个不同的索引指定size

    请注意,结果将出现在单独的 JSON 组中(两个结果,每个查询一个)。

    示例响应:

    {
      "took" : 4,
      "responses" : [
        {                                  <---- Response for Index 1
          "took" : 4,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 2,
              "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
              {
                "_index" : "myindex_news_sports",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 1.0,
                "_source" : {
                  "fooNested" : {
                    "sigma" : 9,
                    "theta" : 1
                  }
                }
              },
              {
                "_index" : "myindex_news_sports",
                "_type" : "_doc",
                "_id" : "4",
                "_score" : 1.0,
                "_source" : {
                  "fooNested" : {
                    "sigma" : 9,
                    "theta" : 1
                  }
                }
              }
            ]
          },
          "status" : 200
        },
        {                                  <---- Response for Index 2
          "took" : 2,
          "timed_out" : false,
          "_shards" : {
            "total" : 1,
            "successful" : 1,
            "skipped" : 0,
            "failed" : 0
          },
          "hits" : {
            "total" : {
              "value" : 4,
              "relation" : "eq"
            },
            "max_score" : 1.0,
            "hits" : [
              {
                "_index" : "myindex_news_economics",
                "_type" : "_doc",
                "_id" : "2",
                "_score" : 1.0,
                "_source" : {
                  "fooNested" : {
                    "sigma" : 9,
                    "theta" : 1
                  }
                }
              },
              {
                "_index" : "myindex_news_economics",
                "_type" : "_doc",
                "_id" : "1",
                "_score" : 1.0,
                "_source" : {
                  "fooNested" : {
                    "sigma" : 9,
                    "theta" : 1
                  }
                }
              }
            ]
          },
          "status" : 200
        }
      ]
    }
    

    不确定这是否适合您,希望对您有所帮助。

    【讨论】:

    • msearch 为每个索引提供分页。但是我想用 alias 跨两个索引进行分页?是否有任何解决方案可以为 msearch @Kamal 中的两个响应提供全局分页?
    • 不可能。您必须通过以事务方式发送两个请求然后合并结果来在客户端进行管理,我认为这或多或少类似于_msearch。我想知道您是否根据分数对结果进行排序。
    • 是的,排序是基于分数的。在客户端管理分页并不好。在每次搜索中,我都想为索引 B 设置最大文档数。
    • 如果你的排序是基于_score,通过做你正在做的事情,我不确定你的用例是什么,但结果可能不正确。因为您的用例希望显示一个索引的相关性值较低的文档,而不是与另一个索引相关性更高的文档。我喜欢认为这就是为什么当涉及到这样的用例时,ES 人员设计了_msearch,而不是按照你想要的方式来设计它,以防止我认为这种错误的结果顺序。但是恐怕,尽管您的用例可能是有效的,但您所要求的在 ES 中是不可能的。
    • ES 不支持该功能。 msearch 可以成为我的解决方案。我会接受你的回答。谢谢。
    猜你喜欢
    • 2015-08-17
    • 2020-06-25
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-04
    • 1970-01-01
    相关资源
    最近更新 更多