排序子文档

时间:2018-06-14 11:21:22

标签: solr

在Solr中,如何按分数对子文档进行排序?在我测试时,似乎只能对文档进行排序,而不是在父文档中检索的子文档。

我有以下文件:

{
    "id": 0,
    "type": "parent",
    "name": "Arnold",
    "_childDocuments_": [
        {
            "id": 1,
            "type": "child",
            "field": "foo bar"
        },
        {
            "id": 2,
            "type": "child",
            "field": "foo baz"
        },
        {
            "id": 3,
            "type": "child",
            "field": "bar baz"
        },
        {
            "id": 4,
            "type": "child",
            "field": "foobar baz bar"
        }
    ]
}

现在我想按foo baz进行过滤。我正在使用:

q= {!parent which=type:parent}
fl= *, [child parentFilter=type:parent childFilter="field:foo OR field:baz"]
score= score desc

由于ID 2是foo baz,我希望将此视图作为第一个检索到的子文档,但我看到ID 1 foo bar是第一个,因为ID 1是第一个插入。

1 个答案:

答案 0 :(得分:1)

您可以使用[child] transformer

,而不是使用[subquery] transformer
q= {!parent which=type:parent}
fl= *, my_childs:[subquery]&my_childs.q=field:foo OR field:baz&my_childs.fl=*, score

结果:

"response": {
    "numFound": 1,
    "start": 0,
    "docs": [
        {
            "id":"0",
            "type": ["parent"],
            "name": ["Arnold"],
            "_version_": 1603334242311340032,
            "name_str": ["Arnold"],
            "type_str": ["parent"],
            "my_childs": {
                "numFound": 4,
                "start": 0,
                "docs":[
                    {
                        "id": "2",
                        "type": ["child"],
                        "field": ["foo baz"],
                        "field_str": ["foo baz"],
                        "_version_": 1603334242311340032,
                        "type_str": ["child"],
                        "score": 1.0998137
                    },
                    {
                        "id": "1",
                        "type": ["child"],
                        "field": ["foo bar"],
                        "field_str": ["foo bar"],
                        "_version_": 1603334242311340032,
                        "type_str": ["child"],
                        "score": 0.7261542
                    },
                    {
                        "id": "3",
                        "type": ["child"],
                        "field": ["bar baz"],
                        "field_str": ["bar baz"],
                        "_version_": 1603334242311340032,
                        "type_str": ["child"],
                        "score": 0.3736595
                    },
                    {
                        "id": "4",
                        "type": ["child"],
                        "field": ["foobar baz bar"],
                        "field_str": ["foobar baz bar"],
                        "_version_": 1603334242311340032,
                        "type_str": ["child"],
                        "score": 0.31387395
                    }
                ]
            }
        }
    ]
}