Elasticsearch(029):es中Meta-Fields(元数据类型)之概述(_index)

1. 概述

在多个索引中执行查询的时候,有时候需要添加子查询来关联特定的索引文档。_index字段可以匹配包含某个文档的索引。在term或terms查询,聚合,脚本以及排序的时候,可以访问_index字段的值。

注意:


_index是一个虚拟字段,不作为一个真实的字段添加到Lucene的索引中。这意味着可以在term或terms查询(任何可以重写term查询:如match.query_string,simple_query_string)中使用_index字段,但是不支持prefix、wildcard、regexp、fuzzy查询。

2. 示例

mappping定义和数据插入

PUT index_1
PUT index_1/docs/_mapping
{
    "properties":{
        "id":{"type": "long"},
        "title":{"type": "text"},
        "content":{"type": "text"}
    }
}
PUT index_1/docs/1
{
    "id": 1,
    "title": "Document title in index 1"
}

PUT index_2
PUT index_2/docs/_mapping
{
    "properties":{
        "id":{"type": "long"},
        "title":{"type": "text"},
        "content":{"type": "text"}
    }
}
PUT index_2/docs/1
{
    "id": 1,
    "title": "Document title 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']"
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章