簡體   English   中英

無法基於彈性搜索中的多個文本字段進行排序

[英]Not able sort based on multiple text field in elastic-search

doc1: 
{
    "createdAt": "2022-11-25T09:45:00.000Z",
    "tags": [
      "Response Needed"
    ]
}
doc2 :
{
    "createdAt": "2022-11-24T09:45:00.000Z",
    "tags": [
      "Customer care","Response Needed"
    ]
}
doc3 :
{
    "createdAt": "2022-11-24T09:45:00.000Z",
    "tags": [
     
    ]
}

我想先根據標簽對以下文檔進行排序,然后再根據 createdAt 對它們進行排序。 如果標簽存在,那么它應該按照 createdAt 的升序排列,否則如果標簽為空,那么它應該按照 createdAt 的降序排列。

我相信解決方案是使用基於腳本的排序

PUT idx_sort
{
  "mappings": {
      "properties": {
        "createdAt": {
          "type": "date"
        },
        "tags": {
          "type": "keyword"
        }
      }
    }
}

POST idx_sort/_doc
{
    "createdAt": "2022-11-25T09:45:00.000Z",
    "tags": [
      "Response Needed"
    ]
}

POST idx_sort/_doc
{
    "createdAt": "2022-11-24T09:45:00.000Z",
    "tags": [
      "Response 02"
    ]
}

POST idx_sort/_doc
{
    "createdAt": "2022-11-24T09:45:00.000Z",
    "tags": [
      "Customer care","Response Needed"
    ]
}

POST idx_sort/_doc
{
    "createdAt": "2022-11-24T09:45:00.000Z",
    "tags": [
     
    ]
}

詢問

GET idx_sort/_search
{
  "sort": [
    {
      "_script": {
        "type": "number",
        "script": {
          "lang": "painless",
          "source": """
          def list = doc['tags.keyword'];
          if(list.size() > 0){
            return list.size();
          } else {
            return 0;
          }
          """
        },
        "order": "desc"
      }
    },
    {
      "createdAt": {
        "order": "asc"
      }
    }
  ]
}

回復

"hits": [
      {
        "_index": "idx_sort",
        "_id": "j489toQBEoAIompjkkXO",
        "_score": null,
        "_source": {
          "createdAt": "2022-11-24T09:45:00.000Z",
          "tags": [
            "Response 02"
          ]
        },
        "sort": [
          1,
          1669283100000
        ]
      },
      {
        "_index": "idx_sort",
        "_id": "kI89toQBEoAIompjxkWN",
        "_score": null,
        "_source": {
          "createdAt": "2022-11-24T09:45:00.000Z",
          "tags": [
            "Customer care",
            "Response Needed"
          ]
        },
        "sort": [
          1,
          1669283100000
        ]
      },
      {
        "_index": "idx_sort",
        "_id": "jo83toQBEoAIompjcEXD",
        "_score": null,
        "_source": {
          "createdAt": "2022-11-25T09:45:00.000Z",
          "tags": [
            "Response Needed"
          ]
        },
        "sort": [
          1,
          1669369500000
        ]
      },
      {
        "_index": "idx_sort",
        "_id": "kY8-toQBEoAIompj6kXg",
        "_score": null,
        "_source": {
          "createdAt": "2022-11-24T09:45:00.000Z",
          "tags": []
        },
        "sort": [
          0,
          1669283100000
        ]
      }
    ]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM