简体   繁体   English

在Elasticsearch中按数组索引对搜索结果进行排序

[英]Sort search results by array index in Elasticsearch

I have records like that: 我有这样的记录:

// tags vary in order within array
{
    name : 'Karl',
    tags : [
        'java',
        'html',
        'javascript'
    ]
},
{
    name : 'John',
    tags : [
        'html',
        'java',
        'javascript'
    ]
}

I run a bool query on tags: 我对标签运行布尔查询:

"query" : {
    "bool" : {
        "must" : [
            {
                "term" : "java"
            }
        ]
    }
}

I want Karl to be the first search result, because 'java' has a lower index in his tags array. 我希望Karl是第一个搜索结果,因为'java'在他的标签数组中索引较低。 Unfortunately my query doesn't care about the index, overall results are ok, but order of results seems to be random. 不幸的是,我的查询不在乎索引,总体结果还可以,但是结果的顺序似乎是随机的。

How can I force Elasticsearch to consider the array index? 如何强制Elasticsearch考虑数组索引?

thx! 谢谢!

If you need this information, then you're going to have to store the index of each element within that element: 如果您需要此信息,则必须将每个元素的索引存储在该元素中:

{
    name : 'Karl',
    tags : [
        'java',
        'html',
        'javascript'
    ],
    index : 0
},
{
    name : 'John',
    tags : [
        'html',
        'java',
        'javascript'
    ],
    index : 1
}

Then you can sort on this index subfield: 然后,您可以对该索引子字段进行排序:

"query" : {
    "bool" : {
        "must" : [
            {
                "term" : "java"
            }
        ]
    },
    "sort" : [
        "arrayFieldName.index" : "asc"
    ]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM