Repository navigation
Expand file tree
/
Copy pathbasic.zig
More file actions
45 lines (40 loc) · 1.73 KB
/
Copy pathbasic.zig
File metadata and controls
45 lines (40 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const std = @import("std");
const zft = @import("zfulltext");
pub fn main(init: std.process.Init) !void {
const io = init.io;
const allocator = init.gpa;
const cwd = std.Io.Dir.cwd();
try cwd.createDirPath(io, "out");
var path: ["out/example-".len + 24]u8 = undefined;
while (true) {
var random: [12]u8 = undefined;
io.random(&random);
path = "out/example-".* ++ std.fmt.bytesToHex(random, .lower);
cwd.createDir(io, &path, .default_dir) catch |err| switch (err) {
error.PathAlreadyExists => continue,
else => return err,
};
break;
}
defer cwd.deleteTree(io, &path) catch {};
const index = try zft.Index.open(allocator, io, &path, .{ .create_if_missing = true });
defer index.deinit();
try index.upsert(1, "Hello from the search example.");
try index.upsert(2, "在中文和 English 中检索。 Search across languages.");
var result = try index.search(allocator, "search 中文", .{ .mode = .any, .limit = 10 });
defer result.deinit(allocator);
const stdout = std.Io.File.stdout();
for (result.hits) |hit| {
const line = try std.fmt.allocPrint(allocator, "match id={d}, score={d:.3}\n", .{ hit.id, hit.score });
defer allocator.free(line);
try stdout.writeStreamingAll(io, line);
}
try index.upsert(1, "Updated searchable text.");
_ = try index.delete(2);
var document = (try index.get(allocator, 1)) orelse return error.MissingDocument;
defer document.deinit(allocator);
const line = try std.fmt.allocPrint(allocator, "document {d}: {s}\n", .{ document.id, document.text });
defer allocator.free(line);
try stdout.writeStreamingAll(io, line);
try index.close();
}