ElasticSearch5.x實踐_day05_03_Mapping_Meta-Fields

2、Meta-Fields(元數據)

2.1 _all

_all字段是把其它字段拼接在一塊兒的超級字段,全部的字段用空格分開,_all字段會被解析和索引,可是不存儲。當你只想返回包含某個關鍵字的文檔可是不明確地搜某個字段的時候就須要使用_all字段。
例子:java

GET http://192.168.20.46:9200/my_index/blog/1 
{
  "title":    "Master Java",
  "content":     "learn java",
  "author": "Tom"
}

_all字段包含:[ 「Master」, 「Java」, 「learn」, 「Tom」 ]  搜索:json

POST http://192.168.20.46:9200/my_index/_search?pretty
{
  "query": {
    "match": {
      "_all": "Java"
    }
  }
}

使用copy_to自定義_all字段:app

PUT http://192.168.20.46:9200/my_index
{
  "mappings": {
    "mytype": {
      "properties": {
        "title": {
          "type":    "text",
          "copy_to": "full_content" 
        },
        "content": {
          "type":    "text",
          "copy_to": "full_content" 
        },
        "full_content": {
          "type":    "text"
        }
      }
    }
  }
}
POST http://192.168.20.46:9200/my_index/mytype/1
{
  "title": "Master Java",
  "content": "learn Java"
}
POST http://192.168.20.46:9200/my_index/_search?pretty
{
  "query": {
    "match": {
      "full_content": "java"
    }
  }
}

2.2 _field_names

_field_names字段用來存儲文檔中的全部非空字段的名字,這個字段經常使用於exists查詢。例子以下:less

terms查詢ide

POST http://192.168.20.46:9200/my_index/my_type/1
{
  "title": "This is a document"
}
POST http://192.168.20.46:9200/my_index/my_type/2
{
  "title": "This is another document",
  "body": "This document has a body"
}

POST http://192.168.20.46:9200/my_index/_search
{
  "query": {
    "terms": {
      "_field_names": [ "body" ] 
    }
  }
}

結果會返回第二條文檔,由於第一條文檔沒有title字段。
一樣,可使用exists查詢ui

POST http://192.168.20.46:9200/my_index/_search
{
    "query": {
        "exists" : { "field" : "body" }
    }
}

2.3 _id

每條被索引的文檔都有一個_type和_id字段,_id能夠用於term查詢、temrs查詢、match查詢、query_string查詢、simple_query_string查詢,可是不能用於聚合、腳本和排序。例子以下:spa

POST http://192.168.20.46:9200/my_index/my_type/1
{
  "text": "Document with ID 1"
}

POST http://192.168.20.46:9200/my_index/my_type/2
{
  "text": "Document with ID 2"
}

POST http://192.168.20.46:9200/my_index/_search
{
  "query": {
    "terms": {
      "_id": [ "1", "2" ] 
    }
  }
}

2.4 _index

多索引查詢時,有時候只須要在特意索引名上進行查詢,_index字段提供了便利,也就是說能夠對索引名進行term查詢、terms查詢、聚合分析、使用腳本和排序。.net

_index是一個虛擬字段,不會真的加到Lucene索引中,對_index進行term、terms查詢(也包括match、query_string、simple_query_string),可是不支持prefix、wildcard、regexp和fuzzy查詢。debug

舉例,2個索引2條文檔code

http://192.168.20.46:9200/index_1/my_type/1
{
  "text": "Document in index 1"
}

http://192.168.20.46:9200/index_2/my_type/2
{
  "text": "Document in index 2"
}

POST http://192.168.20.46:9200/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",
        "inline": "doc['_index']" 
      }
    }
  }
}
{
    "took": 1210,
    "timed_out": false,
    "_shards": {
        "total": 10,
        "successful": 10,
        "failed": 0
    },
    "hits": {
        "total": 3,
        "max_score": null,
        "hits": [
            {
                "_index": "index_1",
                "_type": "my_type",
                "_id": "2",
                "_score": null,
                "fields": {
                    "index_name": [
                        "index_1"
                    ]
                },
                "sort": [
                    "index_1"
                ]
            },
            {
                "_index": "index_1",
                "_type": "my_type",
                "_id": "1",
                "_score": null,
                "fields": {
                    "index_name": [
                        "index_1"
                    ]
                },
                "sort": [
                    "index_1"
                ]
            },
            {
                "_index": "index_2",
                "_type": "my_type",
                "_id": "2",
                "_score": null,
                "fields": {
                    "index_name": [
                        "index_2"
                    ]
                },
                "sort": [
                    "index_2"
                ]
            }
        ]
    },
    "aggregations": {
        "indices": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": "index_1",
                    "doc_count": 2
                },
                {
                    "key": "index_2",
                    "doc_count": 1
                }
            ]
        }
    }
}

 

2.5 _parent

_parent用於指定同一索引中文檔的父子關係。下面例子中如今mapping中指定文檔的父子關係,而後索引父文檔,索引子文檔時指定父id,最後根據子文檔查詢父文檔。

POST http://192.168.20.46:9200/my_index
{
	"mappings":{
		"my_parent":{},
		"my_child":{
			"_parent":{
				"type":{
					"type":"my_parent"
				}
			}
		}
	}
}
POST http://192.168.20.46:9200/my_index/my_parent/1
{
  "text": "This is a parent document"
}

POST http://192.168.20.46:9200/my_index/my_child/2?parent=1 
{
  "text": "This is a child document"
}

POST http://192.168.20.46:9200/my_index/my_child/3?parent=1&refresh=true 
{
  "text": "This is another child document"
}
POST http://192.168.20.46:9200/my_index/my_parent/_search
{
  "query": {
    "has_child": { 
      "type": "my_child",
      "query": {
        "match": {
          "text": "child document"
        }
      }
    }
  }
}

 

2.6 _routing

路由參數,ELasticsearch經過如下公式計算文檔應該分到哪一個分片上:

shard_num = hash(_routing) % num_primary_shards

默認的_routing值是文檔的_id或者_parent,經過_routing參數能夠設置自定義路由。例如,想把user1發佈的博客存儲到同一個分片上,索引時指定routing參數,查詢時在指定路由上查詢:

POST http://192.168.20.46:9200/my_index/my_type/1?routing=user1&refresh=true
{
  "title": "This is a document"
}
GET http://192.168.20.46:9200/my_index/my_type/1?routing=user1
==>
{
    "_index": "my_index",
    "_type": "my_type",
    "_id": "1",
    "_version": 2,
    "_routing": "user1",
    "found": true,
    "_source": {
        "title": "This is a document"
    }
}
POST http://192.168.20.46:9200/my_index/_search
{
  "query": {
    "terms": {
      "_routing": [ "user1" ] 
    }
  }
}==>
{
    "took": 33,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.2876821,
        "hits": [
            {
                "_index": "my_index",
                "_type": "my_type",
                "_id": "1",
                "_score": 0.2876821,
                "_routing": "user1",
                "_source": {
                    "title": "This is a document"
                }
            }
        ]
    }
}
POST http://192.168.20.46:9200/my_index/_search?routing=user1,user2 
{
  "query": {
    "match": {
      "title": "document"
    }
  }
}

在Mapping中指定routing爲必須的:

PUT http://192.168.20.46:9200/my_index2
{
  "mappings": {
    "my_type": {
      "_routing": {
        "required": true 
      }
    }
  }
}
POST http://192.168.20.46:9200/my_index2/my_type/1 
{
  "text": "No routing value provided  routing_missing_exception"
}

2.7 _source

存儲的文檔的原始值。默認_source字段是開啓的,也能夠關閉:

POST http://192.168.20.46:9200/tweets
{
  "mappings": {
    "tweet": {
      "_source": {
        "enabled": false
      }
    }
  }
}

可是通常狀況下不要關閉,除法你不想作一些操做:

  • 使用update、update_by_query、reindex
  • 使用高亮
  • 數據備份、改變mapping、升級索引
  • 經過原始字段debug查詢或者聚合

2.8 _type

每條被索引的文檔都有一個_type和_id字段,能夠根據_type進行查詢、聚合、腳本和排序。例子以下:

POST http://192.168.20.46:9200/my_index/type_1/1
{
  "text": "Document with type 1"
}
POST http://192.168.20.46:9200/my_index/type_2/2?refresh=true
{
  "text": "Document with type 2"
}

POST http://192.168.20.46:9200/my_index/_search
{
  "query": {
    "terms": {
      "_type": [ "type_1", "type_2" ] 
    }
  },
  "aggs": {
    "types": {
      "terms": {
        "field": "_type", 
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_type": { 
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "type": {
      "script": {
        "lang": "painless",
        "inline": "doc['_type']" 
      }
    }
  }
}

2.9 _uid

_uid和_type和_index的組合。和_type同樣,可用於查詢、聚合、腳本和排序。例子以下:

POST http://192.168.20.46:9200/my_index/my_type/1
{
  "text": "Document with ID 1"
}
POST http://192.168.20.46:9200/my_index/my_type/2?refresh=true
{
  "text": "Document with ID 2"
}

POST http://192.168.20.46:9200/my_index/_search
{
  "query": {
    "terms": {
      "_uid": [ "my_type#1", "my_type#2" ] 
    }
  },
  "aggs": {
    "UIDs": {
      "terms": {
        "field": "_uid", 
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_uid": { 
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "UID": {
      "script": {
         "lang": "painless",
         "inline": "doc['_uid']" 
      }
    }
  }
}
相關文章
相關標籤/搜索