使用NodeJS Client在ElasticSearch中排序

时间:2017-05-01 01:45:02

标签: node.js sorting elasticsearch

我正在尝试从基于字段的查询中对结果进行排序,并且由于某种原因,它从未正确排序。以下是查询示例:

  starting request {
   "method": "POST",
   "path": "/index/library/_search",
   "query": {
      "from": 0,
      "size": 10,
      "sort": "[{\"title\":{\"order\":\"asc\"}}]",
      "q": "\"rules\""
     }
   }

生成此网址:

http://localhost:9200/index/library/_search?from=0&size=10&sort=%5B%7B%22title%22%3A%7B%22order%22%3A%22asc%22%7D%7D%5D&q=%22rules%22

我已经尝试了许多不同的结构排序方式,但无论出于何种原因,它都不会按字母顺序返回。

我是否错误地构建了排序部分?或者我错过了什么?

编辑:

    search(type, query, options) {
    let params = {
        index: this.indexName,
        type: type,
        from: options.from || 0,
        size: options.size || 500,
        sort: options.sort
    };


    if (JSON.parse(query).hasOwnProperty('query')) {
        params.body = query;
    } else {
        params.q = query;
    }

    return this.client.search(params);
}

查询:

Elasticsearch DEBUG: 2017-05-04T15:55:00Z
starting request {
  "method": "POST",
  "path": "/index/library/_search",
  "query": {
    "from": 0,
    "size": 10,
    "sort": "title : asc",
    "q": "rules"
  }
}


Elasticsearch TRACE: 2017-05-04T15:55:00Z
 -> POST http://localhost:9200/index/library/_search?
  from=0&size=10&sort=title%20%3A%20asc&q=rules

1 个答案:

答案 0 :(得分:0)

请改为尝试:

  starting request {
   "method": "POST",
   "path": "/index/library/_search",
   "query": {
      "from": 0,
      "size": 10,
      "sort": "title:asc",        <---- change this
      "q": "rules"
     }
   }

这将生成这个应该更正确的URL

http://localhost:9200/index/library/_search?from=0&size=10&sort=title%3Aasc&q=rules