国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

ES實(shí)現(xiàn)三表關(guān)聯(lián)查詢+條件過(guò)濾

這篇具有很好參考價(jià)值的文章主要介紹了ES實(shí)現(xiàn)三表關(guān)聯(lián)查詢+條件過(guò)濾。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

需求背景

????????很多時(shí)候mysql的表之間是一對(duì)多的關(guān)系,比如庫(kù)信息表(元數(shù)據(jù)信息),表信息表(元數(shù)據(jù)信息),字段信息表(元數(shù)據(jù)信息)。一個(gè)庫(kù)可以包含多個(gè)表,一個(gè)表可以包含多個(gè)字段。他們的關(guān)系:庫(kù)—(1:n)->表—(1:n)->字段。

????????ElasticsSearch(以下簡(jiǎn)稱(chēng)ES)處理這種關(guān)系雖然不是特別擅長(zhǎng)(相對(duì)于關(guān)系型數(shù)據(jù)庫(kù)),因?yàn)镋S和大多數(shù) NoSQL 數(shù)據(jù)庫(kù)類(lèi)似,是扁平化的存儲(chǔ)結(jié)構(gòu)。索引是獨(dú)立文檔的集合體。不同的索引之間一般是沒(méi)有關(guān)系的。

不過(guò)ES目前畢竟發(fā)展到8.x版本了, 已經(jīng)有幾種可選的方式能夠高效的支持這種一對(duì)多關(guān)系的映射。

????????比較常用的方案是嵌套對(duì)象,嵌套文檔和父子文檔。后兩種是我們本文要講的內(nèi)容。

表結(jié)構(gòu)

? ? ? ? 為了便于描述下面的demo內(nèi)容,現(xiàn)在先介紹一下表結(jié)構(gòu)demo內(nèi)容(表名稱(chēng):字段1,字段2,字段3......)

database: database_id, name, desc

table:table_id,name,desc,address

column:column_id,name,desc,address文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-755100.html

嵌套文檔查詢實(shí)例

#建立索引元數(shù)據(jù):兩層嵌套 database->table->column
put http://localhost:9200/test_nested
{
	"mappings": {
		"properties": {
			"table": {
				"type": "nested",
				"properties": {
					"column": {
						"type": "nested" 
					}
				}
		 
			}
		}
	}
}
#創(chuàng)建1個(gè)庫(kù)數(shù)據(jù)database1
PUT http://localhost:9200/test_nested/_doc/database1
{
	"database_id": 1,
	"name" : "database1",
	"des" : "This is a database!",
	"table" : [
		{
		  "table_id":1,
		  "name" : "John",
		  "des" :  "This is a table!",
		  "address":"hangzhou",
		  "column":[
				{
					"column_id":1,
					"name" :"zhangsan",
					"des" :  "This is a column!",
					"address":"wuhan"
				},
				{
					"column_id":2,
					"name" :"Alice",
					"des" :  "This is a column!",	
					"address":"changchun"
				}
			]
		},
		{
		  "table_id":2,
		  "name" : "Alice",
		  "des" :  "This is a table!",
		  "address":"changchun",
		  "column":[
				{
					"column_id":3,
					"name" :"zhangsan",
					"des" :  "This is a column!",	
					"address":"hangzhou"
				},
				{
					"column_id":4,
					"name" :"John",
					"des" :  "This is a column!",	
					"address":"zhengzhou"
				}
			]
		}
	]
}

#創(chuàng)建1個(gè)庫(kù)數(shù)據(jù)database2
PUT http://localhost:9200/test_nested/_doc/database2
{
	"database_id": 2,
	"name" : "database2",
	"des" : "This is a database!",
	"table" : [
		{
		  "table_id":3,
		  "name" : "zhangsan",
		  "des" :  "This is a table!",
		  "address":"wuhan",
		  "column":[
				{
					"column_id":5,
					"name" :"John",
					"des" :  "This is a column!",
					"address":"hangzhou"
				},
				{
					"column_id":6,
					"name" :"Alice",
					"des" :  "This is a column!",	
					"address":"changchun"
				}
			]
		},
		{
		  "table_id":4,
		  "name" : "Alice",
		  "des" :  "This is a table!",
		  "address":"changchun",
		  "column":[
				{
					"column_id":7,
					"name" :"zhangsan",
					"des" :  "This is a column!",	
					"address":"hangzhou"
				},
				{
					"column_id":8,
					"name" :"John",
					"des" :  "This is a column!",	
					"address":"zhengzhou"
				}
			]
		}
	]
}

#嵌套查詢例子,查詢column匹配指定內(nèi)容,且table匹配指定內(nèi)容的文檔
POST http://localhost:9200/test_nested/_search
{
	"query" : {
		"bool": {
			"must": [ 
				{
					"nested": {
						"path": "table",
						"query": {
							"bool": {
								"must": [
									{ 
										"match": { 
											"table.address": "hangzhou" 
										}
									},
									{ 
										"match": { 
											"table.name": "John" 
										}
									}
								]
							}
						}
					}
				},
				{
					"nested": {
						"path": "table.column",
						"query" : {
							"bool": {
								"must": [
									{ 
										"match": { 
											"table.column.address": "wuhan" 
										}
									},
									{ 
										"match": { 
											"table.column.name": "zhangsan" 
										}
									}
								 ]
							}

						}
					}
				}
			]
		}
	}
}

#實(shí)現(xiàn)類(lèi)似"三表關(guān)聯(lián)查詢+條件過(guò)濾",查詢cloumn匹配指定內(nèi)容,或table匹配指定內(nèi)容,或database匹配指定內(nèi)容的文檔
POST http://localhost:9200/test_nested/_search
{
	"query" : {
		"bool": {
			"should": [ 
				{
					"nested": {
						"path": "table",
						"query": {
							"bool": {
								"must": [
									{ 
										"match": { 
											"table.address": "hangzhou" 
										}
									},
									{ 
										"match": { 
											"table.name": "John" 
										}
									}
								]
							}
						}
					}
				},
				{
					"nested": {
						"path": "table.column",
						"query" : {
							"bool": {
								"must": [
									{ 
										"match": { 
											"table.column.address": "hangzhou" 
										}
									},
									{ 
										"match": { 
											"table.column.name": "John" 
										}
									}
								 ]
							}

						}
					}
				},
				{
					"match" :{
						"name":"hangzhou"
					}
				}
			]
		}
	}
}

父子文檔查詢實(shí)例

#創(chuàng)建索引元數(shù)據(jù)
put http://localhost:9200/metadata1
{
  "mappings": {
    "properties": {
      "my_join_field": {
        "type": "join",
        "relations": {
          "database": ["table"],
          "table": ["column"]
        }
      }
    }
  }
}

#創(chuàng)建1個(gè)父文檔
put http://localhost:9200/metadata1/_doc/1
{
  "database_id": "1",
  "des": "This is a database!",
  "name":"zhangsan",
  "address":"hangzhou",
  "my_join_field": {
    "name": "database" 
  }
}

#創(chuàng)建1個(gè)子文檔
put http://localhost:9200/metadata1/_doc/2?routing=1
{
  "table_id": "1",
  "des": "This is a table!",
  "name":"lisi",
  "address":"hangzhou",
  "my_join_field": {
    "name": "table",
	"parent":1
  }
}

#創(chuàng)建1個(gè)孫子文檔
put http://localhost:9200/metadata1/_doc/3?routing=2
{
  "column_id": "1",
  "des": "This is a column!",
  "name":"wangwu",
  "address":"hangzhou",
  "my_join_field": {
    "name": "column",
	"parent":2
  }
}

#創(chuàng)建1個(gè)孫子文檔
put http://localhost:9200/metadata1/_doc/4?routing=2
{
  "column_id": "2",
  "des": "This is a column!",
  "name":"hangzhou",
  "address":"zhengzhou",
  "my_join_field": {
    "name": "column",
	"parent":2
  }
}

#創(chuàng)建1個(gè)孫子文檔,用于驗(yàn)證查詢內(nèi)容默認(rèn)分詞了
put http://localhost:9200/metadata1/_doc/5?routing=2
{
  "column_id": "3",
  "des": "This is a column!",
  "name":"hangzhouren",
  "address":"hangzhou city",
  "my_join_field": {
    "name": "column",
	"parent":2
  }
}

#分頁(yè)查詢某個(gè)字段(查詢范圍包括父,子,孫子文檔)
post http://localhost:9200/metadata1/_search
{
  "query" : {
		"match": {
			"address" : "hangzhou"
		}
	},
	"from" : 1,
	"size" : 1
}

#term 批量查詢
post http://localhost:9200/metadata1/_search
{
	"query": {
		"terms" : {
			"address":["hangzhou pro","zhengzhou"]
		}
	}
}

#查詢具備滿足匹配內(nèi)容的孫子文檔的子文檔
post http://localhost:9200/metadata1/_search
{
  "query": {
    "has_child": {
      "type": "column",
      "query" : {
			"match": {
				"address" : "hangzhou"
			}
		}
    }
  }
}

#查詢具備滿足匹配內(nèi)容的子文檔的父文檔
post http://localhost:9200/metadata1/_search
{
  "query": {
    "has_child": {
      "type": "table",
      "query" : {
			"match": {
				"address" : "hangzhou"
			}
		}
    }
  }
}

#查詢具備滿足匹配內(nèi)容的孫子文檔的父文檔
post http://localhost:9200/metadata1/_search
{
  "query": {
    "has_child": {
      "type": "table",
      "query" : {
			"has_child": {
				"type": "column",
				"query" : {
					"multi_match": {
						"query" : "hangzhou",
						"fields":["address","name"]
					}
				}
			}
		}
    }
  }
}

#bool查詢滿足條件孫子文檔的父文檔,和滿足條件子文檔的父文檔
post http://localhost:9200/metadata1/_search
{
	"query": {
		"bool": {
			"should": [
				{
					"has_child": {
					  "type": "table",
					  "query" : {
							"has_child": {
								"type": "column",
								"query" : {
									"multi_match": {
										"query" : "hangzhou",
										"fields":["address","name"]
									}
								}
							}
						}
					}
				},
				{
					"has_child": {
						"type": "table",
						"query" : {
							"multi_match": {
								"query" : "hangzhou",
								"fields":["address","name"]
							}
						}
					}
				}
			]
		}
	}	
	 
}

#查詢滿足條件子文檔的父文檔的子文檔,即子文檔本身;如果父,子,孫文檔的文檔字段名稱(chēng)不同,就不用這么麻煩的查詢
post http://localhost:9200/metadata1/_search
{
	"query": {
		"has_parent": {
			"parent_type": "database",
			"query" : {
				"has_child": {
					"type": "table",
					"query" : {
						"multi_match": {
							"query" : "hangzhou",
							"fields":["address","name"]
						}
					}
				}
			}

		}
	}	
	 
}


#以下兩條查詢可以類(lèi)似實(shí)現(xiàn)"三表關(guān)聯(lián)查詢+條件過(guò)濾"的功能
#先查詢滿足條件匹配的父文檔的子文檔,滿足條件匹配孫子文檔的子文檔和滿足條件匹配的子文檔
post http://localhost:9200/metadata1/_search
{
	"query": {
		"bool": {
			"should": [
				{
					"has_parent": {
					  "parent_type": "database",
							"query" : {
								"multi_match": {
									"query" : "hangzhou",
									"fields":["address","name"]
								}
							}
					}
				},
				{
					"has_child": {
						"type": "column",
						"query" : {
							"multi_match": {
								"query" : "hangzhou",
								"fields":["address","name"]
							}
						}
					}
				},
				{
					"has_parent": {
						"parent_type": "database",
						"query" : {
							"has_child": {
								"type": "table",
								"query" : {
									"multi_match": {
										"query" : "hangzhou",
										"fields":["address","name"]
									}
								}
							}
						}

					}
				}
			]
		}
	}	
	 
}

#根據(jù)上面的子文檔查詢關(guān)聯(lián)的父文檔和孫子文檔,然后再在程序里進(jìn)行數(shù)據(jù)關(guān)聯(lián)組裝
post http://localhost:9200/metadata1/_search
{
	"query": {
		"bool": {
			"should": [
				{
					"has_parent": {
					  "parent_type": "table",
							"query" : {
								"ids": {
									"values" : [2]
								}
							}
					}
				},
				{
					"has_child": {
						"type": "table",
						"query" : {
							"ids": {
									"values" : [2]
								}
						}
					}
				}
			]
		}
	}	
	 
}


到了這里,關(guān)于ES實(shí)現(xiàn)三表關(guān)聯(lián)查詢+條件過(guò)濾的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • ES7 and or 關(guān)聯(lián)條件查詢JAVA

    @ES7 and or 關(guān)聯(lián)條件查詢JAVA 實(shí)現(xiàn)條件( platform=‘xxx’ and (home_path=‘xxx’ or nick_name=‘xxx’ )) kibana

    2024年02月08日
    瀏覽(22)
  • ElasticSearch第十四講 ES有條件復(fù)雜查詢

    模糊匹配 模糊匹配主要是針對(duì)文本類(lèi)型的字段,文本類(lèi)型的字段會(huì)對(duì)內(nèi)容進(jìn)行分詞,對(duì)查詢時(shí),也會(huì)對(duì)搜索條件進(jìn)行分詞,然后通過(guò)倒排索引查找到匹配的數(shù)據(jù),模糊匹配主要通過(guò)match等參數(shù)來(lái)實(shí)現(xiàn) match : 通過(guò)match模糊匹配條件內(nèi)容 prefix : 前綴匹配 regexp : 通過(guò)正則表達(dá)

    2024年02月03日
    瀏覽(24)
  • ElasticSearch系列 - SpringBoot整合ES:組合多個(gè)查詢條件 bool 查詢

    01. ElasticSearch 布爾查詢是什么? 在實(shí)際應(yīng)用中,我們很有可能會(huì)查詢多個(gè)值或字段。 一個(gè) bool 查詢由三部分組成: must:所有的語(yǔ)句都必須(must) 匹配,與 AND 等價(jià)。 must_not:所有的語(yǔ)句都不能(must not)匹配,與 NOT 等價(jià)。 should:至少有一個(gè)語(yǔ)句要匹配,與 OR 等價(jià)。 02.

    2023年04月08日
    瀏覽(28)
  • 【Elasticsearch】ES查詢出滿足以下其中任意一個(gè)條件的訂單

    需求:最近測(cè)試經(jīng)理給我這邊提出一個(gè)需求,大致可以描述為查詢出\\\" 購(gòu)買(mǎi)的商品名稱(chēng)中包含’測(cè)試’ “、” 訂單的收貨地址包含’B360’ \\\"、“收貨人手機(jī)號(hào)為測(cè)試人員的手機(jī)號(hào)”; 一、實(shí)現(xiàn)方案 當(dāng)時(shí)評(píng)估了兩種方案: ①、直接從數(shù)據(jù)庫(kù)中查詢; ②、從ES中查詢; 方案①

    2024年02月16日
    瀏覽(20)
  • sql語(yǔ)句轉(zhuǎn)為es查詢條件(elasticsearch-sql使用)

    github源碼地址: https://gitee.com/weiyxiong_admin/elasticsearch-sql/blob/master/src/test/java/org/nlpcn/es4sql/ExplainTest.java 1、添加pom.xml依賴 2、scala 將sql轉(zhuǎn)為es查詢json語(yǔ)句 3、測(cè)試 4、查詢返回結(jié)果展示(即步驟三esJSON結(jié)果打?。?5、打開(kāi)postman

    2024年02月13日
    瀏覽(23)
  • java 整合ES實(shí)現(xiàn)文檔增刪改查(多條件分頁(yè)查詢)

    本文采用ES版本為8.7.1 由于只存儲(chǔ)文章,僅用固定索引即可,索引用kibanna直接生成,省略索引部分的增刪查步驟 抓取返回信息是因?yàn)榘姹締?wèn)題無(wú)法解析ES返回的正確信息,實(shí)際操作成功但是會(huì)報(bào)錯(cuò) 我這邊只需要單索引操作,有需求的可以讓前端傳過(guò)來(lái)

    2024年02月13日
    瀏覽(27)
  • 【elasticsearch】ES去重查詢實(shí)現(xiàn)

    去重實(shí)現(xiàn)原理: 采用es 的Collapse折疊+cardinality基數(shù)計(jì)算 實(shí)現(xiàn)去重 1、優(yōu)點(diǎn):簡(jiǎn)單快速效率高,幾乎無(wú)性能損耗(相比于分桶去重) 2、缺點(diǎn): 1)Collapse折疊只支持一個(gè)字段去重,且字段必須是 keyword 2)cardinality基數(shù)計(jì)算去重后數(shù)量 (采用hyperloglog實(shí)現(xiàn),hyperloglog一種近似計(jì)算)

    2024年02月06日
    瀏覽(17)
  • ES(Elasticsearch)+SpringBoot實(shí)現(xiàn)分頁(yè)查詢

    1.ES介紹 ??ES作為一個(gè)搜索工具,寄托于Lucene之上,提供了方便的數(shù)據(jù)存儲(chǔ)和搜索服務(wù),一般的用它來(lái)作為網(wǎng)頁(yè)數(shù)據(jù)索引以及存儲(chǔ)用戶畫(huà)像(即用戶標(biāo)簽)數(shù)據(jù),可以提供復(fù)具有復(fù)雜的查詢條件的服務(wù)。例如在網(wǎng)頁(yè)索引中,通過(guò)倒排的方式索引的方式,對(duì)文檔進(jìn)行分詞存儲(chǔ),

    2024年02月16日
    瀏覽(25)
  • ElasticSearch入門(mén):使用ES來(lái)實(shí)現(xiàn)模糊查詢功能

    本文針對(duì)在工作中遇到的需求:通過(guò)es來(lái)實(shí)現(xiàn) 模糊查詢 來(lái)進(jìn)行總結(jié);模糊查詢的具體需求是:查詢基金/A股/港股等金融數(shù)據(jù),要求可以根據(jù) 字段 , 拼音首字母 , 部分拼音全稱(chēng) 進(jìn)行聯(lián)想查詢;需要注意的是,金融數(shù)據(jù)名稱(chēng)中可能不止包含漢字,還有英文,數(shù)字,特殊字符等

    2023年04月09日
    瀏覽(23)
  • ES多條件查詢

    場(chǎng)景:想要查詢出設(shè)備id 為 3 的所有設(shè)備上報(bào)信息,并且上報(bào)信息的開(kāi)始時(shí)間或結(jié)束時(shí)間只要有一個(gè)滿足 在 某個(gè)時(shí)間段里即可。 在 Elasticsearch 中,您可以使用布爾查詢來(lái)實(shí)現(xiàn)同時(shí)滿足條件1并且滿足條件2或條件3中的一個(gè)的查詢。具體來(lái)說(shuō),您可以使用“must”子句來(lái)指定滿足

    2024年02月09日
    瀏覽(24)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包