This project has retired. For details please refer to its Attic page.
Lucy::Docs::Tutorial::BeyondSimpleTutorial
Apache Lucy™

A more flexible app structure.

Goal

In this tutorial chapter, we’ll refactor the apps we built in SimpleTutorial so that they look exactly the same from the end user’s point of view, but offer the developer greater possibilites for expansion.

To achieve this, we’ll ditch Lucy::Simple and replace it with the classes that it uses internally:

Adaptations to indexer.pl

After we load our modules…

#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CFISH_USE_SHORT_NAMES
#define LUCY_USE_SHORT_NAMES
#include "Clownfish/String.h"
#include "Lucy/Analysis/EasyAnalyzer.h"
#include "Lucy/Document/Doc.h"
#include "Lucy/Index/Indexer.h"
#include "Lucy/Plan/FullTextType.h"
#include "Lucy/Plan/StringType.h"
#include "Lucy/Plan/Schema.h"

const char path_to_index[] = "/path/to/index";
const char uscon_source[]  = "/usr/local/apache2/htdocs/us_constitution";

… the first item we’re going need is a Schema.

The primary job of a Schema is to specify what fields are available and how they’re defined. We’ll start off with three fields: title, content and url.

static Schema*
S_create_schema() {
    // Create a new schema.
    Schema *schema = Schema_new();

    // Create an analyzer.
    String       *language = Str_newf("en");
    EasyAnalyzer *analyzer = EasyAnalyzer_new(language);

    // Specify fields.

    FullTextType *type = FullTextType_new((Analyzer*)analyzer);

    {
        String *field_str = Str_newf("title");
        Schema_Spec_Field(schema, field_str, (FieldType*)type);
        DECREF(field_str);
    }

    {
        String *field_str = Str_newf("content");
        Schema_Spec_Field(schema, field_str, (FieldType*)type);
        DECREF(field_str);
    }

    {
        String *field_str = Str_newf("url");
        Schema_Spec_Field(schema, field_str, (FieldType*)type);
        DECREF(field_str);
    }

    DECREF(type);
    DECREF(analyzer);
    DECREF(language);
    return schema;
}

All of the fields are spec’d out using the FullTextType FieldType, indicating that they will be searchable as “full text” – which means that they can be searched for individual words. The “analyzer”, which is unique to FullTextType fields, is what breaks up the text into searchable tokens.

Next, we’ll swap our Lucy::Simple object out for an Indexer. The substitution will be straightforward because Simple has merely been serving as a thin wrapper around an inner Indexer, and we’ll just be peeling away the wrapper.

First, replace the constructor:

int
main() {
    // Initialize the library.
    lucy_bootstrap_parcel();

    Schema *schema = S_create_schema();
    String *folder = Str_newf("%s", path_to_index);

    Indexer *indexer = Indexer_new(schema, (Obj*)folder, NULL,
                                   Indexer_CREATE | Indexer_TRUNCATE);

Next, have the indexer object Add_Doc() where we were having the lucy object adding the document before:

    DIR *dir = opendir(uscon_source);
    if (dir == NULL) {
        perror(uscon_source);
        return 1;
    }

    for (struct dirent *entry = readdir(dir);
         entry;
         entry = readdir(dir)) {

        if (S_ends_with(entry->d_name, ".txt")) {
            Doc *doc = S_parse_file(entry->d_name);
            Indexer_Add_Doc(indexer, doc, 1.0);
            DECREF(doc);
        }
    }

    closedir(dir);

There’s only one extra step required: at the end of the app, you must call commit() explicitly to close the indexing session and commit your changes. (Lucy::Simple hides this detail, calling commit() implicitly when it needs to).

    Indexer_Commit(indexer);

    DECREF(indexer);
    DECREF(folder);
    DECREF(schema);
    return 0;
}

Adaptations to search.cgi

In our search app as in our indexing app, Lucy::Simple has served as a thin wrapper – this time around IndexSearcher and Hits. Swapping out Simple for these two classes is also straightforward:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CFISH_USE_SHORT_NAMES
#define LUCY_USE_SHORT_NAMES
#include "Clownfish/String.h"
#include "Lucy/Document/HitDoc.h"
#include "Lucy/Search/Hits.h"
#include "Lucy/Search/IndexSearcher.h"

const char path_to_index[] = "/path/to/index";

int
main(int argc, char *argv[]) {
    // Initialize the library.
    lucy_bootstrap_parcel();

    if (argc < 2) {
        printf("Usage: %s <querystring>\n", argv[0]);
        return 0;
    }

    const char *query_c = argv[1];

    printf("Searching for: %s\n\n", query_c);

    String        *folder   = Str_newf("%s", path_to_index);
    IndexSearcher *searcher = IxSearcher_new((Obj*)folder);

    String *query_str = Str_newf("%s", query_c);
    Hits *hits = IxSearcher_Hits(searcher, (Obj*)query_str, 0, 10, NULL);

    String *title_str = Str_newf("title");
    String *url_str   = Str_newf("url");
    HitDoc *hit;
    int i = 1;

    // Loop over search results.
    while (NULL != (hit = Hits_Next(hits))) {
        String *title = (String*)HitDoc_Extract(hit, title_str);
        char *title_c = Str_To_Utf8(title);

        String *url = (String*)HitDoc_Extract(hit, url_str);
        char *url_c = Str_To_Utf8(url);

        printf("Result %d: %s (%s)\n", i, title_c, url_c);

        free(url_c);
        free(title_c);
        DECREF(url);
        DECREF(title);
        DECREF(hit);
        i++;
    }

    DECREF(url_str);
    DECREF(title_str);
    DECREF(hits);
    DECREF(query_str);
    DECREF(searcher);
    DECREF(folder);
    return 0;
}

Hooray!

Congratulations! Your apps do the same thing as before… but now they’ll be easier to customize.

In our next chapter, FieldTypeTutorial, we’ll explore how to assign different behaviors to different fields.