int main(int argc, char *argv[]) { // Initialize the library. lucy_bootstrap_parcel(); if (argc != 2) { S_usage_and_exit(argv[0]); } const char *query_c = argv[1]; printf("Searching for: %s\n\n", query_c); String *folder = Str_newf("%s", path_to_index); String *language = Str_newf("en"); Simple *lucy = Simple_new((Obj*)folder, language); String *query_str = Str_newf("%s", query_c); Simple_Search(lucy, query_str, 0, 10); String *title_str = Str_newf("title"); String *url_str = Str_newf("url"); HitDoc *hit; int i = 1; // Loop over search results. while (NULL != (hit = Simple_Next(lucy))) { 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(query_str); DECREF(lucy); DECREF(language); DECREF(folder); return 0; }
static void S_search(IndexSearcher *searcher, const char *query) { printf("Searching for: %s\n", query); // Execute search query. String *query_str = Str_new_from_utf8(query, strlen(query)); Hits *hits = IxSearcher_Hits(searcher, (Obj*)query_str, 0, 10, NULL); String *field_str = Str_newf("title"); HitDoc *hit; int i = 1; // Loop over search results. while (NULL != (hit = Hits_Next(hits))) { String *value_str = (String*)HitDoc_Extract(hit, field_str); char *value = Str_To_Utf8(value_str); printf("Result %d: %s\n", i, value); free(value); DECREF(value_str); DECREF(hit); i++; } printf("\n"); DECREF(query_str); DECREF(hits); DECREF(field_str); }
void Err_warn_mess(String *message) { char *utf8 = Str_To_Utf8(message); fprintf(stderr, "%s", utf8); FREEMEM(utf8); DECREF(message); }
static void test_To_Utf8(TestBatchRunner *runner) { String *string = Str_newf("a%s%sb%sc", smiley, smiley, smiley); char *buf = Str_To_Utf8(string); TEST_TRUE(runner, strcmp(buf, "a" SMILEY SMILEY "b" SMILEY "c") == 0, "To_Utf8"); FREEMEM(buf); DECREF(string); }
static bool S_dir_ok(String *path) { bool retval = false; char *path_ptr = Str_To_Utf8(path); struct stat stat_buf; if (stat(path_ptr, &stat_buf) != -1) { if (stat_buf.st_mode & S_IFDIR) { retval = true; } } FREEMEM(path_ptr); return retval; }
static bool S_create_dir(String *path) { bool retval = true; char *path_ptr = Str_To_Utf8(path); if (-1 == chy_makedir(path_ptr, 0777)) { Err_set_error(Err_new(Str_newf("Couldn't create directory '%o': %s", path, strerror(errno)))); retval = false; } FREEMEM(path_ptr); return retval; }
void TestQPLogic_Run_IMP(TestQueryParserLogic *self, TestBatchRunner *runner) { TestBatchRunner_Plan(runner, (TestBatch*)self, 258); uint32_t i; Folder *folder = S_create_index(); IndexSearcher *searcher = IxSearcher_new((Obj*)folder); QueryParser *or_parser = QParser_new(IxSearcher_Get_Schema(searcher), NULL, NULL, NULL); String *AND = SSTR_WRAP_C("AND"); QueryParser *and_parser = QParser_new(IxSearcher_Get_Schema(searcher), NULL, AND, NULL); QParser_Set_Heed_Colons(or_parser, true); QParser_Set_Heed_Colons(and_parser, true); // Run logical tests with default boolop of OR. for (i = 0; logical_test_funcs[i] != NULL; i++) { LUCY_TestQPLogic_Logical_Test_t test_func = logical_test_funcs[i]; TestQueryParser *test_case_obj = test_func(BOOLOP_OR); TestQueryParserIVARS *test_case = TestQP_IVARS(test_case_obj); Query *tree = QParser_Tree(or_parser, test_case->query_string); Query *parsed = QParser_Parse(or_parser, test_case->query_string); Hits *hits = IxSearcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL); char *qstr = Str_To_Utf8(test_case->query_string); TEST_TRUE(runner, Query_Equals(tree, (Obj*)test_case->tree), "tree() OR %s", qstr); TEST_INT_EQ(runner, Hits_Total_Hits(hits), test_case->num_hits, "hits: OR %s", qstr); free(qstr); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case_obj); } // Run logical tests with default boolop of AND. for (i = 0; logical_test_funcs[i] != NULL; i++) { LUCY_TestQPLogic_Logical_Test_t test_func = logical_test_funcs[i]; TestQueryParser *test_case_obj = test_func(BOOLOP_AND); TestQueryParserIVARS *test_case = TestQP_IVARS(test_case_obj); Query *tree = QParser_Tree(and_parser, test_case->query_string); Query *parsed = QParser_Parse(and_parser, test_case->query_string); Hits *hits = IxSearcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL); char *qstr = Str_To_Utf8(test_case->query_string); TEST_TRUE(runner, Query_Equals(tree, (Obj*)test_case->tree), "tree() AND %s", qstr); TEST_INT_EQ(runner, Hits_Total_Hits(hits), test_case->num_hits, "hits: AND %s", qstr); free(qstr); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case_obj); } // Run tests for QParser_Prune(). for (i = 0; prune_test_funcs[i] != NULL; i++) { LUCY_TestQPLogic_Prune_Test_t test_func = prune_test_funcs[i]; TestQueryParser *test_case_obj = test_func(); TestQueryParserIVARS *test_case = TestQP_IVARS(test_case_obj); String *qstring = test_case->tree ? Query_To_String(test_case->tree) : Str_new_from_trusted_utf8("(NULL)", 6); char *qstr = Str_To_Utf8(qstring); Query *tree = test_case->tree; Query *wanted = test_case->expanded; Query *pruned = QParser_Prune(or_parser, tree); Query *expanded; Hits *hits; TEST_TRUE(runner, Query_Equals(pruned, (Obj*)wanted), "prune() %s", qstr); expanded = QParser_Expand(or_parser, pruned); hits = IxSearcher_Hits(searcher, (Obj*)expanded, 0, 10, NULL); TEST_INT_EQ(runner, Hits_Total_Hits(hits), test_case->num_hits, "hits: %s", qstr); free(qstr); DECREF(hits); DECREF(expanded); DECREF(pruned); DECREF(qstring); DECREF(test_case_obj); } DECREF(and_parser); DECREF(or_parser); DECREF(searcher); DECREF(folder); }
static void S_remove(String *path) { char *str = Str_To_Utf8(path); remove(str); free(str); }