_index 字段

编辑

当跨多个索引执行查询时,有时需要添加仅与某些索引的文档关联的查询子句。_index 字段允许匹配文档索引到的索引。它的值可以在某些查询和聚合中访问,并且可以在排序或脚本中使用。

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

_index 字段上查询

_index 字段上聚合

_index 字段上排序

在脚本中访问 _index 字段

_index 字段是虚拟暴露的,它不会作为真实字段添加到 Lucene 索引中。这意味着你可以在 termterms 查询(或任何被重写为 term 查询的查询,例如 matchquery_stringsimple_query_string 查询)中使用 _index 字段,以及 prefixwildcard 查询。但是,它不支持 regexpfuzzy 查询。

_index 字段的查询除了接受具体的索引名称外,还接受索引别名。

当指定远程索引名称(例如 cluster_1:index_3)时,查询必须包含分隔符 :。例如,对 cluster_*:index_3wildcard 查询将匹配远程索引中的文档。但是,对 cluster*index_1 的查询仅与本地索引匹配,因为没有分隔符。此行为与远程索引名称的通常解析规则一致。