ES父子关系创建、新增、查询、更新、删除

父子文档的特点

1. 父/子文档是完全独立的。

2. 父文档更新不会影响子文档。

3. 子文档更新不会影响父文档或者其它子文档。

操作如下:

  • 创建索引

PUT auto_parts-whole-network-index

{
	"settings": {
		"number_of_shards": 8,
		"number_of_replicas": 1,
		"codec": "best_compression",
		"analysis": {
			"normalizer": {
				"lowercase": {
					"type": "custom",
					"filter": ["lowercase"]
				}
			},
			"analyzer": {
				"ngram_analyzer": {
					"type": "custom",
					"tokenizer": "ngram_tokenizer",
					"filter": [
						"lowercase"
					]
				},
				"productCode_search_analyzer": {
					"type": "custom",
					"tokenizer": "standard",
					"filter": [
						"lowercase"
					]
				}
			},
			"tokenizer": {
				"ngram_tokenizer": {
					"type": "ngram",
					"min_gram": 3,
					"max_gram": 20
				}
			}
		}
	},
	"mappings": {
		"auto_parts-whole-network-type": {
			"dynamic": "true",
			"_routing": {
				"required": false
			},
			"_all": {
				"enabled": false
			},
			"properties": {
				"enterprise_product": {
					"type": "join",
					"relations": {
						"enterprise": "product"
					}
				},
				"enterpriseName": {
					"type": "keyword"
				},
				"cityName": {
					"type": "keyword"
				},
				"qqList": {
					"type": "keyword"
				},
				"contactPhoneList": {
					"type": "keyword"
				},
				"inquiryNum": {
					"type": "double"
				},
				"pkid": {
					"type": "keyword"
				},
				"productId": {
					"type": "keyword"
				},
				"productCode": {
					"type": "text",
					"analyzer": "ngram_analyzer",
					"search_analyzer": "productCode_search_analyzer"
				},
				"brandName": {
					"type": "keyword"
				},
				"productName": {
					"type": "keyword"
				},
				"quantity": {
					"type": "double"
				},
				"salesPrice": {
					"type": "double"
				},
				"status": {
					"type": "keyword"
				},
				"source": {
					"type": "keyword"
				},
				"property1": {
					"type": "keyword"
				},
				"property50": {
					"type": "keyword",
					"normalizer": "lowercase"
				},
				"lastModifiedTime": {
					"type": "date",
					"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
				}
			}
		}
	}
}
  • 创建别名

POST /_aliases

{
	"actions": [{
		"add": {
			"index": "auto_parts-whole-network-index",
			"alias": "auto_parts-whole-network-alias"
		}
	}]
}
  • 添加字段

PUT auto_parts-whole-network-index/_mapping/auto_parts-whole-network-type

{
	"properties": {
		"enterpriseId": {
			"type": "keyword"
		},
		"companyName": {
			"type": "keyword"
		}
	}
}
  • 新增数据

##添加父文本 ID:10000000001

PUT auto_parts-whole-network-index/_doc/10000000001
{
  "companyName": "企业名称",
  "enterprise_product": {
    "name": "enterprise" 
  }
}


##添加子文本,父文本ID:10000000001 子文本ID:20000000001
PUT auto_parts-whole-network-index/_doc/20000000001?routing=10000000001
{
  "productName": "商品",
  "enterprise_product": {
    "name": "product", 
    "parent": "10000000001" 
  }
}

  • 查询enterpriseId为空的数据

(返回父文档及子文档10条)

POST auto_parts-whole-network-alias/_search

{
	"from": 0,
	"size": 10,
	"query": {
		"has_child": {
			"query": {
				"bool": {
					"must_not": {
						"exists": {
							"field": "enterpriseId"
						}
					},
					"adjust_pure_negative": true,
					"boost": 1
				}
			},
			"type": "product",
			"inner_hits": {
				"size": 10
			}
		}
	}
}
  •  根据企业ID查询(返回子数据)

 POST auto_parts-whole-network-alias/_search

{
  "query": {
    "has_parent": {
      "parent_type": "enterprise",
      "query": {
        "term": {
          "enterpriseId": {
            "value": "236848"
          }
        }
      }
    }
  }
}
  • 查询enterpriseId为空的数据

返回所有文档

POST auto_parts-whole-network-alias/_search

{
  "from": 0,
  "size": 10,
  "query": {
    "bool": {
      "must_not": {
        "exists": {
          "field": "enterpriseId"
        }
      },
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}
  • 根据条件删除数据

POST auto_parts-whole-network-alias/_delete_by_query

{
  "query": {
    "bool": {
      "must": [
       {
         "terms": {
           "pkid": ["23C3FE6C14DCC35C3D152461ED99FCB2"]
         }
       }
      ]
    }
  }
}
  • 根据条件更新数据

POST auto_parts-whole-network-index/_update_by_query

{
    "script": {
        "inline": "ctx._source['contactPhoneList']='13415155455';"
    },
    "query": {
        "bool": {
            "must": [
                {
                    "term": {
                        "pkid": "02C92154C3E5EA43054A3681DE938533"
                    }
                }
            ]
        }
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值