Querying DecentCMS, part 1: building an index
It is possible to create and query a search index from code, like
documentation-toc-part is doing, but there is also a ready-to-use content part that makes simple querying really easy:
In order to perform a search, an index has first to be created. An index is defined by three things:
An optional id filter
This is your first and most efficient way to filter out content items. It's a regular expression that will be tested against the id of the content items. If it tests negatively, the content item won't even be fetched from the store.
A mapping function
SELECTin SQL: it defines what properties will be available on the index entries, the same way that a SQL
SELECTspecifies what columns will be available on the rows of the result set. A big difference however is that the mapping function may decline to return an entry for some items, and can return more than one if necessary. In that sense, it's also a little bit of a
An order function
From those three things, an index can be built by the search module by scanning each content item in the system, no matter where it is stored.
The index will be pre-sorted, and pre-filtered by both the id filter, and the logic in the mapping function. The index doesn't need to be rebuilt as long as the content items don't change. When a content item changes, it is possible to update the index by running the id filter and the mapping function on just this item, and by using the order by function to figure out where to change index entries.
This makes the system fast on querying, and a little slower on write operations, as all indexes in the system potentially need to be updated in such a case. The index updates can be performed asynchronously in the background, however, which mitigates the issue.
An index is built by requiring an
index service from the scope, and then calling getIndex on it. An optional name can be given to the index, and it is recommended that you do so. Otherwise, a name is generated from the filter, map, and order function source code, which is harder to maintain.
In the next post, I’ll show how to query the index we just built.