Elasticsearch exclude top hit on field value

前端 未结 4 773
遇见更好的自我
遇见更好的自我 2021-01-17 19:23
{\'country\': \'France\', \'collected\': \'2018-03-12\', \'active\': true}
{\'country\': \'France\', \'collected\': \'2018-03-13\', \'active\': true}
{\'country\': \         


        
4条回答
  •  一个人的身影
    2021-01-17 20:21

    I think you can get away with sorting by two fields in your top_hits: by active and by collected. Basically, you want trues to be first and when equal, then sort by collected. Something like the following will always show the active:true documents sorted by collected.

    The only downside to this solution is that if you don't have any active documents, top_hits will show one active:false document.

    {
      "size": 0,
      "aggs": {
        "group": {
          "terms": {
            "field": "country"
          },
          "aggs": {
            "group_docs": {
              "top_hits": {
                "size": 1,
                "sort": [
                  {
                    "active": {
                      "order": "desc"
                    }, 
                    "collected": {
                      "order": "desc"
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
    

提交回复
热议问题