Skip to content

Hello World

Mark Papadakis edited this page May 9, 2017 · 2 revisions

These examples illustrates how you can use Trinity to index documents, how to merge indices and how to run a query. Please read the comments in the respective header files for how this works. Trinity can support on-disk or in-memory index sources(indices, segments), or pretty much anything else as long as you comply with the respective interfaces.

Indexing

#include <google_codec.h>
#include <indexer.h>
#include <segment_index_source.h>

using namespace Trinity;

int main()
{
        // Will use the handy utility class SegmentIndexSession which takes care of
        // most tasks related to indexing for us
        Trinity::SegmentIndexSession sess;
        // A new indexer Google Codec index session
        // Make sure this directory exists i.e mkdir -p /tmp/TrinitySegments/1
        auto indexSession = std::make_unique<Trinity::Codecs::Google::IndexSession>("/tmp/TrinitySegments/1");

        auto doc1 = sess.begin(1); // first document

        doc1.insert("hello", 1);
        doc1.insert("world", 2);

        sess.insert(doc1);

        auto doc2 = sess.begin(2); // second document

        doc2.insert("world", 1);
        doc2.insert("of", 2);
        doc2.insert("warcraft", 3);

        sess.insert(doc2);

        sess.commit(indexSession.get());
        return 0;
}

Search

#include <google_codec.h>
#include <indexer.h>
#include <segment_index_source.h>
#include <queries.h>
#include <matches.h>
#include <exec.h>

using namespace Trinity;

int main(int argc, char *argv[])
{
        query q(strwlen32_t(argv[1], strlen(argv[1])));
        auto ss = new SegmentIndexSource("/tmp/TrinitySegments/1");
        IndexSourcesCollection sources;

        sources.insert(ss);
        ss->Release();
        sources.commit();

        struct handler final
            : public MatchedIndexDocumentsFilter
        {

                ConsiderResponse consider(const matched_document &match) override final
                {
                        // Just print the document ID
                        Print("Matched document ", match.id, "\n");
                        return ConsiderResponse::Continue;
                }
        };

        exec_query<handler>(q, &sources, nullptr);
        return 0;
}

Merge

#include <google_codec.h>
#include <indexer.h>
#include <merge.h>
#include <segment_index_source.h>

using namespace Trinity;

int main(int argc, char *argv[])
{
        MergeCandidatesCollection collection;
        auto outSess = std::make_unique<Trinity::Codecs::Google::IndexSession>("/tmp/TrinitySegments/10");
        std::vector<SegmentIndexSource *> sources;
        simple_allocator a;
        std::vector<std::pair<str8_t, term_index_ctx>> outTerms;
        std::vector<uint32_t> documentIDs;

        sources.push_back(new SegmentIndexSource("/tmp/TrinitySegments/1"));

        for (const auto it : sources)
        {
                auto tv = it->segment_terms()->new_terms_view();

                collection.insert({it->generation(), tv, it->access_proxy(), it->masked_documents()});
        }

        collection.commit();
        collection.merge(outSess.get(), &a, &outTerms);
        outSess->persist_terms(outTerms);
        persist_segment(outSess.get(), documentIDs);

        for (auto it : sources)
                it->Release();

        return 0;
}
Clone this wiki locally