• ES7 Nested Sort Search


    ES的嵌套搜索查询 + 嵌套搜索排序实贱

    POST /_search
    {
       "query": {
          "nested": {
             "path": "parent",
             "query": {
                "bool": {
                    "must": {"range": {"parent.age": {"gte": 21}}},
                    "filter": {
                        "nested": {
                            "path": "parent.child",
                            "query": {"match": {"parent.child.name": "matt"}}
                        }
                    }
                }
             }
          }
       },
       "sort" : [
          {
             "parent.child.age" : {
                "mode" :  "min",
                "order" : "asc",
                "nested": {
                   "path": "parent",
                   "filter": {
                      "range": {"parent.age": {"gte": 21}}
                   },
                   "nested": {
                      "path": "parent.child",
                      "filter": {
                         "match": {"parent.child.name": "matt"}
                      }
                   }
                }
             }
          }
       ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    要实现Sort filter的多条件限制,如下
    即 sort nested field “fitler”:{} = “query”:{}里面的内容

    • filter
      • A filter that the inner objects inside the nested path should match
        with in order for its field values to be taken into account by
        sorting. Common case is to repeat the query / filter inside the
        nested filter or query. By default no nested_filter is active.
    POST /_search
    {
       "query": {
          "nested": {
             "path": "parent",
             "query": {
                "bool": {
                    "must": {"range": {"parent.age": {"gte": 21}}},
                    "filter": {
                        "nested": {
                            "path": "parent.child",
                            "query": {"match": {"parent.child.name": "matt"}}
                        }
                    }
                }
             }
          }
       },
       "sort" : [
          {
             "parent.child.age" : {
                "mode" :  "min",
                "order" : "asc",
                "nested": {
                   "path": "parent",
                   "filter": {
    			            "bool": {
    			                "must": {"range": {"parent.age": {"gte": 21}}},
    			                "filter": {
    			                    "nested": {
    			                        "path": "parent.child",
    			                        "query": {"match": {"parent.child.name": "matt"}}
    			                    }
    			                }
    			            }
    			         }
    			      },
                   "nested": {
                      "path": "parent.child",
                      "filter": {
                         "match": {"parent.child.name": "matt"}
                      }
                   }
                }
             }
          }
       ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
  • 相关阅读:
    Facebook账号运营技巧
    nginx学习:配置文件详解,负载均衡三种算法学习,上接nginx实操篇
    主机jvisualvm连接到tomcat服务器查看jvm状态
    linux内核初始化成功后是如何过渡到android初始化的
    CSRF和SSRF有什么不同?
    【无标题】
    计算机毕业设计之java+javaweb的超市库存管理系统
    QT静态链接库
    【力扣每日一题】535. TinyURL 的加密与解密
    Android VSYNC发展历程
  • 原文地址:https://blog.csdn.net/linpxing1/article/details/126758673