Sorting documents by a nested field

Nour

I'm trying to sort the result returned by ElasticSearch by the nested field sections.name as follows:

Mapping:

PUT /staff
{
    "mappings": {
        "list": {
            "properties": {
                "id": {"type": "text" },
                "name": {
                  "type":"text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "sections" : {
                  "type":"nested",
                  "properties": {
                    "id": {"type":"text", "fielddata" : true},
                    "name": {
                    "fielddata" : true,
                    "type": "text",
                    "fields": {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                      }
                    }
                  }
                }
              }
            }
        }
    }
}

documents:

POST /staff/list
{
    "id": 10,
    "name": "abc def",
    "sections":
    [
      {
        "id":"1",
        "name" : "zamphire"
      },{
        "id":"2",
        "name" : "warden"
      }
    ]
}

POST /staff/list
{
    "id": 9,
    "name": "abc def",
    "sections":
    [
      {
        "id":"1",
        "name" : "shaggi"
      },{
        "id":"2",
        "name" : "robert"
      }
    ]
}

POST /staff/list
{
    "id": 8,
    "name": "abc def",
    "sections":
    [
      {
        "id":"3",
        "name" : "zamphire"
      },{
        "id":"2",
        "name" : "abi"
      }
    ]
}

I'm performing the following query:

GET /staff/_search
{
  "from": 0,
  "query": {
    "nested": {
      "path": "sections",
      "query": {
        "match": {
          "sections.id": {
            "query": "1"
          }
        }
      }
    }
  },
  "size": 25,
  "sort": [
    {
      "sections.name": {
        "nested": {
          "filter": {
            "nested": {
              "path": "sections",
              "query": {
                 "term" : { "sections.id" : "1" }
              }
            }
          }
        },
        "order": "asc"
      }
    }
  ],
  "_source": {
    "includes": [
      "id",
      "name",
      "sections"
    ]
  }
}

I get these results:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "staff",
        "_type" : "list",
        "_id" : "rJtyyGwBNB-cdBRb5XGR",
        "_score" : null,
        "_source" : {
          "name" : "abc def",
          "id" : 10,
          "sections" : [
            {
              "name" : "zamphire",
              "id" : "1"
            },
            {
              "name" : "warden",
              "id" : "2"
            }
          ]
        },
        "sort" : [
          null
        ]
      },
      {
        "_index" : "staff",
        "_type" : "list",
        "_id" : "rZtyyGwBNB-cdBRb6nHU",
        "_score" : null,
        "_source" : {
          "name" : "abc def",
          "id" : 9,
          "sections" : [
            {
              "name" : "shaggi",
              "id" : "1"
            },
            {
              "name" : "robert",
              "id" : "2"
            }
          ]
        },
        "sort" : [
          null
        ]
      }
    ]
  }
}

I'm expecting the section shaggi to come before zamphire and thus the order of the two documents should be reversed.

I noticed this in the results:

"sort" : [
  null
]

Is that related? What am I missing here?

Rob

Changing sort part to this should do the job according to the docs

  "sort": [
    {
      "sections.name": {
        "order": "asc", 
        "nested": {
          "path": "sections",
          "filter": {
            "term" : { "sections.id" : "1" }
          }
        }
      }
    }
  ]

Returns

{
  "took" : 7,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [
      {
        "_index" : "staff",
        "_type" : "_doc",
        "_id" : "8hSJyWwBHfpsFyAs9f_8",
        "_score" : null,
        "_source" : {
          "name" : "abc def",
          "id" : 9,
          "sections" : [
            {
              "name" : "shaggi",
              "id" : "1"
            },
            {
              "name" : "robert",
              "id" : "2"
            }
          ]
        },
        "sort" : [
          "shaggi"
        ]
      },
      {
        "_index" : "staff",
        "_type" : "_doc",
        "_id" : "8RSJyWwBHfpsFyAs5v98",
        "_score" : null,
        "_source" : {
          "name" : "abc def",
          "id" : 10,
          "sections" : [
            {
              "name" : "zamphire",
              "id" : "1"
            },
            {
              "name" : "warden",
              "id" : "2"
            }
          ]
        },
        "sort" : [
          "zamphire"
        ]
      }
    ]
  }
}

Tested with elasticsearch 7.2.0.

Hope that helps.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Elasticsearch - Sorting by a nested field in 2019

分類Dev

select documents grouped by field

分類Dev

Sorting nested elements with elementtree

分類Dev

Sorting dict() on field of class

分類Dev

Saving and updating nested documents with MongoEngine

分類Dev

Exclude nested documents based on a condition

分類Dev

How to apply nested sorting in javascript

分類Dev

Python: Sorting a nested dict by date

分類Dev

How to get keys of nested mongodb documents

分類Dev

Sort Documents based on nested child count in ElasticSearch

分類Dev

How to sort documents based on length of an Array field

分類Dev

Find Documents Where a Field Compares with Another in an Array

分類Dev

Flutter firestore fetch documents with a condition on a map field

分類Dev

MongoDB - how to merge dict field of documents?

分類Dev

ElasticSearch - Copy one field value to other field for all documents

分類Dev

Sorting a nested array from model in Ember?

分類Dev

Sorting <li> elements by nested hidden input values

分類Dev

ElasticSearch Aggregation + Sorting in on NonNumric Field 5.3

分類Dev

how to sorting according to the value of a field? mongo

分類Dev

Update nested variable field in firestore

分類Dev

Pass nested document query field

分類Dev

How to read attributes out of multiple nested documents in MongoDB Java?

分類Dev

How can I rename a field for all documents in MongoDB?

分類Dev

Bulk add new field to ALL documents in an elasticsearch index

分類Dev

Do elasticsearch documents within a type have to have the same field structure

分類Dev

How to find all documents where the value of one field matches that of another

分類Dev

MongoDB - Find documents matching certain condition for unknown field keys

分類Dev

Pymongo: Aggregate all documents that not have one field and have another - group by

分類Dev

$sum a field in an array - Query that results documents above a certain value

Related 関連記事

  1. 1

    Elasticsearch - Sorting by a nested field in 2019

  2. 2

    select documents grouped by field

  3. 3

    Sorting nested elements with elementtree

  4. 4

    Sorting dict() on field of class

  5. 5

    Saving and updating nested documents with MongoEngine

  6. 6

    Exclude nested documents based on a condition

  7. 7

    How to apply nested sorting in javascript

  8. 8

    Python: Sorting a nested dict by date

  9. 9

    How to get keys of nested mongodb documents

  10. 10

    Sort Documents based on nested child count in ElasticSearch

  11. 11

    How to sort documents based on length of an Array field

  12. 12

    Find Documents Where a Field Compares with Another in an Array

  13. 13

    Flutter firestore fetch documents with a condition on a map field

  14. 14

    MongoDB - how to merge dict field of documents?

  15. 15

    ElasticSearch - Copy one field value to other field for all documents

  16. 16

    Sorting a nested array from model in Ember?

  17. 17

    Sorting <li> elements by nested hidden input values

  18. 18

    ElasticSearch Aggregation + Sorting in on NonNumric Field 5.3

  19. 19

    how to sorting according to the value of a field? mongo

  20. 20

    Update nested variable field in firestore

  21. 21

    Pass nested document query field

  22. 22

    How to read attributes out of multiple nested documents in MongoDB Java?

  23. 23

    How can I rename a field for all documents in MongoDB?

  24. 24

    Bulk add new field to ALL documents in an elasticsearch index

  25. 25

    Do elasticsearch documents within a type have to have the same field structure

  26. 26

    How to find all documents where the value of one field matches that of another

  27. 27

    MongoDB - Find documents matching certain condition for unknown field keys

  28. 28

    Pymongo: Aggregate all documents that not have one field and have another - group by

  29. 29

    $sum a field in an array - Query that results documents above a certain value

ホットタグ

アーカイブ