matteodem / meteor-easy-search

Easy-to-use search for Meteor with Blaze Components
MIT License
438 stars 68 forks source link

Reactive Elasticsearch tophits #602

Open bompi88 opened 7 years ago

bompi88 commented 7 years ago

The top hits aggregation is quite a powerful feature of Elasticsearch. The aggregation support is a step closer to get this functionality, but it would be awesome to make these top hits reactive and as a built it feature.

Example:

engine: new EasySearch.ElasticSearch({
   tophits: [{ field: 'category', size: 5, limit: 10 }]
})

This would generate tophits for 5 of the most popular categories. This would result in an ES aggregation that looks like:

{
  "top_category_hits": {
    "terms": {
      "field": "category",
      "order": {
        "max_score": "desc"
      },
      "size": 5
    },
    "aggs": {
      "top_category_hits": {
        "top_hits": {
          "track_scores": true,
          "sort": [{
            "_score": {
              "order": "desc"
            }
          }],
          "_source": {
            "include": [
              "name", "category", "score" // example fields
            ]
          },
          "size": 10
        }
      },
      "max_score": {
        "max": {
          "script": "_score"
        }
      }
    }
  }
}

The tophits can then be retrieved by:

var cursor = index.search('test');

// get all aggregations
var types = cursor.getTophitsTypes();

types.forEach(function (type) {
  var tophits = cursor.getTophits(type);
  console.log(tophits.fetch());
})

I'd like some feedback on this 😄

matteodem commented 7 years ago

looks cool for me! would be awesome to have the logic for the tophits implemented in an ElasticsearchCursor so that we don't have to adjust code in the core package unnecessarily.