elasticsearch - Elasticsearch如何将选定的行项目放在结果顶部

标签 elasticsearch kibana

如何将所选项目放在结果之上?例如,我想按名称排序后在顶部分别设置ID 3和4。与查询中的SQL Order类似。

假设我有一个索引调用人员。
GET人员/ _搜索

{
hits: [
  "_source" : {
     "id" : 1,
      "name" : "Paul"
  },
  "_source" : {
     "id" : 2,
      "name" : “Mike”
  },
  "_source" : {
     "id" : 3,
      "name" : “Alex”
  },
  "_source" : {
     "id" : 4,
      "name" : “Susan”
  }

]
}

我当前的搜索查询:
{  "track_total_hits": true,
"from" : 0,
"size" : 30,
"query":{
  "query_string":
  {
    "query": " some query matchin"
  }
},    "sort" :
[
  {"nae" : {"order" : "desc"}}
]    }}

最佳答案

您可以使用自定义脚本对文档进行排序。将有两种。

  • 自定义脚本:对选定值进行排序
  • 按名称排序:基于名称排序
  • {
      "sort": [
        {
          "_script": {
                "type": "number",
                "script": {
                    "lang": "painless",
                    "inline": "if(params.names.containsKey(doc['name.keyword'].value)) { return params.names[doc['name.keyword'].value];} return 100;",
                    "params": {
                        "names": {
                            "Susan": 0,
                            "Alex": 1
                        }
                    }
                },
                "order": "asc"
            }
        },
        {
          "name.keyword": {
            "order": "desc"
          }
        }
      ]
    }
    

    关于elasticsearch - Elasticsearch如何将选定的行项目放在结果顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61741282/

    相关文章:

    python - ES索引与python-结合常规映射设置(含)。 index.mapping.ignore_malformed

    elasticsearch - grok 不读带连字符的单词

    python - elasticsearch 聚合哈希值的唯一条目并查找所有标签

    elasticsearch - Elasticsearch:通过时间戳过滤聚合

    logging - 如何聚合来自多个 Elasticsearch 索引的 Kibana 信息?

    java - Elasticsearch : "failed to get node info for {IP}" and "noNodeAvailableException" in service log

    apache - 如何知道每日索引文件的Apache Solr状态

    elasticsearch - 我们在麋鹿堆栈中应用sigma规则的地方

    internet-explorer - Kibana (https) 无法在 Internet Explorer 中访问 ElasticSearch (http)

    elasticsearch - 使用为其他索引模式(相同数据)创建的Visualize Kibana?