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"}
                  }
               }
            }
         }
      }
   ]
}

要实现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"}
                  }
               }
            }
         }
      }
   ]
}

版权声明:本文为linpxing1原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。