弹性搜索按特定顺序按字段排序 [英] Elasticsearch Query sorted by field in specific order

查看:137
本文介绍了弹性搜索按特定顺序按字段排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用elasticsearch 2.2

I work with elasticsearch 2.2

我有一些索引的文档:

{     
  "id": 2116,
  "brand": "The Brand 1",
  "reference": "OI444",
  "supplier": "My supplier 1"
},
{     
  "id": 2118,
  "brand": "The Brand",
  "reference": "OI44488",
  "supplier": "My supplier"
}

请求索引获取按id列表排序的查询,此查询工作,但以其他顺序返回结果。

how i can request the index to get a query sorted by an id list, this query work, but return the result in other order.

{
  "query": {
    "bool": {
      "filter": {
        "ids": {
          "values": [
            2118, 2116
          ]
        }
      }
    }
  }
}

在我的情况下,我的ids列表可以是随机的,如216,105,208,我需要这个顺序在结果集中被尊重

In my case, my ids list can be random like 216,105,208 and i need this order be respected in resultset

推荐答案


  • 你使用的字段应该是b没有在映射中分析

  • 添加排序

  • 查询

    POST c1_2/Test/_search/
    {
      "sort": [
        {
          "_script": {
            "script": "doc['id'] != null ? sortOrder.indexOf(doc['id'].value.toInteger()) : 0",
            "type": "number",
            "params": {
              "sortOrder": [
                2116,
                211
              ]
            },
            "order": "desc"
          }
        },
        "_score"
      ],
      "query": {
        "bool": {
          "should": [
            {
              "terms": {
                "brand": [
                  "The"
                ]
              }
            },
            {
              "ids": {
                "values": [
                  2118,
                  2116,
                  211
                ]
              }
            }
          ]
        }
      }
    }
    

    这篇关于弹性搜索按特定顺序按字段排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆