javascript - Elastic - 优先排序值

标签 javascript node.js elasticsearch restify

所以,我有 1 个字段,我们可以将其称为 timeInt。类型值是整数。该值是从0开始计数的。我想按优先级条件对其进行排序。 0/1 将放置在顶部,然后将较大的值放置在值 0/1 之后。例如:

0 1 100 99 98 等等...

我不知道如何实现这一点,我已经在谷歌上搜索,但仍然卡住了,我只是尝试像这样的查询排序:

排序:[{timeInt:{'order':'asc'}}]

但它只是升序排列

let newQuery = {
      index: `${config.get('/indexElastic').needAction}`,
      type: 'notification',
      body: {
        from: from, size: size,
        query: match_all: {},
        sort:[{timeInt:{'order':'asc'}}]
      }
    };

预期结果:

0 1 100 99 98 等等...

当前结果:

0 1 2 3 4 等等...

最佳答案

您可以申请boostshould 中的值 01然后通过包含 _score 字段来修改排序查询,如下所示。

另外,看看您的预期结果,timeInt 的排序顺序将是 desc 而不是 asc

POST <your_index_name>/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_all": {}
        },
        {
          "match": {
            "timeInt": {
              "query": 0,
              "boost": 3
            }
          }
        },
        {
          "match": {
            "timeInt": {
              "query": 1,
              "boost": 2
            }
          }
        }
      ]
    }
  },
  "sort": [
    { "_score": "desc"},
    {
      "timeInt": {
        "order": "desc"
      }
    }
  ]
}

以下是结果的显示方式:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 5,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "myintindex",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 4.0,
        "_source" : {
          "timeInt" : 0
        },
        "sort" : [
          4.0,
          0
        ]
      },
      {
        "_index" : "myintindex",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 3.0,
        "_source" : {
          "timeInt" : 1
        },
        "sort" : [
          3.0,
          1
        ]
      },
      {
        "_index" : "myintindex",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "timeInt" : 99
        },
        "sort" : [
          1.0,
          99
        ]
      },
      {
        "_index" : "myintindex",
        "_type" : "_doc",
        "_id" : "4",
        "_score" : 1.0,
        "_source" : {
          "timeInt" : 98
        },
        "sort" : [
          1.0,
          98
        ]
      },
      {
        "_index" : "myintindex",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 1.0,
        "_source" : {
          "timeInt" : 97
        },
        "sort" : [
          1.0,
          97
        ]
      }
    ]
  }
}

注意:所有其他文档的 _score 均为 1,因为 match_all询问。

请告诉我这是否有帮助!

关于javascript - Elastic - 优先排序值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57518283/

相关文章:

javascript - 为什么 Mocha 测试没有运行? - Node .js

javascript - ssl kraken-js 的 app.json 配置

javascript - 无法使用 webpack 将 NODE_ENV 设置为生产环境

amazon-web-services - CloudWatch 警报 : how to describe the alarm with metrics by ElasticSearch node, 的 AWS CloudFormation 模板不是整个 ElasticSearch 集群

elasticsearch - 在ElasticSearch中检索最大文档大小

php - 客户端调用 php 的 Elasticsearch 配置问题

javascript - 哪个位置更适合压缩图像?在浏览器中还是在服务器上?

javascript - 需要 Firefox ctypes 输出字符串参数的工作示例

javascript - 从索引位置向后查找字符串中的空格

javascript - sublime text 3如何选择打开哪个浏览器