Usage
The 2 examples below are taken from zig-search-engine's README.
const se = @import("search-engine");
// The model: which attributes are searchable, filterable (facets), sortable.
const messages_schema = se.Schema{
.name = "messages",
.searchable = &.{ .{ .name = "subject" }, .{ .name = "body" } },
.filterable = &.{ .{ .name = "username" }, .{ .name = "mailbox" } },
.sortable = &.{ .{ .name = "date", .type = .int64 } },
};
const engine = se.Typesense.init(.{ .api_key = key }); // host/port default to 127.0.0.1:8108
try engine.ensureCollection(allocator, messages_schema);
// Index a document
var doc = se.DocBuilder.init(allocator);
defer doc.deinit();
try doc.putString("id", "msg-1");
try doc.putString("subject", "hello world");
const json = try doc.toOwned();
defer allocator.free(json);
try engine.upsertDocument(allocator, "messages", json);
// Search
var result = try engine.search(allocator, "messages", .{
.query = "hello",
.query_by = "subject,body",
.filter_by = "username:=`chris`",
});
defer result.deinit();
var it = result.hits();
while (it.next()) |hit| {
std.debug.print("{s}\n", .{se.SearchResponse.docString(hit, "subject")});
}
const search_engine = b.dependency("search_engine", .{ .target = target, .optimize = optimize });
exe.root_module.addImport("search-engine", search_engine.module("search-engine"));