Skip to main content

Exercises: compound

minimumShouldMatch​

πŸ”— Playground link: adjust minimumShouldMatch
Solution
"minimumShouldMatch": 2

Increase minimumShouldMatch to increase precision of results.

filter​

You've got Products, each with a name and a category. A user is looking for accessories for a "camera", not actual cameras. Let's guide the user:

πŸ”— Playground link: filter by category
Solution

You might first try adding an equals on category within compound.filter:

    filter: [
{
equals: {
path: "category",
value: "Accessories"
}
}
]

Which leads us to this, which isn't yet working: πŸ”— Playground link: filter by category not working yet

Note the surprising effect of querying a path that was not indexed specifically for that operator!

Using equals on a string field effectively requires that it be mapped as type token:

  {
"mappings": {
"dynamic": true,
"fields": {
"category": [
{
"type": "token"
}
]
}
}
}

With this final solution: πŸ”— Playground link: filter by category solution