elasticsearchelasticsearch-indices

Elasticsearch max document count for each index


I have two separate indices A and B with alias X. Both indices have same document structure. When I search with size = 20 on alias X , I want to set max document size 5 for index B. The search result should contains max 5 documents from index B. If there is no document in index B, search result should contains 20 documents from index A.

Is there any solution to set max document count per index for searchin across multiple index with alias?


Solution

  • You can achieve that using _msearch API.

    Below is a sample query as how you can apply this:

    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}
    

    Basically, _msearch allows you to search multiple requests using the same API. There are two different queries, where you can specify size for two different indexes.

    Note that the results would appear in separate groups of JSONs(two results, one for each query).

    Sample Response:

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

    Not sure if this would be ideal for you, I just hope this helps.