public function testTermQuery()
 {
     $objSearchTerm = new class_module_search_term("blub");
     $objSearchQuery = new class_module_search_term_query($objSearchTerm);
     $arrResults = $this->getResultsFromQuery($objSearchQuery);
     $this->assertEquals(count($arrResults), 1, "simple term query (blub)");
     $this->assertEquals($this->getCountFromQuery($objSearchQuery), 1);
     $objMetadataFilter = new class_module_search_metadata_filter();
     $objMetadataFilter->setFilterModules(array(_search_module_id_));
     $objSearchQuery->setMetadataFilter($objMetadataFilter);
     $arrResults = $this->getResultsFromQuery($objSearchQuery);
     $this->assertEquals(count($arrResults), 0, "metadata filter (blub)");
     $this->assertEquals($this->getCountFromQuery($objSearchQuery), 0);
     $objMetadataFilter->setFilterModules(array(_system_modul_id_, _search_module_id_));
     $objSearchQuery->setMetadataFilter($objMetadataFilter);
     $arrResults = $this->getResultsFromQuery($objSearchQuery);
     $this->assertEquals(count($arrResults), 1, "metadata filter (blub)");
     $this->assertEquals($this->getCountFromQuery($objSearchQuery), 1);
     $objParser = new class_module_search_query_parser();
     $objQuery = $objParser->parseText("subtitle:blub");
     $arrResult = $this->getResultsFromQuery($objQuery);
     $this->assertEquals(count($arrResult), 1, "count error (subtitle:blub)");
     $this->assertEquals($arrResult[0]["score"], 1, "field filter scoring (subtitle:blub)");
     $this->assertEquals($this->getCountFromQuery($objSearchQuery), 1);
     $objQuery = $objParser->parseText("title:hallo");
     $arrResult = $this->getResultsFromQuery($objQuery);
     $this->assertEquals(count($arrResult), 2);
     $this->assertEquals($arrResult[0]["score"], 1, "field filter scoring (title:hallo)");
     $this->assertEquals($this->getCountFromQuery($objQuery), 2);
     $objQuery = $objParser->parseText("ipsum");
     $arrResult = $this->getResultsFromQuery($objQuery);
     $this->assertEquals(count($arrResult), 1);
     $this->assertEquals($arrResult[0]["score"], 3, "field filter scoring (ipsum)");
     $this->assertEquals($this->getCountFromQuery($objQuery), 1);
 }
 public function testQueryParser()
 {
     echo "Test Query Parser\n";
     $objParser = new class_module_search_query_parser();
     // Must
     echo "Must...\n";
     $objQuery = $objParser->parseText("hello");
     /** @var $objQuery class_module_search_term_query */
     $this->assertTrue($objQuery instanceof class_module_search_term_query, "wrong query type");
     $this->assertEquals($objQuery->getObjTerm()->getStrText(), "hello");
     echo "Must...\n";
     $objQuery = $objParser->parseText("glückwunsch");
     /** @var $objQuery class_module_search_term_query */
     $this->assertTrue($objQuery instanceof class_module_search_term_query, "wrong query type");
     $this->assertEquals($objQuery->getObjTerm()->getStrText(), "glückwunsch");
     // Must - Must
     echo "Must - Must...\n";
     $objQuery = $objParser->parseText("hello world");
     $this->assertTrue($objQuery instanceof class_module_search_boolean_query, "wrong query type");
     if ($objQuery instanceof class_module_search_boolean_query) {
         /** @var $objQuery class_module_search_boolean_query */
         $this->assertEquals(count($objQuery->getMustOccurs()), 2);
         $this->assertEquals(count($objQuery->getMustNotOccurs()), 0);
         $this->assertEquals(count($objQuery->getShouldNotOccurs()), 0);
         $this->assertEquals(count($objQuery->getMustNotOccurs()), 0);
         $this->assertEquals($objQuery->getMustOccurs()[0]->getStrText(), "hello");
         $this->assertEquals($objQuery->getMustOccurs()[1]->getStrText(), "world");
     }
     // Must - Should
     echo "Must - Should...\n";
     $objQuery = $objParser->parseText("+hello world");
     $this->assertTrue($objQuery instanceof class_module_search_boolean_query, "wrong query type");
     if ($objQuery instanceof class_module_search_boolean_query) {
         /** @var $objQuery class_module_search_boolean_query */
         $this->assertEquals(count($objQuery->getMustOccurs()), 1);
         $this->assertEquals(count($objQuery->getShouldOccurs()), 1);
         $this->assertEquals(count($objQuery->getMustNotOccurs()), 0);
         $this->assertEquals(count($objQuery->getShouldNotOccurs()), 0);
         $this->assertEquals($objQuery->getMustOccurs()[0]->getStrText(), "hello");
         $this->assertEquals($objQuery->getShouldOccurs()[0]->getStrText(), "world");
     }
     // Must - Must - Should
     echo "Must - Must - Should...\n";
     $objQuery = $objParser->parseText("+hello +world blub");
     $this->assertTrue($objQuery instanceof class_module_search_boolean_query, "wrong query type");
     if ($objQuery instanceof class_module_search_boolean_query) {
         /** @var $objQuery class_module_search_boolean_query */
         $this->assertEquals(count($objQuery->getMustOccurs()), 2);
         $this->assertEquals(count($objQuery->getShouldOccurs()), 1);
         $this->assertEquals(count($objQuery->getMustNotOccurs()), 0);
         $this->assertEquals(count($objQuery->getShouldNotOccurs()), 0);
         $this->assertEquals($objQuery->getMustOccurs()[0]->getStrText(), "hello");
         $this->assertEquals($objQuery->getMustOccurs()[1]->getStrText(), "world");
         $this->assertEquals($objQuery->getShouldOccurs()[0]->getStrText(), "blub");
     }
     // Must - Must - MustNot
     echo "Must - Must - MustNot...\n";
     $objQuery = $objParser->parseText("+hello +world -blub");
     $this->assertTrue($objQuery instanceof class_module_search_boolean_query, "wrong query type");
     if ($objQuery instanceof class_module_search_boolean_query) {
         /** @var $objQuery class_module_search_boolean_query */
         $this->assertEquals(count($objQuery->getMustOccurs()), 2);
         $this->assertEquals(count($objQuery->getShouldOccurs()), 0);
         $this->assertEquals(count($objQuery->getMustNotOccurs()), 1);
         $this->assertEquals(count($objQuery->getShouldNotOccurs()), 0);
         $this->assertEquals($objQuery->getMustOccurs()[0]->getStrText(), "hello");
         $this->assertEquals($objQuery->getMustOccurs()[1]->getStrText(), "world");
         $this->assertEquals($objQuery->getMustNotOccurs()[0]->getStrText(), "blub");
     }
     // Must - MustNot
     echo "Must - MustNot...\n";
     $objQuery = $objParser->parseText("hello -world");
     $this->assertTrue($objQuery instanceof class_module_search_boolean_query, "wrong query type");
     if ($objQuery instanceof class_module_search_boolean_query) {
         /** @var $objQuery class_module_search_boolean_query */
         $this->assertEquals(count($objQuery->getMustOccurs()), 1);
         $this->assertEquals(count($objQuery->getMustNotOccurs()), 1);
         $this->assertEquals(count($objQuery->getShouldOccurs()), 0);
         $this->assertEquals(count($objQuery->getShouldNotOccurs()), 0);
         $this->assertEquals($objQuery->getMustOccurs()[0]->getStrText(), "hello");
         $this->assertEquals($objQuery->getMustNotOccurs()[0]->getStrText(), "world");
     }
 }
 /**
  * Counts the number of hits
  *
  * @param class_module_search_search $objSearch
  *
  * @return int
  */
 public function getIndexedSearchCount($objSearch)
 {
     $objParser = new class_module_search_query_parser();
     $objSearchQuery = $objParser->parseText($objSearch->getStrQuery());
     if ($objSearchQuery == null) {
         return 0;
     }
     $objSearchQuery->setMetadataFilter($this->getMetadataFilterFromSearch($objSearch));
     $strQuery = "";
     $arrParameters = array();
     $objSearchQuery->getCountQuery($strQuery, $arrParameters);
     $arrSearchResult = $this->objDB->getPRow($strQuery, $arrParameters);
     return $arrSearchResult["COUNT(*)"];
 }
 public function testBooleanQuery()
 {
     $objSearchTerm1 = new class_module_search_term("hallo");
     $objSearchTerm2 = new class_module_search_term("welt");
     $objBooleanSearch = new class_module_search_boolean_query();
     $objBooleanSearch->add($objSearchTerm1, class_module_search_boolean_query::BOOLEAN_CLAUSE_OCCUR_MUST);
     $objBooleanSearch->add($objSearchTerm2, class_module_search_boolean_query::BOOLEAN_CLAUSE_OCCUR_MUST);
     $arrResult = $this->getResultsFromQuery($objBooleanSearch);
     $this->assertEquals(count($arrResult), 2);
     $this->assertEquals($arrResult[0]["score"], 2);
     $this->assertEquals($this->getCountFromQuery($objBooleanSearch), 2);
     // One more optional search term
     $objSearchTerm3 = new class_module_search_term("blub");
     $objBooleanSearch->add($objSearchTerm3, class_module_search_boolean_query::BOOLEAN_CLAUSE_OCCUR_SHOULD);
     $arrResult = $this->getResultsFromQuery($objBooleanSearch);
     $this->assertEquals(count($arrResult), 2);
     $this->assertEquals($arrResult[0]["score"], 4);
     $this->assertEquals($arrResult[1]["score"], 2);
     $this->assertEquals($this->getCountFromQuery($objBooleanSearch), 2);
     // Module metadata filter
     $objMetadataFilter = new class_module_search_metadata_filter();
     $objMetadataFilter->setFilterModules(array(_system_modul_id_));
     $objBooleanSearch->setMetadataFilter($objMetadataFilter);
     $arrResult = $this->getResultsFromQuery($objBooleanSearch);
     $this->assertEquals(count($arrResult), 1, "Metadata module filter");
     $this->assertEquals($arrResult[0]["score"], 4);
     $this->assertEquals($this->getCountFromQuery($objBooleanSearch), 1);
     $objMetadataFilter->setFilterModules(array(_system_modul_id_, _search_module_id_));
     $objBooleanSearch->setMetadataFilter($objMetadataFilter);
     $arrResult = $this->getResultsFromQuery($objBooleanSearch);
     $this->assertEquals(count($arrResult), 2, "Metadata module filter");
     $this->assertEquals($this->getCountFromQuery($objBooleanSearch), 2);
     // must must mustNot Search
     $objParser = new class_module_search_query_parser();
     $objQuery = $objParser->parseText("+hallo +welt -blub");
     $arrResult = $this->getResultsFromQuery($objQuery);
     $this->assertEquals(count($arrResult), 1);
     $this->assertEquals($this->getCountFromQuery($objQuery), 1);
     $objParser = new class_module_search_query_parser();
     $objQuery = $objParser->parseText("+hallo +subtitle:blub");
     $arrResult = $this->getResultsFromQuery($objQuery);
     $this->assertEquals(count($arrResult), 1);
     $this->assertEquals($arrResult[0]["score"], 2, "field filter scoring");
     $this->assertEquals($this->getCountFromQuery($objQuery), 1);
     $objQuery = $objParser->parseText("title:hallo");
     $arrResult = $this->getResultsFromQuery($objQuery);
     $this->assertEquals(count($arrResult), 2);
     $this->assertEquals($arrResult[0]["score"], 1, "field filter scoring");
     $this->assertEquals($this->getCountFromQuery($objQuery), 2);
     //test new Scoring
     $objQuery = $objParser->parseText("lorem ipsum");
     $arrResult = $this->getResultsFromQuery($objQuery);
     $this->assertEquals(count($arrResult), 1);
     $this->assertEquals($arrResult[0]["score"], 4, "'lorem ipsum' scoring");
     $this->assertEquals($this->getCountFromQuery($objQuery), 1);
 }