如何通过子对象字段对对象进行排序?

时间:2019-03-15 12:12:56

标签: elasticsearch

仅在GroupModel.PeerOrder与某些ID匹配的情况下,如何才能按GroupModel.ParentGroupId对产品进行排序?

我的C#模型:

public class ProductModel
{
    public int Id { get; set; }
    public int Title { get; set; }
    public List<GroupModel> Groups { get; set; }
}

public class GroupModel
{
    public int Id { get; set; }
    public int Title { get; set; }
    public int ParentGroupId { get; set; }
    public int PeerOrder { get; set; }
}

2 个答案:

答案 0 :(得分:1)

使用Nested Query过滤匹配的“ GroupModel.ParentGroupId ”值,然后应用Nested Sort Query对结果进行“ GroupModel.PeerOrder ”排序。

根据文档:

嵌套查询:嵌套查询允许查询嵌套对象/文档(请参见嵌套映射)。对嵌套对象/文档执行查询,就好像它们被索引为单独的文档(它们在内部)一样,并导致根父文档(或父嵌套映射)。这是一个示例映射:

PUT /my_index
{
    "mappings": {
        "_doc" : {
            "properties" : {
                "obj1" : {
                    "type" : "nested"
                }
            }
        }
    }
}

GET /_search
{
    "query": {
        "nested" : {
            "path" : "obj1",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"obj1.name" : "blue"} },
                    { "range" : {"obj1.count" : {"gt" : 5}} }
                    ]
                }
            }
        }
    }
}

嵌套排序查询:即使该值存在于单独的嵌套文档中,也可以按嵌套字段的值进行排序。

GET /_search
{
  "query": {
    "nested": { 
      "path": "comments",
      "filter": {
        "range": {
          "comments.date": {
            "gte": "2014-10-01",
            "lt":  "2014-11-01"
          }
        }
      }
    }
  },
  "sort": {
    "comments.stars": { 
      "order": "asc",   
      "mode":  "min",   
      "nested_filter": { 
        "range": {
          "comments.date": {
            "gte": "2014-10-01",
            "lt":  "2014-11-01"
          }
        }
      }
    }
  }
}

答案 1 :(得分:1)

这就是我最终的结果(在@ydrall的帮助下-非常感谢!:>):

查询部分(对我们来说,没有第一部分就可以了)

"query": {
  "bool": {
        "must": [                
                {
                  "nested": {
                    "path": "groups", 
                    "query": {
                      "bool": {
                        "must": [ 
                          {
                            "match": {
                              "groups.parentGroupId": 3
                            }
                          }
                        ]
                      }
                    }
                  }
                }
    ]
  }
}

查询的排序部分:

"sort": [
{
    "groups.peerOrder": { 
      "order": "asc",
        "nested_path": "groups",   
      "nested_filter": { 
        "match": {
          "groups.parentGroupId": 3 
        }
      }
    }
 }

索引映射:

 "mappings": {
    "productmodel": {
      "properties": {
          "groups": {
          "type": "nested",
          "properties": {
            "id": {
              "type": "integer"
            },
            "parentGroupId": {
              "type": "integer"
            },
            "peerOrder": {
              "type": "integer"
            }
          }
        }
      }
    }
  }