【问题标题】:elastic search sort not working弹性搜索排序不起作用
【发布时间】:2017-06-06 13:34:19
【问题描述】:

嗨,这是我的弹性搜索示例输出

"table":{  
   "data":[  
      {  
         "label":"First Label",
         "value":"10"
      },
      {  
         "label":"1st Label",
         "value":"9"
      }
   ],
   "details":"Examples set on MSRP, your actual payment may vary based on price set by dealer."
}

我希望按升序排序,通过列标签或通过值。

我尝试的搜索选项是

sort = [{"data.label" : {"order" : "asc", "mode" : "min", "nested_path" : "data"}}];
sort = [{ "table.data": {"order": "asc"}]

但是,我没有得到预期的排序结果

对此的任何帮助将不胜感激

【问题讨论】:

  • 你有解决这个问题的办法吗?面对同样的事情。

标签: sorting elasticsearch


【解决方案1】:

查询的排序部分应该是 -

"sort": { "label":   { "order": "desc" }}

"sort": { "value":   { "order": "desc" }}

"sort": [
        { "label":   { "order": "desc" }},
        { "value": { "order": "desc" }}
    ]

下面是获取内部排序元素的映射、文档和后续查询。

PUT /table
{
  "mappings": {
    "data": {
      "properties": {
        "name": {"type": "string"},
        "subjects": {
          "type": "nested", 
          "properties": {
            "name": { "type": "string"},
            "marks":{ "type": "integer"}
          }
        }
      }
    }
  }
}

PUT /table/data/1?pretty 
{
  "name":"Ram",
  "subjects":[
    { 
      "name":"English",
      "marks":13
    },
    { 
      "name":"Hindi",
      "marks":12
    }
    ]
}
PUT /table/data/2?pretty 
{
  "name":"Sam",
  "subjects":[
    { 
      "name":"Biology",
      "marks":83
    },
    { 
      "name":"Maths",
      "marks":68
    }
    ]
}

PUT /table/data/3?pretty 
{
  "name":"Jim",
  "subjects":[
    { 
      "name":"Chemistry",
      "marks":96
    },
    { 
      "name":"Geology",
      "marks":58
    }
    ]
}

GET table/data/_search
{
 "query":{
   "nested":{
     "path":"subjects",
     "query": {
        "match_all": {}
      },
     "inner_hits":{
       "sort":{
         "subjects.marks":{
           "order":"asc"
         }
       }
     }
   }
 }
}

【讨论】:

  • 我尝试了所有组合,但它不起作用。我们应该使用嵌套排序吗?
  • 你能不能把你正在做的完整查询!
  • 请找到查询url - /_search?q="{"filtered":{"filter":{"bool":{"must":[{"ids":{ "values":["search_key"]}}]}"&sort={ "value": { "order": "asc" }}
  • @Arjun sort=value:descsort=value:asc 或类似的东西会这样做。我之前提到的是针对 CURL 请求的。
  • http://localhost:9200/table/data/_search?q={filtered:{filter:{bool:{must:[{label:{values:["search_key"]}}]}&sort=value:asc&pretty - 这对我来说很好。
【解决方案2】:

你有两个选择。

  1. 使用网址参数
POST /index/_search?sort=table.data:asc
{}
  1. 使用正文
POST /index/_search
{
    "sort":[{"table.data": "asc"}],
    ...
}

POST /index/_search
{
    "sort":[{"table.data": {"order": "asc"}}],
    ...
}

【讨论】:

    猜你喜欢
    • 2014-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多