elasticsearch 父子文档(十一)

说明

需求 一个产品多个区域销售 每个区域有自己的价格,

方式1冗余行,a 产品分别在  area1 area2 area3区域销售 a产品就会生成3条产品数据 搜索id去重就行了,但是问题就是 聚合去重不支持 获得count 所以不能分页 只有选择假分页

方式2:父子关系  用于描述一对多管理  这里我采用方式2  产品为父文档 区域产品信息为子文档

6.*以下版本

创建索引

//put http://192.168.20.4:9200/lq_product_test
{
"mappings": { "product": {}, //父文档 type name "areaProductInfo": { //子文档type name "_parent": { "type": "product" //指定子文档的父文档typename } } } }

添加测试数据

1.插入文档

注意最后一个js有个\n换行符哦

//post http://192.168.20.4:9200/lq_product_test/product/_bulk
{ "index": { "_id": "1" }}
{"productId":1, "name": "产品1","sumSellCount":10}
{ "index": { "_id": "2" }}
{"productId":2, "name": "产品2","sumSellCount":11}
{ "index": { "_id": "3" }}
{"productId":3, "name": "产品3","sumSellCount":12}

2.插入子文档

http://192.168.20.4:9200/lq_product_test/areaProductInfo/1?parent=1

{
"id": 1,
"areaCode": 2,
"price": 20
}

http://192.168.20.4:9200/lq_product_test/areaProductInfo/2?parent=2

{
"id": 2,
"areaCode": 2,
"price": 30
}

get:http://192.168.20.4:9200/lq_product_test/_search

子文档查询

get:http://192.168.20.4:9200/lq_product_test/_search

包含有product父文档的所有子文档

{
  "query":{
      "has_parent":{
          "parent_type":"product",
          "query":{
              "match_all":{}
          }
      }
  }
}

父文档增加条件

{
  "query":{
      "has_parent":{
          "parent_type":"product",
          "query":{
                  "term":{"productId":1}
              }
          }
      }
  }

父子文档都增加条件

{
    "query": {
        "bool": {
            "must": [{
                "query": {
                    "term": {
                        "areaCode": 2
                    }
                }
            }, {
                "has_parent": {
                    "parent_type": "product",
                    "query": {
                        "term": {
                            "productId": 1
                        }
                    }
                }
            }]

        }
    }
}

 

function_score排序

{
    "query": {
        "function_score": {
            "query": {
                "has_parent": {
                    "parent_type": "product",
                    "query": {
                        "match_all": {}
                    }
                }
            },
            "script_score": {
                "script": "_score *sumSellCount"
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/LQBlog/p/11152314.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值