Skip to content

Instantly share code, notes, and snippets.

@alexandrebodin
Created June 7, 2018 08:32
Show Gist options
  • Save alexandrebodin/abb52bccc19d35fc9843acc44dbd4b46 to your computer and use it in GitHub Desktop.
Save alexandrebodin/abb52bccc19d35fc9843acc44dbd4b46 to your computer and use it in GitHub Desktop.
aggregation_search
import algoliasearch from 'algoliasearch';
const client = algoliasearch('applicationId', 'apiKey');
const index = client.initIndex('indexName');
index.setSettings({
attributesForFaceting: ['someDate']
// other settings
});
index
.search({
query: 'query',
aggregation: {
type: 'date',
field: 'someDate',
interval: '1w'
// we could image to have nested aggregations
// for exemple making a timeline with the avg of price per color
// with some nested parameters:
// aggregation: {
// type: 'avg',
// field: 'price'
// facet: ['color']
// }
}
})
.then(res => {
// The main data I would then need to get is sth like that
res = {
aggregation: [
{
facetValue: 'date1',
hits: [
/*matching objects */
]
},
{
facetValue: 'date2',
hits: [
/*matching objects */
]
},
{
facetValue: 'date3',
hits: [
/*matching objects */
]
}
]
};
// or with nested aggregations
// it's missing a lot of meta info but the main idea is here I hope :)
res = {
aggregation: [
{
facetValue: 'date1',
aggregation: [
{
facetValue: 'color1',
value: 'avgPrice',
hits: [
/*matching objects*/
]
}
]
},
{
facetValue: 'date2',
aggregation: [
{
facetValue: 'color1',
value: 'avgPrice',
hits: [
/*matching objects*/
]
}
]
},
{
facetValue: 'date3',
aggregation: [
{
facetValue: 'color1',
value: 'avgPrice',
hits: [
/*matching objects*/
]
}
]
}
]
};
});
@alexandrebodin
Copy link
Author

We would also need the different facet values for color somewhere :)

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