_index field

edit

When performing queries across multiple indexes, it is sometimes desirable to add query clauses that are associated with documents of only certain indexes. The _index field allows matching on the index a document was indexed into. Its value is accessible in certain queries and aggregations, and when sorting or scripting:

resp = client.index(
    index="index_1",
    id="1",
    document={
        "text": "Document in index 1"
    },
)
print(resp)

resp1 = client.index(
    index="index_2",
    id="2",
    refresh=True,
    document={
        "text": "Document in index 2"
    },
)
print(resp1)

resp2 = client.search(
    index="index_1,index_2",
    query={
        "terms": {
            "_index": [
                "index_1",
                "index_2"
            ]
        }
    },
    aggs={
        "indices": {
            "terms": {
                "field": "_index",
                "size": 10
            }
        }
    },
    sort=[
        {
            "_index": {
                "order": "asc"
            }
        }
    ],
    script_fields={
        "index_name": {
            "script": {
                "lang": "painless",
                "source": "doc['_index']"
            }
        }
    },
)
print(resp2)
response = client.index(
  index: 'index_1',
  id: 1,
  body: {
    text: 'Document in index 1'
  }
)
puts response

response = client.index(
  index: 'index_2',
  id: 2,
  refresh: true,
  body: {
    text: 'Document in index 2'
  }
)
puts response

response = client.search(
  index: 'index_1,index_2',
  body: {
    query: {
      terms: {
        _index: [
          'index_1',
          'index_2'
        ]
      }
    },
    aggregations: {
      indices: {
        terms: {
          field: '_index',
          size: 10
        }
      }
    },
    sort: [
      {
        _index: {
          order: 'asc'
        }
      }
    ],
    script_fields: {
      index_name: {
        script: {
          lang: 'painless',
          source: "doc['_index']"
        }
      }
    }
  }
)
puts response
const response = await client.index({
  index: "index_1",
  id: 1,
  document: {
    text: "Document in index 1",
  },
});
console.log(response);

const response1 = await client.index({
  index: "index_2",
  id: 2,
  refresh: "true",
  document: {
    text: "Document in index 2",
  },
});
console.log(response1);

const response2 = await client.search({
  index: "index_1,index_2",
  query: {
    terms: {
      _index: ["index_1", "index_2"],
    },
  },
  aggs: {
    indices: {
      terms: {
        field: "_index",
        size: 10,
      },
    },
  },
  sort: [
    {
      _index: {
        order: "asc",
      },
    },
  ],
  script_fields: {
    index_name: {
      script: {
        lang: "painless",
        source: "doc['_index']",
      },
    },
  },
});
console.log(response2);
PUT index_1/_doc/1
{
  "text": "Document in index 1"
}

PUT index_2/_doc/2?refresh=true
{
  "text": "Document in index 2"
}

GET index_1,index_2/_search
{
  "query": {
    "terms": {
      "_index": ["index_1", "index_2"] 
    }
  },
  "aggs": {
    "indices": {
      "terms": {
        "field": "_index", 
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_index": { 
        "order": "asc"
      }
    }
  ],
  "script_fields": {
    "index_name": {
      "script": {
        "lang": "painless",
        "source": "doc['_index']" 
      }
    }
  }
}

Querying on the _index field

Aggregating on the _index field

Sorting on the _index field

Accessing the _index field in scripts

The _index field is exposed virtually — it is not added to the Lucene index as a real field. This means that you can use the _index field in a term or terms query (or any query that is rewritten to a term query, such as the match, query_string or simple_query_string query), as well as prefix and wildcard queries. However, it does not support regexp and fuzzy queries.

Queries on the _index field accept index aliases in addition to concrete index names.

When specifying a remote index name such as cluster_1:index_3, the query must contain the separator character :. For example, a wildcard query on cluster_*:index_3 would match documents from the remote index. However, a query on cluster*index_1 is only matched against local indices, since no separator is present. This behavior aligns with the usual resolution rules for remote index names.