Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested sort inside a top hits inside a reverse nested doesn't work #13420

Closed
ericrenard opened this issue Sep 9, 2015 · 2 comments
Closed

Nested sort inside a top hits inside a reverse nested doesn't work #13420

ericrenard opened this issue Sep 9, 2015 · 2 comments

Comments

@ericrenard
Copy link

Hello,
I'm facing something that looks like a bug. I have a simple document model : my documents are items, and inside these items there are categories.
I want to get a list of categories with a certain type, and for each of these categories, a list of top items belonging to this category, sorted on the category rank. So I'm doing a top hits inside reverse nested

Mapping example :

POST /bugth
{
    "mappings": 
    {        
        "item" : {
            "properties" : {
                "id" : { "type" : "integer" },
                "categories" : { 
                    "type" : "nested",
                    "properties" : {
                        "id" : { "type" : "integer" },
                        "type" : { "type" : "integer" },
                        "rank" : { "type" : "integer" }
                    }
                }
            }
        }        
    }
}

Some data to test :

PUT /bugth/item/3
{
    "id": 3,
    "categories": [{
        "id": 1,
        "type": 1,
        "rank": 15
    }, {
        "id": 2,
        "type": 1,
        "rank": 1
    }]
}

PUT /bugth/item/2
{
    "id": 2,
    "categories": [{
        "id": 1,
        "type": 1,
        "rank": 10
    }, {
        "id": 2,
        "type": 1,
        "rank": 5
    },
    {
        "id": 3,
        "type": 2,
        "rank": 20
    }]
}

PUT /bugth/item/1
{
    "id": 1,
    "categories": {
        "id": 1,
        "type": 1,
        "rank": 0
    }
}

Finally the search query :

POST /bugth/item/_search
{
    "size": 0,
    "query": {
    "filtered": {
      "filter": {
        "match_all": {}
      }
    }
  },
  "aggs": {
      "categories": {                        
          "nested": {
              "path": "categories"
          },
          "aggs": {
              "filteredcategories": {
                  "filter": {
                      "bool": {
                          "must": {
                              "term": { "categories.type": 1}
                          }                          
                      }
                  },
                  "aggs": {
                      "top_categories": {
                          "terms": {
                              "field": "categories.id"
                          },
                          "aggs": {
                              "top_items": {
                                  "reverse_nested": {                                
                                  },
                                  "aggs": {                          
                                      "top_items_per_categories": {                              
                                          "top_hits": {                   
                                              "sort": [
                                                {
                                                  "categories.rank": {
                                                    "order": "asc",
                                                    "mode": "max"
                                                  }
                                                }                                                
                                              ]
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
                }
          }
      }    
    }
}

Ultimately, I want to sort by the category rank of the current bucket but I don't know how to refer to the bucket id inside the sort clause.
Anyways, sorting by any nested property doesn't work (sort doesn't apply). However, sorting by a base property of item do work correctly.

Is this a bug or just me doing my query the wrong way ?

Thanks

@martijnvg
Copy link
Member

@ericrenard if you define the nested_path option and set it to categories then it work as expected:

{
  "size": 0,
  "query": {
    "filtered": {
      "filter": {
        "match_all": {}
      }
    }
  },
  "aggs": {
    "categories": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "filteredcategories": {
          "filter": {
            "bool": {
              "must": {
                "term": {
                  "categories.type": 1
                }
              }
            }
          },
          "aggs": {
            "top_categories": {
              "terms": {
                "field": "categories.id"
              },
              "aggs": {
                "top_items": {
                  "reverse_nested": {},
                  "aggs": {
                    "top_items_per_categories": {
                      "top_hits": {
                        "sort": [
                          {
                            "categories.rank": {
                              "order": "asc",
                              "mode": "max",
                              "nested_path": "categories"
                            }
                          }
                        ]
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

The problem is that nested sorting inside top_hits can't detect automatically with the nested path should be. Also there are cases where the nested_path is selected incorrectly in a regular search request.

I think the automatic detection of the nested path should be removed, because nested sorting is clearer when you tell it what to do. There is a TODO in the code, but I lost track of it.

@ericrenard
Copy link
Author

Yeah, you're right, it works with nested_path, thanks !
Only thing left for me to figure out is if I can reference the current bucket key in the top hits sort, ideally I want to sort by the rank of the category of the current bucket.

martijnvg added a commit that referenced this issue Sep 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants