Created
June 7, 2018 08:32
-
-
Save alexandrebodin/abb52bccc19d35fc9843acc44dbd4b46 to your computer and use it in GitHub Desktop.
aggregation_search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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*/ | |
] | |
} | |
] | |
} | |
] | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We would also need the different facet values for color somewhere :)