How do I sort the search results according to the number of items in ElasticSearch?

余生颓废 提交于 2020-01-11 06:42:48

问题


Let's say that I store documents like this in ElasticSearch:

{
    'name':'user name', 
    'age':43, 
    'location':'CA, USA', 
    'bio':'into java, scala, python ..etc.', 
    'tags':['java','scala','python','django','lift']
}

And let's say that I search using location=CA, how can I sort the results according to the number of the items in 'tags'?

I would like to list the people with the most number of tag in the first page.


回答1:


You can do it indexing an additional field which contains the number of tags, on which you can then easily sort your results. Otherwise, if you are willing to pay a little performance cost at query time there's a nice solution that doesn't require to reindex your data: you can sort based on a script like this:

{
    "query" : {
        "match_all" : {}
    },
    "sort" : {
        "_script" : { 
            "script" : "doc['tags'].values.length",
            "type" : "number",
            "order" : "asc"
        }
    }
}

As you can read from the script based sorting section:

Note, it is recommended, for single custom based script based sorting, to use custom_score query instead as sorting based on score is faster.

That means that it'd be better to use a custom score query to influence your score, and then sort by score, like this:

{
    "query" : {
        "custom_score" : {
            "query" : {
                "match_all" : {}
            },
            "script" : "_score * doc['tags'].values.length"
        }
    }
}


来源:https://stackoverflow.com/questions/12733351/how-do-i-sort-the-search-results-according-to-the-number-of-items-in-elasticsear

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!