在Elasticsearch 2.4中按父项数量(包括无)对父项进行排序

时间:2017-04-04 17:44:20

标签: sorting elasticsearch

我正在尝试按照文档所具有的子项数对结果进行排序,将那些没有子项的子项放在第一位(其次是order值)。

相关问题

根据这些答案,我设法使用bool查询对文档进行排序,以选择没有文档的文档,并使用提升对其进行评分,并计算其他文档的子项数。

这在ES 5.3中运行良好,但是在ES 2.4(我需要使用)中,增强似乎没有任何影响;相反,它使用must_not bool的分数。

是否有其他方法可以对无子案例进行评分以确保其返回0?或者我可以确信queryNorm始终是< PUT parent_child { "mappings": { "parent_doc": { "dynamic": "strict", "properties": { "parent_id": { "type": "string", "index": "not_analyzed" }, "age": { "type": "long" }, "relationship_status": { "type": "string", "index": "not_analyzed" }, "order": { "type": "long" } } }, "child_doc": { "dynamic": "strict", "_parent": { "type": "parent_doc" }, "properties": { "parent_id": { "type": "string", "index": "not_analyzed" }, "child_id": { "type": "string", "index": "not_analyzed" }, "accommodation": { "type": "string", "index": "not_analyzed" } } } } } ES 2.4中的1(似乎有风险,但可能是无知......)?

映射

PUT parent_child/parent_doc/0
{
    "parent_id": "p0",
    "age": 20,
    "relationship_status": "single",
    "order": 0
}

PUT parent_child/parent_doc/1
{
    "parent_id": "p1",
    "age": 33,
    "relationship_status": "married",
    "order": 1
}

PUT parent_child/child_doc/0?parent=0
{
    "parent_id": "p0",
    "child_id": "c0",
    "accommodation": "rent"
}

数据

GET parent_child/parent_doc/_search?explain=true
{
    "query": {
        "bool": {
            "should": [
                {
                    "has_child": {
                        "type": "child_doc",
                        "score_mode": "sum",
                        "query": {
                            "match_all": {}
                        }
                    }
                },
                {
                  "bool": {
                    "boost": 0,
                    "constant_score" : {
                        "must_not": [
                            {
                                "has_child": {
                                    "type": "child_doc",
                                    "query": {
                                      "match_all": {}
                                    }
                                }
                            }
                        ]
                    }
                  }
                }
            ],
            "minimum_should_match": 1,
            "disable_coord": true
        }
    },
    "sort" : [
        {"_score" : {"order" : "asc"}},
        {"order" : {"order" : "desc"}}
   ]
}

查询

{
  "took": 58,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_shard": "[parent_child][3]",
        "_node": "k-pLeatPSsaA8OXrcuLHqg",
        "_index": "parent_child",
        "_type": "parent_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "parent_id": "p1",
          "age": 33,
          "relationship_status": "married",
          "order": 1
        },
        "sort": [
          0,
          1
        ],
        "_explanation": {
          "value": 0,
          "description": "sum of:",
          "details": [
            {
              "value": 0,
              "description": "sum of:",
              "details": [
                {
                  "value": 0,
                  "description": "ConstantScore(-GlobalOrdinalsQuery{joinField=_parent#parent_doc} +*:*), product of:",
                  "details": [
                    {
                      "value": 0,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 1,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            },
            {
              "value": 0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 1,
                  "description": "*:*, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 1,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      {
        "_shard": "[parent_child][0]",
        "_node": "k-pLeatPSsaA8OXrcuLHqg",
        "_index": "parent_child",
        "_type": "parent_doc",
        "_id": "0",
        "_score": 1,
        "_source": {
          "parent_id": "p0",
          "age": 20,
          "relationship_status": "single",
          "order": 0
        },
        "sort": [
          1,
          0
        ],
        "_explanation": {
          "value": 1,
          "description": "sum of:",
          "details": [
            {
              "value": 1,
              "description": "sum of:",
              "details": [
                {
                  "value": 1,
                  "description": "A match, join value 0",
                  "details": []
                }
              ]
            },
            {
              "value": 0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 1,
                  "description": "_type:parent_doc, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 1,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

说明

在Elasticsearch 5.3中,使用boost 0,我得到了

{
  "took": 7,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_shard": "[parent_child][3]",
        "_node": "6WApYVoYSSaj3BeMwO1z_Q",
        "_index": "parent_child",
        "_type": "parent_doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "parent_id": "p1",
          "age": 33,
          "relationship_status": "married",
          "order": 1
        },
        "sort": [
          1,
          1
        ],
        "_explanation": {
          "value": 1,
          "description": "sum of:",
          "details": [
            {
              "value": 1,
              "description": "sum of:",
              "details": [
                {
                  "value": 1,
                  "description": "sum of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "*:*, product of:",
                      "details": [
                        {
                          "value": 1,
                          "description": "boost",
                          "details": []
                        },
                        {
                          "value": 1,
                          "description": "queryNorm",
                          "details": []
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value": 0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 1,
                  "description": "*:*, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 1,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      {
        "_shard": "[parent_child][0]",
        "_node": "6WApYVoYSSaj3BeMwO1z_Q",
        "_index": "parent_child",
        "_type": "parent_doc",
        "_id": "0",
        "_score": 1,
        "_source": {
          "parent_id": "p0",
          "age": 20,
          "relationship_status": "single",
          "order": 0
        },
        "sort": [
          1,
          0
        ],
        "_explanation": {
          "value": 1,
          "description": "sum of:",
          "details": [
            {
              "value": 1,
              "description": "sum of:",
              "details": [
                {
                  "value": 1,
                  "description": "A match, join value 0",
                  "details": []
                }
              ]
            },
            {
              "value": 0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 1,
                  "description": "_type:parent_doc, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 1,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

在ES 5.3中使用boost 1(修改查询提升),我得到了

{
  "took": 63,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": null,
    "hits": [
      {
        "_shard": 3,
        "_node": "prwYMacQRLCOi_qDoVSmpA",
        "_index": "parent_child",
        "_type": "parent_doc",
        "_id": "1",
        "_score": 0.70710677,
        "_source": {
          "parent_id": "p1",
          "age": 33,
          "relationship_status": "married",
          "order": 1
        },
        "sort": [
          0.70710677,
          1
        ],
        "_explanation": {
          "value": 0.70710677,
          "description": "sum of:",
          "details": [
            {
              "value": 0.70710677,
              "description": "sum of:",
              "details": [
                {
                  "value": 0.70710677,
                  "description": "sum of:",
                  "details": [
                    {
                      "value": 0.70710677,
                      "description": "*:*, product of:",
                      "details": [
                        {
                          "value": 1,
                          "description": "boost",
                          "details": []
                        },
                        {
                          "value": 0.70710677,
                          "description": "queryNorm",
                          "details": []
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value": 0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 0.70710677,
                  "description": "_type:parent_doc, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 0.70710677,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      },
      {
        "_shard": 0,
        "_node": "prwYMacQRLCOi_qDoVSmpA",
        "_index": "parent_child",
        "_type": "parent_doc",
        "_id": "0",
        "_score": 1,
        "_source": {
          "parent_id": "p0",
          "age": 20,
          "relationship_status": "single",
          "order": 0
        },
        "sort": [
          1,
          0
        ],
        "_explanation": {
          "value": 1,
          "description": "sum of:",
          "details": [
            {
              "value": 1,
              "description": "sum of:",
              "details": [
                {
                  "value": 1,
                  "description": "A match, join value 0",
                  "details": []
                }
              ]
            },
            {
              "value": 0,
              "description": "match on required clause, product of:",
              "details": [
                {
                  "value": 0,
                  "description": "# clause",
                  "details": []
                },
                {
                  "value": 0.70710677,
                  "description": "_type:parent_doc, product of:",
                  "details": [
                    {
                      "value": 1,
                      "description": "boost",
                      "details": []
                    },
                    {
                      "value": 0.70710677,
                      "description": "queryNorm",
                      "details": []
                    }
                  ]
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

在ES 2.4中使用提升0或1(或其他任何东西),我得到了

transfrom()/process()

1 个答案:

答案 0 :(得分:1)

这是在ES 2.4.3中适用于我的查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "has_child": {
            "type": "child_doc",
            "score_mode": "sum",
            "query": {
              "match_all": {}
            }
          }
        },
        {
          "constant_score": {
            "boost": 0,
            "query": {
              "bool": {
                "must_not": [
                  {
                    "has_child": {
                      "type": "child_doc",
                      "query": {
                        "match_all": {}
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      ],
      "minimum_should_match": 1,
      "disable_coord": true
    }
  },
  "sort": [
    {
      "_score": {
        "order": "asc"
      }
    },
    {
      "order": {
        "order": "desc"
      }
    }
  ]
}