# Search and the file index

Users search for files by name in the web interface. By default the server answers a search by walking through the folder that is searched, at that moment. For big folders that is slow. The server can keep an **index** instead: a list of what is in the folders, kept up to date, from which a search is answered at once.

The index is a set of plain files on the server's disk. No database is involved.

## Searching without an index

A search looks through the folder the user is in and everything below it, with the user's own rights: what a user may not see is not found.

It matches the words against file names, and against more when you allow it in [Search](Prefs-Search.md) of the Preferences:

| Setting | A search also looks at |
|---|---|
| **Search the keywords of items too** | the keywords users gave to files |
| **Search inside files too** | the text in files. "Each file up to 5 MB is read at the search, so this is slow on big folders." |

On storage that can search by itself, the server can hand the search over: "Where there is no index, let a storage that can search do the search itself".

## The index

Name the users whose folders shall be indexed, under **Index the folders of these users** in [Search](Prefs-Search.md). The server then walks through the folders of those users' VFS and writes down what it finds.

An index belongs to a **place**, not to a user. If the index user has the folder `/srv/data/projects/`, a search below that folder is answered from the index for **every** user who reaches it, with their own rights. One index user whose VFS covers all the shared folders is enough: make a user for this purpose, such as `indexer`, give it the folders, and no login it does not need.

A folder that is not in the index is walked at the moment of the search, as before. Turning the index on never makes a search find less.

## How it stays up to date

| What happens | The index |
|---|---|
| an upload, a rename, a delete, a new folder or new keywords **through the server** | is changed at once |
| a change on the disk **without the server** | is found by the next walk |
| a walk | every 60 minutes unless you set another time under **Walk the indexed folders every**; at once when the index users change; and when **Rebuild Now** is pressed |

A walk lists every folder of the index users, down to 20 levels. While it runs, searches are answered from the index as it is. If a walk finds nothing where there were items, for instance because storage could not be reached, the last good index is kept and the page says so.

## The Index Now

The Preferences show a line for each indexed place: the folder, its state, how many items it holds, when it was last walked, how long that took, how many changes came in since, and a note. **Refresh** reads it again; **Rebuild Now** starts a walk of everything.

## Where the index is kept

In the folder `search` of the server's activity folder, or in the folder you name under **Folder of the index**. There is one subfolder per indexed place, holding a sorted list of every item, with path, type, size, date and keywords, at about 75 bytes an item, and a list of the changes since.

The index holds names and keywords, not the content of files. With **Search inside files too**, contents are read from the files at the moment of the search.

It needs no backup: if it is lost, the next walk writes it again.

## In a cluster

Servers that share the index folder share the index. Each writes down its own changes and reads the others' every few seconds, and one of them at a time walks a place. With separate folders, each server keeps and walks its own index.

A DMZ server keeps no index: it passes searches on to the internal server.

## The memory cache

**Memory cache (memcache)** keeps the index users' **local** folders in the server's memory instead, rebuilt at the same interval, and searches are answered from there. It is the fastest and costs memory for every file. Use the index on disk unless you have measured a need.

## Coming from an earlier version

Earlier versions kept the index in a database. Nothing of it is carried over, and nothing needs to be: the first walk writes the new index, within a minute of the start when index users are set. A `searchDB` folder left by an older server can be deleted.

## When a search finds too little

| What you see | Look at |
|---|---|
| a file that was copied onto the disk by hand is not found | it is found after the next walk: press **Rebuild Now** |
| a whole folder is missing from results | **The Index Now**: its state and note. A folder whose listing failed is left out until a walk reaches it again. |
| a user finds nothing in a folder others can search | the user's own rights there: results are checked against them |
| words inside documents are not found | **Search inside files too**, and for formats other than plain text the Tika tool of [Preview](Prefs-Preview.md) |
