当前位置:网站首页>es-object vs nested vs has_ child and has_ parent
es-object vs nested vs has_ child and has_ parent
2022-06-22 01:57:00 【Tong Xiaolv】
1.object type
increase object Index of type
PUT /joining_query_object_test/_doc/1
{
"obj1": {
"name": "blue",
"count": 5
},
"tags": [
" billboard ",
" The golden bull Award "
],
"comments": [
{
"name": " Sima yi ",
"comment": " Wisdom is actually , Fools contend for fame ",
"age": 28,
"stars": 4,
"date": "2022-05-01"
},
{
"name": " Liu Bowen ",
"comment": " Peace is followed by purity , Qing Dynasty and then Ming Dynasty ",
"age": 31,
"stars": 5,
"date": "2022-05-11"
}
],
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
PUT /joining_query_object_test/_doc/2
{
"obj1": {
"name": "red",
"count": 3
},
"tags": [
"1h Hot stock ",
"24h Hot stock "
],
"comments": [
{
"name": " Liu Ji ",
"comment": " Have wine and enjoy it , Listen to my Xielu song ",
"age": 42,
"stars": 3,
"date": "2022-05-02"
},
{
"name": " Yuan Tiangang ",
"comment": " Xingyiluoshui , Shuai also Luoshui ",
"age": 32,
"stars": 4,
"date": "2022-05-12"
}
],
"group" : "fans",
"user" : [
{
"first" : "xiaoping",
"last" : "liu"
},
{
"first" : "tianji",
"last" : "li"
}
]
}
see mapping
GET /joining_query_object_test/_mapping/
## The age of inquiry is 32 Liu Ji, aged, commented
## In theory, it should hit 1, result hit 2
## Liu Jishi 42 year ,32 Yuantiangang, aged , Not the same comment at all
## reason :object Type the underlying data structure , It will json Data in array , Flattening
GET /joining_query_object_test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": " Liu Ji "
}
},
{
"match": {
"comments.age": 32
}
}
]
}
}
}
## verification :
##+(+(comments.name: Liu comments.name: The base ) +comments.age:[32 TO 32]) #MatchNoDocsQuery("")
GET /joining_query_object_test/_mapping/_validate/query?explain
{
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": " Liu Ji "
}
},
{
"match": {
"comments.age": 32
}
}
]
}
}
}
## Inquire about first=Alice and last=Smith There is a problem of inaccurate data
GET joining_query_object_test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice" }},
{
"match": {
"user.last": "Smith" }}
]
}
}
}
2.nested query
- doc Must be nested type .
- nested Type is to solve object The problem of types losing associativity on an array of objects
(1)nested query
## Set up mapping
DELETE joining_query_nested_test
PUT /joining_query_nested_test
{
"mappings": {
"properties": {
"obj1": {
"type": "nested"
},
"comments": {
"type": "nested",
"properties": {
"name": {
"type": "text" },
"comment": {
"type": "text" },
"age": {
"type": "short" },
"stars": {
"type": "short" },
"date": {
"type": "date" }
}
},
"user": {
"type": "nested"
}
}
}
}
## increase doc
PUT /joining_query_nested_test/_doc/1
{
"obj1": {
"name": "blue",
"count": 5
},
"tags": [
" billboard ",
" The golden bull Award "
],
"comments": [
{
"name": " Sima yi ",
"comment": " Wisdom is actually , Fools contend for fame ",
"age": 28,
"stars": 4,
"date": "2022-05-01"
},
{
"name": " Liu Bowen ",
"comment": " Peace is followed by purity , Qing Dynasty and then Ming Dynasty ",
"age": 31,
"stars": 5,
"date": "2022-05-11"
}
],
"group" : "fans",
"user" : [
{
"first" : "John",
"last" : "Smith"
},
{
"first" : "Alice",
"last" : "White"
}
]
}
PUT /joining_query_nested_test/_doc/2
{
"obj1": {
"name": "red",
"count": 3
},
"tags": [
"1h Hot stock ",
"24h Hot stock "
],
"comments": [
{
"name": " Liu Ji ",
"comment": " Have wine and enjoy it , Listen to my Xielu song ",
"age": 42,
"stars": 3,
"date": "2022-05-02"
},
{
"name": " Yuan Tiangang ",
"comment": " Xingyiluoshui , Shuai also Luoshui ",
"age": 32,
"stars": 4,
"date": "2022-05-12"
}
],
"group" : "fans",
"user" : [
{
"first" : "xiaoping",
"last" : "liu"
},
{
"first" : "tianji",
"last" : "li"
}
]
}
## Inquire about mapping(object nested)
GET /joining_query_object_test/_mapping/
GET /joining_query_nested_test/_mapping/
## The age of inquiry is 32 Liu Ji, aged, commented
## solve object The flattening problem of , Liu Jishi 42 year ,32 Yuantiangang, aged , Not the same comment at all
## result :hit 0 In line with expectations
GET /joining_query_nested_test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": " Liu Ji "
}
},
{
"match": {
"comments.age": 32
}
}
]
}
}
}
##_validate Check it out
##+(+(comments.name: Liu comments.name: The base ) +comments.age:[32 TO 32]) #MatchNoDocsQuery("") #DocValuesFieldExistsQuery [field=_primary_term]
GET /joining_query_nested_test/_mapping/_validate/query?explain
{
"query": {
"bool": {
"must": [
{
"match": {
"comments.name": " Liu Ji "
}
},
{
"match": {
"comments.age": 32
}
}
]
}
}
}
## Inquire about first=Alice and last=Smith
##hit: 0 In line with expectations
GET joining_query_nested_test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"user.first": "Alice" }},
{
"match": {
"user.last": "Smith" }}
]
}
}
}
## Inquire about name=blue,count>4 The record of
## hit:1
GET /joining_query_object_test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"obj1.name": "blue" } },
{
"range": {
"obj1.count": {
"gt": 4 } } }
]
}
}
}
(2) Inquire about + polymerization
## Inquire about name=blue,count>4 The record of , And aggregate to find avg
## hit:1
GET /joining_query_nested_test/_search
{
"query": {
"nested": {
"path": "obj1",
"query": {
"bool": {
"must": [
{
"match": {
"obj1.name": "blue" } },
{
"range": {
"obj1.count": {
"gt": 4 } } }
]
}
},
"score_mode": "avg"
}
}
}
(3) Multi-level nested queries Multi level nested query
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"
}
]
}
}
## Use multi-level nested queries based on make and model Field matches document
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" } }
]
}
}
}
}
}
}
}
3.has_child and has_parent
## There can be a connection field relationship between documents in a single index .
## Insert subdocument note : The child document must be on the same slice as the parent document , Parent document , When a route exists , Inserting a child document must also fill in the same route as the parent document .
## When querying documents, pay attention to :
##has_child The query returns the parent document whose child document matches the specified query ,
##has_parent The query returns the child document whose parent document matches the specified query .
Parent/child VS nested
Father - Child relationship document In essence, it is similar to nested model : Allows you to associate one object entity with another .
(1)nested objects , All objects are in the same document , High read performance
(2)Parent/child, Both parent and child objects are completely independent documents , Parent child updates do not affect each other
- The scope of influence when modifying the index ( scene )
- (1)nested, When you update a parent-child document, you need to update the entire document
- (2)Parent/child, establish , When modifying or deleting subdocuments , Does not affect the parent document or other child documents , But in order to maintain join The relationship between , Need to occupy part of the memory , Poor read performance
## Set the parent-child structure mapping ,join + relations
PUT /joining_query_child_test
{
"mappings": {
"properties": {
"my-join-field": {
"type": "join",
"relations": {
"parent": "child"
}
},
"tag": {
"type": "keyword"
}
}
}
}
## Use has_child Inquire about
GET /joining_query_child_test/_search
{
"query": {
"has_child": {
"type": "child",
"query": {
"match_all": {
}
},
"max_children": 10,
"min_children": 2,
"score_mode": "min"
}
}
}
PUT /joining_query_father_test
{
"mappings": {
"father":{
"properties": {
"id":{
"type": "integer"
},
"name":{
"type": "keyword"
}
}
},
"childers":{
"_parent": {
"type": "father"
},
"properties": {
"age":{
"type": "integer"
},
"name":{
"type": "keyword"
}
}
}
}
}
PUT three_tree_index
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "keyword"
},
"my_join_field": {
"type": "join",
"relations": {
"user_base": "article",
"article": "vote"
}
},
"stars": {
"type": "short"
},
"article_desc": {
"type": "text"
}
}
}
}
# Insert 6 Data
PUT three_tree_index/_doc/1?routing=1&refresh
{
"name":"xiaoming",
"age":29,
"my_join_field":"user_base"
}
PUT three_tree_index/_doc/2?routing=2&refresh
{
"name":"xiaohong",
"age":32,
"my_join_field":"user_base"
}
PUT three_tree_index/_doc/3?routing=1&refresh
{
"article_desc":"xiaoming,article_desc_1",
"my_join_field":{
"name":"article",
"parent":"1"
}
}
PUT three_tree_index/_doc/4?routing=2&refresh
{
"article_desc":"xiaohong,article_desc_1",
"my_join_field":{
"name":"article",
"parent":"2"
}
}
PUT three_tree_index/_doc/5?routing=1&refresh
{
"stars":5,
"my_join_field":{
"name":"vote",
"parent":"3"
}
}
PUT three_tree_index/_doc/6?routing=2&refresh
{
"stars":3,
"my_join_field":{
"name":"vote",
"parent":"4"
}
}
## three_tree_index
## hits : 6
GET /three_tree_index/_search
## Query the content of the article “xiaoming” User information for
## By inquiring has_child Query to parent Information
GET three_tree_index/_search
{
"query": {
"has_child": {
"type": "article",
"query": {
"match": {
"article_desc": "xiaoming"
}
}
}
}
}
## You can do it by adding "inner_hits":{
}, Return both parent and child documents
##_source: Father
##inner_hits: Son
GET three_tree_index/_search
{
"query": {
"has_child": {
"type": "article",
"query": {
"match": {
"article_desc": "xiaoming"
}
},
"inner_hits": {
}
}
}
}
## Query the users corresponding to the articles with five-star reviews ( Nested two layers has_child Inquire about )
GET three_tree_index/_search
{
"query": {
"bool": {
"must": [
{
"has_child": {
"type": "article",
"query": {
"has_child": {
"type": "vote",
"query": {
"bool": {
"should": [
{
"term": {
"stars": 5
}
}
]
}
}
}
}
}
}
]
}
}
}
##has_parent It's just the opposite , For example, I want to search for all that contain “xiaoming” The average star of the article
GET three_tree_index/_search
{
"query": {
"has_parent": {
"parent_type": "article",
"query": {
"bool": {
"should": [
{
"match": {
"article_desc": "xiaoming"
}
}
]
}
},
"inner_hits": {
}
}
},
"aggs": {
"avg_star": {
"avg": {
"field": "stars"
}
}
}
}
## Use has _ father Inquire about , The index must contain a join field mapping
DELETE joining_parent_test_02
PUT /joining_parent_test_02
{
"mappings": {
"properties": {
"my-join-field": {
"type": "join",
"relations": {
"parent": "child"
}
},
"tag": {
"type": "keyword"
}
}
}
}
GET /joining_parent_test_02/_search
{
"query": {
"has_parent": {
"parent_type": "parent",
"query": {
"term": {
"tag": {
"value": "Elasticsearch"
}
}
}
}
}
}
##Parent ID query Parental identity inquiry ===================================================================================
##1.Create an index with a join field mapping.join + relations
DELETE my-index-000001
PUT /my-index-000001
{
"mappings": {
"properties": {
"my-join-field": {
"type": "join",
"relations": {
"my-parent": "my-child"
}
}
}
}
}
##2. Parent document index
PUT /my-index-000001/_doc/1?refresh
{
"text": "This is a parent document.",
"my-join-field": "my-parent"
}
##3. Sub document index
PUT /my-index-000001/_doc/2?routing=1&refresh
{
"text": "This is a child document.",
"my-join-field": {
"name": "my-child",
"parent": "1"
}
}
##4. Search returns ID by 1 The child document of the parent document of .
## hit :1
GET /my-index-000001/_search
{
"query": {
"parent_id": {
"type": "my-child",
"id": "1"
}
}
}
边栏推荐
- Individual problem solution of the 298th round of force deduction
- 【第 26 章 基于最小误差法和区域生长的医学影响分割系统--matlab深度学习实战GUI项目】
- Appium面试题
- 大厂英伟达面试题整理123
- NOIP 提高组 初赛 三、问题求解 习题集NOIP1995-NOIP2018
- Download links to components, frameworks and development tools commonly used by programmers
- Pyechart drawing word cloud
- 【随笔】昨天研究了一天 RN 生态的 Expo 的确牛逼,从开发构建到部署一条龙,很好使。
- Chapter 08 handwritten digit recognition based on knowledge base matlab deep learning application practice
- 【第 02 章 基于形态学的权重自适应图像去噪技术-全套系统MATLAB智能驾驶深度学习】
猜你喜欢

Shardingsphere-proxy-5.0.0 implementation of distributed hash modulo fragmentation (4)

Intranet learning notes (3)

测试用例设计方法——因果图法

【第 02 章 基于形态学的权重自适应图像去噪技术-全套系统MATLAB智能驾驶深度学习】

digital signal processing

【第 15 章 基于小波的图像压缩技术深度学习机器学习的图像处理应用matlab.】
![[chapter 07 face QR code recognition based on principal component analysis matlab deep learning practical case]](/img/cd/65a541e8d0fc7a49ee3ef1d266790a.png)
[chapter 07 face QR code recognition based on principal component analysis matlab deep learning practical case]

acwing 835. Trie字符串统计

Apache Doris real-time data analysis nanny level tutorial

【第 26 章 基于最小误差法和区域生长的医学影响分割系统--matlab深度学习实战GUI项目】
随机推荐
程序员常用的组件、框架、开发工具下载链接大全
Download links to components, frameworks and development tools commonly used by programmers
Seeking an anti association detection tool, online detection of browser fingerprint
Amazon evaluation browser, core knowledge points of Amazon evaluation risk control
LeetCode+ 46 - 50
初识Unity3D(项目结构、ProBuilder第三方插件)
第六届世界智能大会“云”端召开在即
【第 01 章 基于直方图优化的图像去雾技术-全套系统MATLAB智能驾驶深度学习】
acwing 835. Trie string statistics
【第 20 章 基于帧间差法进行视频目标检测--MATLAB软件深度学习应用】
Lianfa science and technology -- Introduction to Lianfa science and technology ++ attached
Function test - Introduction to MySQL database
第 19 章 基于语音识别的信号灯图像模拟控制技术
第 18 章 基于GUI搭建通用视频处理工具matlab应用GUI实现
Redis缓存异常及处理方案总结
Intranet learning notes (9)
Google multi user anti Association tool
1277_FreeRTOS中vTaskDelay的实现分析
Mathematical knowledge in the first round of noip preliminary round csp-j1 csp-s1 Sinorgchem (III)
[chapter 06 MATLAB realizes lung cancer diagnosis based on watershed segmentation]