zoukankan      html  css  js  c++  java
  • 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有个 换行符哦

    //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"
                }
            }
        }
    }
  • 相关阅读:
    Text Link Ads 注册[赚钱一]
    Linux文件系统中的链接
    C++虚函数和纯虚函数(1)
    Android init reading tips
    Android上GDB的使用
    What is prelink?
    Linux fork哪些被继承,哪些不被继承
    为什么x86 Linux程序起始地址是从0x08048000开始的?
    Android应用开发的插件化 模块化
    C++拷贝构造函数(深拷贝、浅拷贝)
  • 原文地址:https://www.cnblogs.com/LQBlog/p/11152314.html
Copyright © 2011-2022 走看看