sonandmjyS
Convex Community13mo ago
3 replies
sonandmjy

search index multiple chained filter fields behaviour not as expected

Hi there,

I have a schema that looks like this
export const Destinations = Table("destinations", {
  type: VDestinationTypes,
  contactId: v.optional(v.id("contacts")),
  value: v.string(),
  isDeleted: v.boolean(),
  organizationId: v.id("organizations"),
  lastMessageId: v.optional(v.record(v.id("channels"), v.id("messages"))),
  lastInboundMessageId: v.optional(
    v.record(v.id("channels"), v.id("messages")),
  ),
  lastOutboundMessageId: v.optional(
    v.record(v.id("channels"), v.id("messages")),
  ),
});

and I am trying to add a searchIndex like this

Destinations.table
    .index("by_contact_id", [
      "organizationId",
      "contactId",
      "isDeleted",
      "type",
    ])
    .index("by_value", ["organizationId", "type", "value", "isDeleted"])
    .searchIndex("by_search_value", {
      searchField: "value",
      filterFields: ["organizationId", "type", "isDeleted", "contactId"],
    })

but when I try to filter it using a query

ctx.db
  .query("destinations")
  .withSearchIndex("by_search_value", (q) => {
    return q
      .search("value", args.searchQuery)
      .eq("organizationId", ctx.user.defaultOrganizationId!)
      .eq("type", "phone_number")
      .eq("isDeleted", false)
      .eq("contactId", undefined);
  })
  .take(20)

it seems that it is still returning results where contactId IS defined. Also when I change .eq("type", "phone_number") => .eq("type", "email") it is still returning results where the type is of phone_number. When I only have 1 .eq the filter seems to be working fine but when I chain them they are not. Just want to check if I am misunderstanding some limitations of how to use this filter or if it is a bug
Was this page helpful?