简体   繁体   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": [
     
    ]
}

I want to sort the below documents based on tags first and then createdAt.我想先根据标签对以下文档进行排序,然后再根据 createdAt 对它们进行排序。 If tags is present then it should be in ascending order of createdAt otherwise if tags is blank then it should be in sort based on createdAt in Descending order.如果标签存在,那么它应该按照 createdAt 的升序排列,否则如果标签为空,那么它应该按照 createdAt 的降序排列。

I believe the solution is to use Script based sorting .我相信解决方案是使用基于脚本的排序

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": [
     
    ]
}

Query询问

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"
      }
    }
  ]
}

Response回复

"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.

相关问题 无法在弹性搜索中创建TransportClient - Unable to create TransportClient in Elastic-Search Jhipster弹性搜索数据迁移 - Jhipster Elastic-search data migration 如何在弹性搜索中基于多字段值进行过滤 - How to filter based on multiple field value in elastic search java 从Java应用程序插入数据以进行弹性搜索 - insert data in to elastic-search from Java application 使用springboot与本地弹性搜索实例不起作用 - using springboot with local elastic-search instance not working Java中的弹性搜索查询:包含字符串查询? - Elastic-search queries in Java: Contains-String query? 如何使用Java API计算基于多个字段值的Elastic Search文档? - How to count Elastic Search documents based on multiple field values with the Java API? Hibernate-search:通过在 hibernate-search 中以编程方式注册字段,基类字段未在弹性搜索中注册 - Hibernate-search: Base class fields are not registered in elastic-search by registering the fields programmatically in hibernate-search 弹性搜索排序字段,其中包含特殊字符数字和字母缩写 - Elastic search sort field containing special characters numbers and alpahbets 为什么弹性搜索容器的内存使用量会随着使用量的增加而持 - Why elastic-search container memory usage keeps increasing with little use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM