Query an index from the dashboard doesn't work

I have a Forms collection that has, amongst other fields, the fields accountId and id which is identical to the actual document id.
I have created this index:

{
  name: "forms_by_accountId_and_id",
  unique: false,
  serialized: true,
  source: "Forms",
  terms: [
    {
      field: ["data", "accountId"]
    },
    {
      field: ["data", "id"]
    }
  ],
  values: [
    {
      field: ["data"]
    }
  ]
}

and also this one:

{
  name: "forms_by_accountId_and_id_",
  unique: false,
  serialized: true,
  source: "Forms",
  terms: [
    {
      field: ["data", "accountId"]
    },
    {
      field: ["ref", "id"]
    }
  ],
  values: [
    {
      field: ["data"]
    }
  ]
}

When I paste some values in the dashabord on either of these Indexes I get no results.
I have tripple-checked that the values are correct.
I actually copy-paste them from a document that I get from the Collection tab, so it is there for sure.
Other indexes work properly.

p.s.
The first index is new. I created it after I noticed that the second one does not work, so I renamed it by adding _ and created the new one that uses the id from the data rather than from the ref.

Any ideas?

I suspect the problem is that these indexes appear to be specifying the collection source incorrectly.

Instead of:

  source: "Forms",

use

  source: Collection("Forms"),

Thanks for the suggestion, but that’s not the problem :frowning:
I actually didn’t write that by hand - I used the UI and then switched to the fql tab which gave me this syntax. So I’d be very surprised if that was the issue.

The representation of an index in the Dashboard is not equivalent to the FQL required to create the index. Since you managed to create the indexes successfully, that’s not the problem.

I had not noticed on my first reading that your values definition is attempting to return the data field. Indexes can contain scalar values, or arrays of scalar values. The data field in a document is an object: because it is an object, data is not stored in the index. You need to specify the values field to list the fields within data that should be stored in the index.

For example:

> CreateIndex({
  name: "forms_by_accountId_and_id2",
  unique: false,
  serialized: true,
  source: Collection("Forms"),
  terms: [
    { field: ["data", "accountId"]},
    { field: ["data", "id"]}
  ],
  values: [
    { field: ["data", "foo"] },
  ]
})

> Create(Collection("Forms"), { data: { accountId: '123', id: '1', foo: 'bar' }})
> Create(Collection("Forms"), { data: { accountId: '123', id: '2', foo: 'baz' }})
> Create(Collection("Forms"), { data: { accountId: '456', id: '3', foo: 'foo' }})

> Paginate(Match(Index("forms_by_accountId_and_id"), '123', '2'))
{ data: [] }

> Paginate(Match(Index("forms_by_accountId_and_id2"), '123', '2'))
{ data: [ 'baz' ] }

Hi @ewan , thanks for the detailed answer.

The representation of an index in the Dashboard is not equivalent to the FQL required to create the index

Why is that so? I would expect to be able to use this FQL as a reference to how an Index can be created using FQL. Having an incorrect FQL is misleading.

because it is an object, data is not stored in the index

I think it would be very helpful if the UI would protect you from such a mistake. The UI already does a few things for you, for example turning id into ref.id or someField into data.someField. So this is just another thing it can help those who didn’t read all part of the docs :slight_smile:

The representation of indexes in the dashboard is likely a bug, and an issue has been filed to investigate.

Indeed, the Dashboard could be more helpful in guidance users around common problems. That’s good feedback that we’ll incorporate into our development plans!