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

Reactive Elasticsearch tophits #602

Open
bompi88 opened this issue Apr 25, 2017 · 1 comment
Open

Reactive Elasticsearch tophits #602

bompi88 opened this issue Apr 25, 2017 · 1 comment

Comments

@bompi88
Copy link
Contributor

bompi88 commented Apr 25, 2017

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
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants