nest 類型是對(duì)象數(shù)據(jù)類型的一種特殊版本,允許以一種可以獨(dú)立查詢對(duì)象數(shù)組元素的方式對(duì)對(duì)象數(shù)組進(jìn)行索引。
為福鼎等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及福鼎網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、福鼎網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!Thenested
type is a specialised version of theobject
data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.
nest類型的官方文檔介紹
nested類型的限制:
// todo — 待更新
e.g.
PUT my-index-000001/_doc/1
{
"user": [
{
"first": "John",
"last": "Smith"
},
{
"first": "Alice",
"last": "White"
}
]
}
上述的插入文檔的操作在內(nèi)部會(huì)將其轉(zhuǎn)換成如下:
{
"user.first": ["john", "alice"],
"user.last": ["smith", "white"]
}
可以看出 user.first、user.last 字段都會(huì)平鋪成多值的字段,alice、white 之間還有 john、smith 之間的關(guān)聯(lián)關(guān)系會(huì)丟失。
導(dǎo)致下面的查詢的結(jié)果也是不準(zhǔn)確的:
GET my-index-000001/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice"
}
},
{
"match": {
"user.last": "Smith"
}
}
]
}
}
}
對(duì)此,可以采用 nested 類型解決這種問(wèn)題。
PUT my-index-000001
{
"mappings": {
"properties": {
"user": {
"type": "nested",
"properties": {
"first": {
"type": "text"
},
"last": {
"type": "text"
}
}
}
}
}
}
PUT my-index-000001/_doc/1
{
"user": [
{
"first": "John",
"last": "Smith"
},
{
"first": "Alice",
"last": "White"
}
]
}
GET my-index-000001/_search
{
"query": {
"nested": {
"path": "user",
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice"
}
},
{
"match": {
"user.last": "White"
}
}
]
}
}
}
}
}
Nested Query即嵌套查詢。針對(duì) nested 類型字段的查詢。
自己的理解:前面說(shuō)到 nested 類型是允許以一種可以獨(dú)立查詢對(duì)象數(shù)組元素的方式對(duì)對(duì)象數(shù)組進(jìn)行索引,也就是說(shuō)對(duì)象數(shù)組元素即子對(duì)象作為一個(gè)整體進(jìn)行匹配,如果匹配成功,則返回父文檔。
nested query 的官方文檔介紹
path:(必須)想要查詢的嵌套對(duì)象
query:(必須)基于嵌套對(duì)象,想要查詢的內(nèi)容
score_mode:匹配的子對(duì)象集合的分?jǐn)?shù)如何影響父文檔的關(guān)聯(lián)性分?jǐn)?shù)
ignore_unmapped:當(dāng) path 沒(méi)有匹配到時(shí),如果設(shè)置為 true,則返回空文檔;如果設(shè)置為 false,則報(bào)錯(cuò)。默認(rèn) false
多層嵌套
PUT drivers
{
"mappings": {
"properties": {
"driver": {
"type": "nested",
"properties": {
"last_name": {
"type": "text"
},
"vehicle": {
"type": "nested",
"properties": {
"make": {
"type": "text"
},
"model": {
"type": "text"
}
}
}
}
}
}
}
}
PUT /drivers/_doc/1
{
"driver": {
"last_name": "McQueen",
"vehicle": [
{
"make": "Powell Motors",
"model": "Canyonero"
},
{
"make": "Miller-Meteor",
"model": "Ecto-1"
}
]
}
}
PUT /drivers/_doc/2?refresh
{
"driver": {
"last_name": "Hudson",
"vehicle": [
{
"make": "Mifune",
"model": "Mach Five"
},
{
"make": "Miller-Meteor",
"model": "Ecto-1"
}
]
}
}
GET drivers/_search
{
"query": {
"nested": {
"path": "driver",
"query": {
"nested": {
"path": "driver.vehicle",
"query": {
"bool": {
"must": [
{
"match": {
"driver.vehicle.make": "Powell Motors"
}
},
{
"match": {
"driver.vehicle.model": "Canyonero"
}
}
]
}
}
}
}
}
}
}
Join數(shù)據(jù)類型join 類型是一種特殊類型,可以在相同索引下的多個(gè)文檔中創(chuàng)建父子關(guān)系。它會(huì)在 relations 字段中定義一組可能的關(guān)系,包括父名稱和子名稱。
Thejoin
data type is a special field that creates parent/child relation within documents of the same index. Therelations
section defines a set of possible relations within the documents, each relation being a parent name and a child name.
e.g.
PUT my-index-000001
{
"mappings": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"question": "answer"
}
},
"text": {
"type": "text"
}
}
}
}
PUT my-index-000001/_doc/1
{
"my_join_field": {
"name": "question"
},
"text": "This is a question"
}
對(duì)于父文檔來(lái)說(shuō),上述的文檔可以簡(jiǎn)寫(xiě)成如下這種形式:
PUT my-index-000001/_doc/1
{
"my_join_field": "question",
"text": "This is a question"
}
但是對(duì)于子文檔來(lái)說(shuō),name、parent 一定要有。
PUT my-index-000001/_doc/3?routing=1
{
"my_join_field": {
"name": "answer",
"parent": "1"
},
"text": "This is an answer"
}
創(chuàng)建子文檔,必須指定routing參數(shù)值
join 類型的限制
join 類型的字段使用全局序號(hào)來(lái)提高連接查詢的速度。在分片發(fā)生變化后,需要重建全局序號(hào)。存儲(chǔ)在一個(gè)分片中的 parent id 值越多,重建 join 類型的字段的全局序號(hào)所需的時(shí)間越長(zhǎng)。默認(rèn)情況下,全局序號(hào)會(huì)預(yù)先構(gòu)建,如果索引發(fā)生了變化,join 類型的字段的全局序號(hào)就會(huì)作為刷新的一部分進(jìn)行重新構(gòu)建。
如果 join 類型的字段不經(jīng)常使用并且寫(xiě)操作頻繁時(shí),可以選擇禁用預(yù)先加載全局序號(hào),如下:
PUT my-index-000001
{
"mappings": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"question": "answer"
},
"eager_global_ordinals": false
}
}
}
}
此外,可以一個(gè)父關(guān)系對(duì)應(yīng)多個(gè)子關(guān)系:
PUT my-index-000001
{
"mappings": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"question": ["answer", "comment"]
}
}
}
}
}
還可以定義多層的關(guān)系模型:
PUT my-index-000001
{
"mappings": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"question": ["answer", "comment"],
"answer": "vote"
}
}
}
}
}
不推薦使用上述的多層的關(guān)系模型。每一層級(jí)關(guān)系在查詢時(shí)都會(huì)增加內(nèi)存和計(jì)算的開(kāi)銷。
Has_Child Query返回與提供的查詢相匹配的子文檔關(guān)聯(lián)的父文檔??梢允褂?join 類型的字段在不同文檔中建立父子關(guān)系。
Returns parent documents whose joined child documents match a provided query. You can create parent-child relationships between documents in the same index using a join field mapping.
因?yàn)?has_child 查詢執(zhí)行關(guān)聯(lián)查詢,所以它比其它查詢要慢。并且它會(huì)隨著匹配的子文檔數(shù)量增加而性能降低。搜索中的每個(gè) has_child 查詢會(huì)顯著增加查詢時(shí)間。
Because it performs a join, the has_child is slow compared to other queries. Its performance degrades as the number of matching child documents pointing to unique parent documents increases. Eachhas_child
query in a search can increase query time significantly.
has_child query 的官方文檔介紹
type:(必須)join 類型的字段中定義的子關(guān)系名稱
query:(必須)基于 type 查詢對(duì)應(yīng)的子文檔,如果有子文檔匹配查詢,則返回與之關(guān)聯(lián)的父文檔
ignore_unmapped:如果 type 沒(méi)有匹配到,選擇返回空內(nèi)容還是返回一個(gè)錯(cuò)誤。默認(rèn) false
max_children:要求父文檔中子文檔的數(shù)量<=該值時(shí),父文檔才能從結(jié)果中返回
min_children:要求父文檔中子文檔的數(shù)量>=該值時(shí),父文檔才能從結(jié)果中返回
score_mode:匹配的子文檔的分?jǐn)?shù)如何影響父文檔的關(guān)聯(lián)性分?jǐn)?shù)。默認(rèn) none
e.g.
PUT my-index-000001
{
"mappings": {
"properties": {
"my_join_field": {
"type": "join",
"relations": {
"question": "answer"
}
},
"text": {
"type": "text"
}
}
}
}
PUT my-index-000001/_doc/1
{
"my_join_field": {
"name": "question"
},
"text": "This is a question"
}
PUT my-index-000001/_doc/2
{
"my_join_field": {
"name": "question"
},
"text": "This is another question"
}
PUT my-index-000001/_doc/3?routing=1
{
"my_join_field": {
"name": "answer",
"parent": "1"
},
"text": "This is an answer"
}
PUT my-index-000001/_doc/4?routing=2
{
"my_join_field": {
"name": "answer",
"parent": "2"
},
"text": "This is another answer"
}
PUT my-index-000001/_doc/5?routing=2
{
"my_join_field": {
"name": "answer",
"parent": "2"
},
"text": "This is another answer"
}
創(chuàng)建子文檔,必須指定routing參數(shù)值
GET my-index-000001/_search
{
"query": {
"has_child": {
"type": "answer",
"ignore_unmapped": true,
"min_children": 2,
"query": {
"match": {
"text": "answer"
}
}
}
}
}
值得注意的是,不能使用標(biāo)準(zhǔn)的 sort options 對(duì) has_child 查詢結(jié)果排序,但是可以使用 function_score 查詢,并且按照 _score 排序。
GET my-index-000001/_search
{
"query": {
"has_child": {
"type": "answer",
"score_mode": "max",
"query": {
"function_score": {
"query": {
"match": {
"text": "answer"
}
},
"script_score": {
"script": "_score"
}
}
}
}
}
}
Has_Parent Query返回與提供的查詢相匹配的父文檔關(guān)聯(lián)的子文檔??梢允褂?join 類型的字段在不同文檔中建立父子關(guān)系。
Returns child documents whose joined parent documents match a provided query. You can create parent-child relationships between documents in the same index using a join field mapping.
因?yàn)?has_parent 查詢執(zhí)行關(guān)聯(lián)查詢,所以它比其它查詢要慢。并且它會(huì)隨著匹配的父文檔的數(shù)量增加而性能降低。搜索中的每個(gè) has_parent 查詢會(huì)顯著增加查詢時(shí)間。
Because it performs a join, thehas_parent
query is slow compared to other queries. Its performance degrades as the number of matching parent documents increases. Eachhas_parent
query in a search can increase query time significantly.
has_parent query 的官方文檔介紹
parent_type:(必須)join 類型的字段中定義的父關(guān)系名稱
query:(必須)基于 parent_type 查詢對(duì)應(yīng)的父文檔,如果有父文檔匹配查詢,則返回與之關(guān)聯(lián)的子文檔
ignore_unmapped:如果 parent_type 沒(méi)有匹配到,選擇返回空內(nèi)容還是返回一個(gè)錯(cuò)誤。默認(rèn) false
score:是否將匹配到的父文檔的關(guān)聯(lián)性分?jǐn)?shù)聚合到對(duì)應(yīng)的子文檔中。默認(rèn) false
e.g.
GET my-index-000001/_search
{
"query": {
"has_parent": {
"parent_type": "question",
"score": false,
"query": {
"match": {
"text": "question"
}
}
}
}
}
值得注意的是,不能使用標(biāo)準(zhǔn)的 sort options 對(duì) has_child 查詢結(jié)果排序,但是可以使用 function_score 查詢,并且按照 _score 排序。
GET my-index-000001/_search
{
"query": {
"has_parent": {
"parent_type": "question",
"score": true,
"query": {
"function_score": {
"query": {
"match": {
"text": "question"
}
},
"script_score": {
"script": "_score"
}
}
}
}
}
}
Parent_Id Query返回與指定父文檔關(guān)聯(lián)的所有子文檔??梢允褂?join 類型的字段在不同文檔中建立父子關(guān)系。
Returns child documents joined to a specific parent document. You can use a join field mapping to create parent-child relationships between documents in the same index.
parent_id query 官方文檔的介紹
type:(必須)join 類型字段定義的子關(guān)系
id:(必須)父文檔的id
ignore_unmapped:如果 type 沒(méi)有匹配到,選擇返回空內(nèi)容還是返回一個(gè)錯(cuò)誤。默認(rèn) false
true:返回空內(nèi)容
false:返回一個(gè)錯(cuò)誤
e.g.
GET my-index-000001/_search
{
"query": {
"parent_id": {
"type": "answer",
"id": 1
}
}
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧