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']"
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章