void TestQPSyntax_run_tests(Folder *index) { uint32_t i; TestBatch *batch = TestBatch_new(58); IndexSearcher *searcher = IxSearcher_new((Obj*)index); QueryParser *qparser = QParser_new(IxSearcher_Get_Schema(searcher), NULL, NULL, NULL); QParser_Set_Heed_Colons(qparser, true); TestBatch_Plan(batch); for (i = 0; leaf_test_funcs[i] != NULL; i++) { kino_TestQPSyntax_test_t test_func = leaf_test_funcs[i]; TestQueryParser *test_case = test_func(); Query *tree = QParser_Tree(qparser, test_case->query_string); Query *expanded = QParser_Expand_Leaf(qparser, test_case->tree); Query *parsed = QParser_Parse(qparser, 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() %s", (char*)CB_Get_Ptr8(test_case->query_string)); TEST_TRUE(batch, Query_Equals(expanded, (Obj*)test_case->expanded), "expand_leaf() %s", (char*)CB_Get_Ptr8(test_case->query_string)); TEST_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: %s", (char*)CB_Get_Ptr8(test_case->query_string)); DECREF(hits); DECREF(parsed); DECREF(expanded); DECREF(tree); DECREF(test_case); } for (i = 0; syntax_test_funcs[i] != NULL; i++) { kino_TestQPSyntax_test_t test_func = syntax_test_funcs[i]; TestQueryParser *test_case = test_func(); Query *tree = QParser_Tree(qparser, test_case->query_string); Query *parsed = QParser_Parse(qparser, 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() %s", (char*)CB_Get_Ptr8(test_case->query_string)); TEST_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: %s", (char*)CB_Get_Ptr8(test_case->query_string)); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case); } DECREF(batch); DECREF(searcher); DECREF(qparser); }
void TestQPSyntax_run_tests(Folder *index) { u32_t i; TestBatch *batch = Test_new_batch("TestQueryParserSyntax", 48, NULL); Searcher *searcher = Searcher_new((Obj*)index); QueryParser *qparser = QParser_new(Searcher_Get_Schema(searcher), NULL, NULL, NULL); QParser_Set_Heed_Colons(qparser, true); PLAN(batch); for (i = 0; leaf_test_funcs[i] != NULL; i++) { kino_TestQPSyntax_test_t test_func = leaf_test_funcs[i]; TestQueryParser *test_case = test_func(i); Query *tree = QParser_Tree(qparser, test_case->query_string); Query *expanded = QParser_Expand_Leaf(qparser, test_case->tree); Query *parsed = QParser_Parse(qparser, 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() %s", test_case->query_string->ptr); ASSERT_TRUE(batch, Query_Equals(expanded, (Obj*)test_case->expanded), "expand_leaf() %s", test_case->query_string->ptr); ASSERT_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: %s", test_case->query_string->ptr); DECREF(hits); DECREF(parsed); DECREF(expanded); DECREF(tree); DECREF(test_case); } for (i = 0; syntax_test_funcs[i] != NULL; i++) { kino_TestQPSyntax_test_t test_func = syntax_test_funcs[i]; TestQueryParser *test_case = test_func(i); Query *tree = QParser_Tree(qparser, test_case->query_string); Query *parsed = QParser_Parse(qparser, 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() %s", test_case->query_string->ptr); ASSERT_INT_EQ(batch, Hits_Total_Hits(hits), test_case->num_hits, "hits: %s", test_case->query_string->ptr); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case); } batch->destroy(batch); DECREF(searcher); DECREF(qparser); }
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; }
static void test_query_parser_syntax(TestBatchRunner *runner) { if (!RegexTokenizer_is_available()) { for (uint32_t i = 0; leaf_test_funcs[i] != NULL; i++) { SKIP(runner, 3, "RegexTokenizer not available"); } for (uint32_t i = 0; syntax_test_funcs[i] != NULL; i++) { SKIP(runner, 2, "RegexTokenizer not available"); } return; } Folder *index = build_index(); IndexSearcher *searcher = IxSearcher_new((Obj*)index); QueryParser *qparser = QParser_new(IxSearcher_Get_Schema(searcher), NULL, NULL, NULL); QParser_Set_Heed_Colons(qparser, true); for (uint32_t i = 0; leaf_test_funcs[i] != NULL; i++) { LUCY_TestQPSyntax_Test_t test_func = leaf_test_funcs[i]; TestQueryParser *test_case = test_func(); TestQueryParserIVARS *ivars = TestQP_IVARS(test_case); Query *tree = QParser_Tree(qparser, ivars->query_string); Query *expanded = QParser_Expand_Leaf(qparser, ivars->tree); Query *parsed = QParser_Parse(qparser, ivars->query_string); Hits *hits = IxSearcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL); TEST_TRUE(runner, Query_Equals(tree, (Obj*)ivars->tree), "tree() %s", Str_Get_Ptr8(ivars->query_string)); TEST_TRUE(runner, Query_Equals(expanded, (Obj*)ivars->expanded), "expand_leaf() %s", Str_Get_Ptr8(ivars->query_string)); TEST_INT_EQ(runner, Hits_Total_Hits(hits), ivars->num_hits, "hits: %s", Str_Get_Ptr8(ivars->query_string)); DECREF(hits); DECREF(parsed); DECREF(expanded); DECREF(tree); DECREF(test_case); } for (uint32_t i = 0; syntax_test_funcs[i] != NULL; i++) { LUCY_TestQPSyntax_Test_t test_func = syntax_test_funcs[i]; TestQueryParser *test_case = test_func(); TestQueryParserIVARS *ivars = TestQP_IVARS(test_case); Query *tree = QParser_Tree(qparser, ivars->query_string); Query *parsed = QParser_Parse(qparser, ivars->query_string); Hits *hits = IxSearcher_Hits(searcher, (Obj*)parsed, 0, 10, NULL); TEST_TRUE(runner, Query_Equals(tree, (Obj*)ivars->tree), "tree() %s", Str_Get_Ptr8(ivars->query_string)); TEST_INT_EQ(runner, Hits_Total_Hits(hits), ivars->num_hits, "hits: %s", Str_Get_Ptr8(ivars->query_string)); DECREF(hits); DECREF(parsed); DECREF(tree); DECREF(test_case); } DECREF(searcher); DECREF(qparser); DECREF(index); }