Need help with filter syntax, not returning the expected results

Hey all! Trying to build an app using Algolia and Im building my own components in React that generates a filter to pass to Algolia. I’m having an issue understanding why some filters work and some don’t though, at least now how I would expect them to work.

To start, here is a sample of a record in Algolia;

    {
      "name": "brianmm02's Titan",
      "notes": "test\nwith\n\nnew\n\nlines",
      "class": 0,
      "subclass": 3,
      "itemHashes": [
        1119734784,
        3356526253,
        3165547384,
        391384020,
        3399325807,
        3497822409,
        1160559849,
        860168648
      ],
      "primaryActivity": "100",
      "itemNames": "Chroma Rush|Wishbringer|Memory Interdict|Iron Fellowship Helm|Lightkin Gauntlets|Lightkin Plate|Dunemarchers|Radegast's Iron Sash|",
      "playerDetails": "14214042|brianmm02|",
      "summary": {
        "id": "fde0add7-62f4-4cf3-87f4-5ec064f310cd",
        "name": "brianmm02's Titan",
        "userId": "14214042",
        "username": "brianmm02",
        "highlights": [
          "https://www.bungie.net/common/destiny2_content/icons/c291e9e23c0f4a7b7646f32e0936e307.jpg",
          "https://www.bungie.net/common/destiny2_content/icons/d21b395a27b3fc994674a199d804e35e.jpg",
          "https://www.bungie.net/common/destiny2_content/icons/ec0fc34b7199005aeaa415adedb8f63b.jpg"
        ],
        "primaryIconSet": "0-3-2"
      }

Here are some filters that I have tested and confirmed working.

  • (primaryActivity:100)
  • (primaryActivity:100) AND (class = 2)
  • (class = 0 OR class = 2)

And here are a few that return NO results and I’m trying to understand why.

  • (primaryActivity:100 OR primaryActivity:102)
  • (primaryActivity:100) AND (class = 1)
  • (primaryActivity:100) AND (class = 2 OR class = 1)

I should add that I tested these filters with and without setting primaryActivity and class as facets but the results dont change.

Any tips? Thank you!

Hey @brian2! Happy to help out if I can.

Out of curiosity, can you share the code you’re using to query?

Also, have you tried these types of queries in the Algolia Dashboard? I just did a very small test in the dashboard and am getting results on primaryActivity 100 OR 102 AND class=0 (granted my Index only has the one item you listed above. Give it a try in the Dashboard and share the query here and we’ll see about getting things working!

Hey @bryan.robinson ! Thanks for the help.

So for whatever reason, it seems that things are working as expected today. I tried both in my UI and in the Dashboard using a custom filter. Any ideas that would cause a delay in the filters working?

Also, here is the code I used to generate the filter queries:


  function buildFilters() {
    let filterMap = {}
    filters.forEach(el => {
      if(filterMap[el.fieldName]) {
        filterMap[el.fieldName].values.push(el.value)
      } else {
        filterMap[el.fieldName] = {
          values: [
            el.value
          ]
        }
      }
    })

    let filterString = ""
    Object.keys(filterMap).forEach(key => {
      if (filterString !== "") {
        filterString += " AND "
      }
      filterString += "("
      filterMap[key].values.forEach((value, idx) => {
        if(typeof(value) === "number") {
          filterString += `${key} = ${value}`
        }
        if(typeof(value) === "string") {
          filterString += `${key}:${value}`
        }
        if (idx < filterMap[key].values.length - 1) {
          filterString += " OR "
        }
      })
      filterString += ")"
    })
    return filterString
  }

And here is an example of a filter:

    {
      id: "class-0",
      friendlyName: "Class",
      displayValue: "Titan",
      fieldName: "class",
      value: 0,
      iconPath: "/img/classicos/0nom.png",
      color: "rgb(188,58,34)"
    },

thanks for the awesome information.

1 Like