0

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

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

4

1 回答 1

2

您可以使用_msearchAPI 来实现。

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

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
    }
  ]
}

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

于 2019-06-13T08:26:09.190 回答