gpt4 book ai didi

sorting - 在Elasticsearch 2.4中按 child 数排序 parent (包括无子女)

转载 作者:行者123 更新时间:2023-12-03 01:46:51 25 4
gpt4 key购买 nike

我试图按文档的子级数对结果进行排序,将没有子级的子级放在第一位(其次是order值)。

相关问题

  • Elasticsearch get all parents with no children
  • How to sort parents by number of children in Elasticsearch

  • 根据这些答案,我设法使用 bool查询对文档进行了排序,以选择没有文档的文档,并使用boost将其评分为0,并计算其他文档的子级数量。

    这在ES 5.3中可以正常工作,但是增强功能在ES 2.4(我需要使用)中似乎没有任何作用。而是使用 must_not的分数 bool

    是否有一种对no child进行计分以确保其返回0的替代方法?还是我可以确信 queryNorm在ES 2.4中将始终<1(似乎有风险,但这可能是无知...)?

    制图
    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"
    }
    }
    }
    }
    }

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

    说明

    在Boost 0的Elasticsearch 5.3中,我得到
    {
    "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": []
    }
    ]
    }
    ]
    }
    ]
    }
    }
    ]
    }
    }

    在带有提升1(修改查询提升)的ES 5.3中,我得到了
    {
    "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": []
    }
    ]
    }
    ]
    }
    ]
    }
    }
    ]
    }
    }

    在带有加速0或1(或其他任何值)的ES 2.4中,我得到了
    {
    "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.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"
    }
    }
    ]
    }

    关于sorting - 在Elasticsearch 2.4中按 child 数排序 parent (包括无子女),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43214141/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com