Query* QParser_Parse_IMP(QueryParser *self, String *query_string) { String *qstring = query_string ? Str_Clone(query_string) : Str_new_from_trusted_utf8("", 0); Query *tree = QParser_Tree(self, qstring); Query *expanded = QParser_Expand(self, tree); Query *pruned = QParser_Prune(self, expanded); DECREF(expanded); DECREF(tree); DECREF(qstring); return pruned; }
Query* QParser_Expand_IMP(QueryParser *self, Query *query) { Query *retval = NULL; if (Query_is_a(query, LEAFQUERY)) { retval = QParser_Expand_Leaf(self, query); } else if (Query_is_a(query, ORQUERY) || Query_is_a(query, ANDQUERY)) { PolyQuery *polyquery = (PolyQuery*)query; Vector *children = PolyQuery_Get_Children(polyquery); Vector *new_kids = Vec_new(Vec_Get_Size(children)); for (uint32_t i = 0, max = Vec_Get_Size(children); i < max; i++) { Query *child = (Query*)Vec_Fetch(children, i); Query *new_child = QParser_Expand(self, child); // recurse if (new_child) { if (Query_is_a(new_child, NOMATCHQUERY)) { bool fails = NoMatchQuery_Get_Fails_To_Match( (NoMatchQuery*)new_child); if (fails) { Vec_Push(new_kids, (Obj*)new_child); } else { DECREF(new_child); } } else { Vec_Push(new_kids, (Obj*)new_child); } } } if (Vec_Get_Size(new_kids) == 0) { retval = (Query*)NoMatchQuery_new(); } else if (Vec_Get_Size(new_kids) == 1) { retval = (Query*)INCREF(Vec_Fetch(new_kids, 0)); } else { PolyQuery_Set_Children(polyquery, new_kids); retval = (Query*)INCREF(query); } DECREF(new_kids); } else if (Query_is_a(query, NOTQUERY)) { NOTQuery *not_query = (NOTQuery*)query; Query *negated_query = NOTQuery_Get_Negated_Query(not_query); negated_query = QParser_Expand(self, negated_query); if (negated_query) { NOTQuery_Set_Negated_Query(not_query, negated_query); DECREF(negated_query); retval = (Query*)INCREF(query); } else { retval = (Query*)MatchAllQuery_new(); } } else if (Query_is_a(query, REQUIREDOPTIONALQUERY)) { RequiredOptionalQuery *req_opt_query = (RequiredOptionalQuery*)query; Query *req_query = ReqOptQuery_Get_Required_Query(req_opt_query); Query *opt_query = ReqOptQuery_Get_Optional_Query(req_opt_query); req_query = QParser_Expand(self, req_query); opt_query = QParser_Expand(self, opt_query); if (req_query && opt_query) { ReqOptQuery_Set_Required_Query(req_opt_query, req_query); ReqOptQuery_Set_Optional_Query(req_opt_query, opt_query); retval = (Query*)INCREF(query); } else if (req_query) { retval = (Query*)INCREF(req_query); } else if (opt_query) { retval = (Query*)INCREF(opt_query); } else { retval = (Query*)NoMatchQuery_new(); } DECREF(opt_query); DECREF(req_query); } else { retval = (Query*)INCREF(query); } return retval; }
void TestQPLogic_run_tests() { u32_t i; TestBatch *batch = Test_new_batch("TestQueryParserLogic", 178, NULL); Folder *folder = S_create_index(); Searcher *searcher = Searcher_new((Obj*)folder); QueryParser *or_parser = QParser_new(Searcher_Get_Schema(searcher), NULL, NULL, NULL); static ZombieCharBuf AND = ZCB_LITERAL("AND"); QueryParser *and_parser = QParser_new(Searcher_Get_Schema(searcher), NULL, (CharBuf*)&AND, NULL); QParser_Set_Heed_Colons(or_parser, true); QParser_Set_Heed_Colons(and_parser, true); PLAN(batch); /* Run logical tests with default boolop of OR. */ for (i = 0; logical_test_funcs[i] != NULL; i++) { kino_TestQPLogic_logical_test_t test_func = logical_test_funcs[i]; TestQueryParser *test_case = test_func(BOOLOP_OR); Query *tree = QParser_Tree(or_parser, test_case->query_string); Query *parsed = QParser_Parse(or_parser, test_case->query_string); Hits *hits = Searcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL); ASSERT_TRUE(batch, Query_Equals(tree, (Obj*)test_case->tree), "tree() OR %s", test_case->query_string->ptr); ASSERT_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: OR %s", test_case->query_string->ptr); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case); } /* Run logical tests with default boolop of AND. */ for (i = 0; logical_test_funcs[i] != NULL; i++) { kino_TestQPLogic_logical_test_t test_func = logical_test_funcs[i]; TestQueryParser *test_case = test_func(BOOLOP_AND); Query *tree = QParser_Tree(and_parser, test_case->query_string); Query *parsed = QParser_Parse(and_parser, test_case->query_string); Hits *hits = Searcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL); ASSERT_TRUE(batch, Query_Equals(tree, (Obj*)test_case->tree), "tree() AND %s", test_case->query_string->ptr); ASSERT_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: AND %s", test_case->query_string->ptr); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case); } /* Run tests for QParser_Prune(). */ for (i = 0; prune_test_funcs[i] != NULL; i++) { kino_TestQPLogic_prune_test_t test_func = prune_test_funcs[i]; TestQueryParser *test_case = test_func(); CharBuf *qstring = test_case->tree ? Obj_To_String(test_case->tree) : CB_new_from_trusted_utf8("(NULL)", 6); Query *tree = test_case->tree; Query *wanted = test_case->expanded; Query *pruned = QParser_Prune(or_parser, tree); Query *expanded; Hits *hits; ASSERT_TRUE(batch, Query_Equals(pruned, (Obj*)wanted), "prune() %s", qstring->ptr); expanded = QParser_Expand(or_parser, pruned); hits = Searcher_Hits(searcher, (Obj*)expanded, 0, 10, NULL); ASSERT_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: %s", qstring->ptr); DECREF(hits); DECREF(expanded); DECREF(pruned); DECREF(qstring); DECREF(test_case); } DECREF(and_parser); DECREF(or_parser); DECREF(searcher); DECREF(folder); batch->destroy(batch); }
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); }
void TestQPLogic_run_tests() { uint32_t i; TestBatch *batch = TestBatch_new(258); Folder *folder = S_create_index(); IndexSearcher *searcher = IxSearcher_new((Obj*)folder); QueryParser *or_parser = QParser_new(IxSearcher_Get_Schema(searcher), NULL, NULL, NULL); ZombieCharBuf *AND = ZCB_WRAP_STR("AND", 3); QueryParser *and_parser = QParser_new(IxSearcher_Get_Schema(searcher), NULL, (CharBuf*)AND, NULL); QParser_Set_Heed_Colons(or_parser, true); QParser_Set_Heed_Colons(and_parser, true); TestBatch_Plan(batch); // 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 = test_func(BOOLOP_OR); 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); TEST_TRUE(batch, Query_Equals(tree, (Obj*)test_case->tree), "tree() OR %s", (char*)CB_Get_Ptr8(test_case->query_string)); TEST_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: OR %s", (char*)CB_Get_Ptr8(test_case->query_string)); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case); } // 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 = test_func(BOOLOP_AND); 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); TEST_TRUE(batch, Query_Equals(tree, (Obj*)test_case->tree), "tree() AND %s", (char*)CB_Get_Ptr8(test_case->query_string)); TEST_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: AND %s", (char*)CB_Get_Ptr8(test_case->query_string)); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case); } // 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 = test_func(); CharBuf *qstring = test_case->tree ? Query_To_String(test_case->tree) : CB_new_from_trusted_utf8("(NULL)", 6); Query *tree = test_case->tree; Query *wanted = test_case->expanded; Query *pruned = QParser_Prune(or_parser, tree); Query *expanded; Hits *hits; TEST_TRUE(batch, Query_Equals(pruned, (Obj*)wanted), "prune() %s", (char*)CB_Get_Ptr8(qstring)); expanded = QParser_Expand(or_parser, pruned); hits = IxSearcher_Hits(searcher, (Obj*)expanded, 0, 10, NULL); TEST_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: %s", (char*)CB_Get_Ptr8(qstring)); DECREF(hits); DECREF(expanded); DECREF(pruned); DECREF(qstring); DECREF(test_case); } DECREF(and_parser); DECREF(or_parser); DECREF(searcher); DECREF(folder); DECREF(batch); }