External Indexing and Searching

# Introduction

With release 1.8.3, doxygen provides the ability to search through HTML using an external indexing tool and search engine. This has several advantages:

- For large projects it can have significant performance advantages over doxygen's built-in search engine, as doxygen uses a rather simple indexing algorithm.
- It allows combining the search data of multiple projects into one index, allowing a global search across multiple doxygen projects.
- It allows adding additional data to the search index, i.e. other web pages not produced by doxygen.
- The search engine needs to run on a web server, but clients can still browse the web pages locally.

To avoid that everyone has to start writing their own indexer and search engine, doxygen provides an example tool for each action: `doxyindexer` for indexing the data and `doxysearch.cgi` for searching through the index.

The data flow is shown in the following diagram:

External Search Data Flow

- `doxygen` produces the raw search data
- `doxyindexer` indexes the data into a search database `doxysearch.db`
- when a user performs a search from a doxygen generated HTML page, the CGI binary `doxysearch.cgi` will be invoked.
- the `doxysearch.cgi` tool will perform a query on the database and return the results.
- The browser will show the search results.

# Configuring

The first step is to make the search engine available via a web server. If you use `doxysearch.cgi` this means making the CGI binary available from the web server (i.e. be able to run it from a browser via an URL starting with http:)

How to setup a web server is outside the scope of this document, but if you for instance have Apache installed, you could simply copy the `doxysearch.cgi` file from doxygen's `bin` directory to the `cgi-bin` directory of the Apache web server. Read the apache documentation for details.

To test if `doxysearch.cgi` is accessible start your web browser and point to URL to the binary and add `?test` at the end

    http://yoursite.com/path/to/cgi/doxysearch.cgi?test

You should get the following message:

    Test failed: cannot find search index doxysearch.db

If you use Internet Explorer you may be prompted to download a file, which will then contain this message.

Since we didn't create or install a doxysearch.db it is OK for the test to fail for this reason. How to correct this is discussed in the next section.

Before continuing with the next section add the above URL (without the `?test` part) to the SEARCHENGINE_URL tag in doxygen's configuration file:

    SEARCHENGINE_URL = http://yoursite.com/path/to/cgi/doxysearch.cgi

## Single project index

To use the external search option, make sure the following options are enabled in doxygen's configuration file:

    SEARCHENGINE           = YES
    SERVER_BASED_SEARCH    = YES
    EXTERNAL_SEARCH        = YES

This will make doxygen generate a file called `searchdata.xml` in the output directory (configured with OUTPUT_DIRECTORY). You can change the file name (and location) with the SEARCHDATA_FILE option.

The next step is to put the raw search data into an index for efficient searching. You can use `doxyindexer` for this. Simply run it from the command line:

    doxyindexer searchdata.xml

This will create a directory called `doxysearch.db` with some files in it. By default the directory will be created at the location from which doxyindexer was started, but you can change the directory using the `-o` option.

Copy the `doxysearch.db` directory to the same directory as where the `doxysearch.cgi` is located and rerun the browser test by pointing the browser to

    http://yoursite.com/path/to/cgi/doxysearch.cgi?test

You should now get the following message:

    Test successful.

Now you should be able to search for words and symbols from the HTML output.

## Multi project index

In case you have more than one doxygen project and these projects are related, it may be desirable to allow searching for words in all projects from within the documentation of any of the projects.

To make this possible all that is needed is to combine the search data for all projects into a single index, e.g. for two projects A and B for which the searchdata.xml is generated in directories project_A and project_B run:

    doxyindexer project_A/searchdata.xml project_B/searchdata.xml

and then copy the resulting `doxysearch.db` to the directory where also `doxysearch.cgi` is located.

The `searchdata.xml` file doesn't contain any absolute paths or links, so how can the search results from multiple projects be linked back to the right documentation set? This is where the EXTERNAL_SEARCH_ID and EXTRA_SEARCH_MAPPINGS options come into play.

To be able to identify the different projects, one needs to set a unique ID using EXTERNAL_SEARCH_ID for each project.

To link the search results to the right project, you need to define a mapping per project using the EXTRA_SEARCH_MAPPINGS tag. With this option to can define the mapping from IDs of other projects to the (relative) location of documentation of those projects.

So for projects A and B the relevant part of the configuration file could look as follows:

    project_A/Doxyfile
    ------------------
    EXTERNAL_SEARCH_ID    = A
    EXTRA_SEARCH_MAPPINGS = B=../../project_B/html

for project A and for project B

    project_B/Doxyfile
    ------------------
    EXTERNAL_SEARCH_ID    = B
    EXTRA_SEARCH_MAPPINGS = A=../../project_A/html

with these settings, projects A and B can share the same search database, and the search results will link to the right documentation set.

# Updating the index

When you modify the source code, you should re-run doxygen to get up to date documentation again. When using external searching you also need to update the search index by re-running `doxyindexer`. You could wrap the call to `doxygen` and `doxyindexer` together in a script to make this process easier.

# Programming interface

Previous sections have assumed you use the tools `doxyindexer` and `doxysearch.cgi` to do the indexing and searching, but you could also write your own index and search tools if you like.

For this 3 interfaces are important

- The format of the input for the index tool.
- The format of the input for the search engine.
- The format of the output of search engine.

The next subsections describe these interfaces in more detail.

## Indexer input format

The search data produced by doxygen follows the Solr XML index message format.

The input for the indexer is an XML file, which consists of one `<add>` tag containing multiple `<doc>` tags, which in turn contain multiple `<field>` tags.

Here is an example of one doc node, which contains the search data and meta data for one method:

    <add>
      ...
      <doc>
        <field name="type">function</field>
        <field name="name">QXmlReader::setDTDHandler</field>
        <field name="args">(QXmlDTDHandler *handler)=0</field>
        <field name="tag">qtools.tag</field>
        <field name="url">de/df6/class_q_xml_reader.html#a0b24b1fe26a4c32a8032d68ee14d5dba</field>
        <field name="keywords">setDTDHandler QXmlReader::setDTDHandler QXmlReader</field>
        <field name="text">Sets the DTD handler to handler DTDHandler()</field>
      </doc>
      ...
    </add>

Each field has a name. The following field names are supported:

- type: the type of the search entry; can be one of: source, function, slot, signal, variable, typedef, enum, enumvalue, property, event, related, friend, define, file, namespace, group, package, page, dir
- name: the name of the search entry; for a method this is the qualified name of the method, for a class it is the name of the class, etc.
- args: the parameter list (in case of functions or methods)
- tag: the name of the tag file used for this project.
- url: the (relative) URL to the HTML documentation for this entry.
- keywords: important words that are representative for the entry. When searching for such keyword, this entry should get a higher rank in the search results.
- text: the documentation associated with the item. Note that only words are present, no markup.

Note
  Due to the potentially large size of the XML file, it is recommended to use a SAX based parser to process it.

## Search URL format

When the search engine is invoked from a doxygen generated HTML page, a number of parameters are passed to via the query string.

The following fields are passed:

- q: the query text as entered by the user
- n: the number of search results requested.
- p: the number of search page for which to return the results. Each page has n values.
- cb: the name of the callback function, used for JSON with padding, see the next section.

From the complete list of search results, the range `[n*p - n*(p+1)-1]` should be returned.

Here is an example of how a query looks like.

    http://yoursite.com/path/to/cgi/doxysearch.cgi?q=list&n=20&p=1&cb=dummy

It represents a query for the word 'list' (`q=list`) requesting 20 search results (`n=20`), starting with the result number 20 (`p=1`) and using callback 'dummy' (`cb=dummy`):

Note
  The values are URL encoded so they have to be decoded before they can be used.

## Search results format

When invoking the search engine as shown in the previous subsection, it should reply with the results. The format of the reply is JSON with padding, which is basically a javascript struct wrapped in a function call. The name of function should be the name of the callback (as passed with the cb field in the query).

With the example query as shown the previous subsection the main structure of the reply should look as follows:

    dummy({
      "hits":179,
      "first":20,
      "count":20,
      "page":1,
      "pages":9,
      "query": "list",
      "items":[
      ...
     ]})

The fields have the following meaning:

- hits: the total number of search results (could be more than was requested).
- first: the index of first result returned: .
- count: the actual number of results returned:
- page: the page number of the result:
- pages: the total number of pages: .
- items: an array containing the search data per result.

Here is an example of how the element of the items array should look like:

    {"type": "function",
     "name": "QDir::entryInfoList(const QString &nameFilter, int filterSpec=DefaultFilter, int sortSpec=DefaultSort) const",
     "tag": "qtools.tag",
     "url": "d5/d8d/class_q_dir.html#a9439ea6b331957f38dbad981c4d050ef",
     "fragments":[
       "Returns a <span class=\"hl\">list</span> of QFileInfo objects for all files and directories...",
       "... pointer to a QFileInfoList The <span class=\"hl\">list</span> is owned by the QDir object...",
       "... to keep the entries of the <span class=\"hl\">list</span> after a subsequent call to this..."
     ]
    },

The fields for such an item have the following meaning:

- type: the type of the item, as found in the field with name "type" in the raw search data.
- name: the name of the item, including the parameter list, as found in the fields with name "name" and "args" in the raw search data.
- tag: the name of the tag file, as found in the field with name "tag" in the raw search data.
- url: the name of the (relative) URL to the documentation, as found in the field with name "url" in the raw search data.
- "fragments": an array with 0 or more fragments of text containing words that have been search for. These words should be wrapped in `<span class="hl">` and `</span>` tags to highlight them in the output.