索引映射如下:

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

查询排序一:

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

查询排序二:

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

 

posted on 2021-07-28 17:56  鸥海  阅读(304)  评论(0编辑  收藏  举报