Skip to content

Instantly share code, notes, and snippets.

@manegame
Last active May 31, 2019 22:45
Show Gist options
  • Save manegame/81a16150d2936b5536a99e0df5947e3f to your computer and use it in GitHub Desktop.
Save manegame/81a16150d2936b5536a99e0df5947e3f to your computer and use it in GitHub Desktop.
[
{
"category": "genre",
"uid": "32e195f0-f5c9-4646-b76c-b4a68cc11be2",
"percentage": 100,
"color": "rgb(53, 204, 113)",
"catIndex": 0,
"subcatIndex": 0
},
{
"category": "situation",
"uid": "4cfc25b9-9f71-4170-9066-db3b9bdb4977",
"percentage": 100,
"color": "rgb(67, 218, 127)",
"catIndex": 0,
"subcatIndex": 6
},
{
"category": "genre",
"uid": "78fbe83e-7b97-4ac0-a772-c8e2a66ab58d",
"percentage": 84,
"color": "rgb(59, 159, 226)",
"catIndex": 1,
"subcatIndex": 2
},
{
"category": "mood",
"uid": "88207876-6536-7ef0-79ac-ec7d0f97c3fb",
"percentage": 84,
"color": "rgb(183, 126, 200)",
"catIndex": 2,
"subcatIndex": 3
}
]
musimapRequests() {
let mm = {
requests: []
}
let query = {
MUST: [],
SHOULD: []
}
let genres = this.allPercentages.filter(perc => perc.catIndex === 0) // allPercentages
if (genres.length < 2) {
/**
* 0 or 1 genres, results in 1 query
*/
this.allPercentages.forEach((perc, index) => {
if (this.isType(perc.uid, 'genre') || this.isType(perc.uid, 'situation')) {
query.MUST.push({
MATCH: {
predicted_lexicology_v1_1: {
id: perc.uid,
value: this.bound(perc.percentage) // Bound percentage range
}
}
})
} else {
query.SHOULD.push({
MATCH: {
predicted_lexicology_v1_1: {
id: perc.uid,
value: this.bound(perc.percentage)
}
}
})
}
})
mm.requests.push(query)
} else {
/**
* More than one genre
*/
let maxMUST = Math.min(genres.length, 3)
genres.forEach((genre, i) => {
let innerQuery = {
MUST: [],
SHOULD: []
}
this.allPercentages.forEach(innerPerc => {
if (i < maxMUST) {
if (this.isType(innerPerc.uid, 'genre')) {
innerQuery.MUST.push({
MATCH: {
predicted_lexicology_v1_1: {
id: innerPerc.uid,
value: this.bound(innerPerc.percentage)
}
}
})
} else {
innerQuery.SHOULD.push({
MATCH: {
predicted_lexicology_v1_1: {
id: innerPerc.uid,
value: this.bound(innerPerc.percentage)
}
}
})
}
}
mm.requests.push(innerQuery)
})
})
}
return mm
},
[
// Beginning of the first request
{
"MUST": [
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "32e195f0-f5c9-4646-b76c-b4a68cc11be2",
"value": "[80:100]"
}
}
},
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "4cfc25b9-9f71-4170-9066-db3b9bdb4977",
"value": "[80:100]"
}
}
}
],
"SHOULD": [
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "78fbe83e-7b97-4ac0-a772-c8e2a66ab58d",
"value": "[64:100]"
}
}
},
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "88207876-6536-7ef0-79ac-ec7d0f97c3fb",
"value": "[64:100]"
}
}
}
]
},
// Beginning of the second request
{
"MUST": [
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "32e195f0-f5c9-4646-b76c-b4a68cc11be2",
"value": "[80:100]"
}
}
},
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "4cfc25b9-9f71-4170-9066-db3b9bdb4977",
"value": "[80:100]"
}
}
}
],
"SHOULD": [
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "78fbe83e-7b97-4ac0-a772-c8e2a66ab58d",
"value": "[64:100]"
}
}
},
{
"MATCH": {
"predicted_lexicology_v1_1": {
"id": "88207876-6536-7ef0-79ac-ec7d0f97c3fb",
"value": "[64:100]"
}
}
}
]
}
]
[
{
id: 'a',
type: genre,
score: 100
},
{
id: 'b',
type: genre,
score: 80
},
{
id: 'c',
type: situation,
score: 100
},
]
should output:
[
// 1st query
{
MUST: [
{ MATCH: 'a' },
{ MATCH: 'b' },
],
SHOULD: [
{ MATCH: 'c' },
]
},
// 2nd
{
MUST: [
{ MATCH: 'a' },
],
SHOULD: [
{ MATCH: 'b' },
{ MATCH: 'c' },
]
},
// 3rd
{
MUST: [
{ MATCH: 'b' },
],
SHOULD: [
{ MATCH: 'a' },
{ MATCH: 'c' },
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment