ElasticSearch按值排序 [英] ElasticSearch sort by value

查看:546
本文介绍了ElasticSearch按值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有ElasticSearch 5,我想根据字段值进行排序.想象一下,例如具有类别的文档可能具有科幻,戏剧,喜剧等价值的类型,在搜索时,我想订购值,以便首先出现喜剧,然后是科幻和戏剧.然后,我当然会按照其他条件在小组内订购.有人可以指出我该怎么做吗?

I have ElasticSearch 5 and I would like to do sorting based on field value. Imagine having document with category e.g. genre which could have values like sci-fi, drama, comedy and while doing search I would like to order values so that first comes comedies then sci-fi and drama at last. Then of course I will order within groups by other criteria. Could somebody point me to how do this ?

推荐答案

使用手动排序进行Elasticsearch排序

这在elasticsearch中是可能的,您可以在其中根据字段的特定值分配顺序.

Elasticsearch Sort Using Manual Ordering

This is possible in elasticsearch where you can assign order based on particular values of a field.

我已经使用基于脚本的排序,它使用无痛脚本.您可以参考我提到的链接,以了解更多有关以下查询的内容,这些内容足以满足您的需求.

I've implemented what you are looking for using script based sorting which makes use of painless script. You can refer to the links I've mentioned to know more on these for below query would suffice what you are looking for.

还要假设您使用以下映射来将genremovie作为关键字.

Also assuming you have genre and movie as keyword with the below mapping.

PUT sampleindex
{
  "mappings": {
    "_doc": {
      "properties": {
        "genre": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        },
        "movie": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        }
      }
    }
  }
}

您可以利用以下查询来获取所需的内容

You can make use of below query to get what you are looking for

GET sampleindex/_search
{
  "query": {
    "match_all": {}
  },
  "sort": [{
        "_script": {
            "type": "number",
            "script": {
                "lang": "painless",
                "inline": "if(params.scores.containsKey(doc['genre.raw'].value)) { return params.scores[doc['genre.raw'].value];} return 100000;",
                "params": {
                    "scores": {
                        "comedy": 0,
                        "sci-fi": 1,
                        "drama": 2
                    }
                }
            },
            "order": "asc"
        }
    },
    { "movie.raw": { "order": "asc"}
    }]
}

请注意,我已经包括了genremovie的排序.基本上,排序会在genre上进行,然后再发布,每种类型都将基于movie进行排序.

Note that I've included sort for both genre and movie. Basically sort would happen on genre and post that further it would sort based on movie for each genre.

希望它会有所帮助.

这篇关于ElasticSearch按值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆