tao-test/app/vendor/oat-sa/lib-generis-search/doc/usage.md

12 KiB

lib-generis-search

service basic usage :

criteria basic search :

search every items with label equal to 'foo'.

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query()
$myquery = $queryBuilder->newQuery()->add(RDFS_LABEL)->equals('foo');
$queryBuilder->setCriteria($myquery);
$result = $search->getGateway()->search($queryBuilder);

search by type :

search every test-takers.

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query();
$query = $search->searchType( $queryBuilder , 'http://www.tao.lu/Ontologies/TAOSubject.rdf#Subject' , true);
$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder);

add criteria : search every test-takers having label containing 'foo'.

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query();
$query = $search->searchType($queryBuilder , 'http://www.tao.lu/Ontologies/TAOSubject.rdf#Subject' , true)
                 ->add(RDFS_LABEL)
                 ->contains('foo');
$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder);

language search :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query();
$query = $search->setLanguage($queryBuilder , $userLanguage , $defaultLanguage)
                 ->add(RDFS_LABEL)
                 ->contains('foo');
$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder);

multiple criteria search :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query();
$query = $queryBuilder->newQuery()
                 ->add(RDFS_LABEL)
                 ->contains('foo')
                 ->add(RDFS_COMMENT)
                 ->contains('bar');

$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder);

multiple values on same criterion search :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query();
$query = $queryBuilder->newQuery()
                 ->add(RDFS_LABEL)
                 ->contains('foo')
                 ->addOr('bar');
$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder);

using different operator on same criterion :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query();
$query = $queryBuilder->newQuery()
                 ->add(RDFS_LABEL)
                 ->contains('foo')
                 ->addAnd('bar' , SupportedOperatorHelper::BEGIN);
$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder);

use OR :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

$queryBuilder = $search->query();

$query = $queryBuilder->newQuery()
                 ->add(RDFS_LABEL)
                 ->begin('a')

$queryOr = $queryBuilder->newQuery()
                 ->add(RDFS_LABEL)
                 ->begin('z');

$queryBuilder->setCriteria($query)->setOr($queryOr);
$result = $search->getGateway()->search($queryBuilder);
$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

$queryBuilder = $search->query();

/**
* search for anything with a label begins by a AND comment contains "foo" and comment contains "bar" 
* OR
* anything with a label begins by b AND comment contains "titi" and comment contains "toto" 
*
**/
$queryBuilder = $search->query();

$query = $queryBuilder->newQuery()
                 ->add(RDFS_LABEL)->begin('a')
                 ->add(RDFS_COMMENT)->contains('foo')  
                 ->add(RDFS_COMMENT)->contains('bar')          

$queryOr = $queryBuilder->newQuery()
                 ->add(RDFS_LABEL)->begin('z')
                 ->add(RDFS_COMMENT)->contains('titi')  
                 ->add(RDFS_COMMENT)->contains('toto');

$queryBuilder->setCriteria($query)->setOr($queryOr);
$result = $search->getGateway()->search($queryBuilder);

by default, Criteria are grouped by AND operator :

to search test takers which label begin by 'a' or begin by 'z' :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

/**
* a test taker with label begins by 'a'
*
* or
*
* a test taker with comment begins by 'b'
**/

$queryBuilder = $search->query();

$query = $search->searchType($queryBuilder ,'http://www.tao.lu/Ontologies/TAOSubject.rdf#Subject' , true)
                 ->add(RDFS_LABEL)
                 ->begin('a');

$queryOr = $search->searchType( $queryBuilder ,'http://www.tao.lu/Ontologies/TAOSubject.rdf#Subject' , true)
                 ->add(RDFS_COMMENT)
                 ->begin('z');

$queryBuilder->setCriteria($query)->setOr($queryOr);

$result = $search->getGateway()->search($queryBuilder);
$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

/**
* a test taker with label begin by 'a' or label begin by 'b'
**/

$queryBuilder = $search->query();

$query = $search->searchType($queryBuilder ,'http://www.tao.lu/Ontologies/TAOSubject.rdf#Subject' , true)
                 ->add(RDFS_LABEL)
                 ->begin('a')
                 ->addOr('b');

$queryBuilder->setCriteria($query);

$result = $search->getGateway()->search($queryBuilder);

supported operators :

operators list :

Constant string value SQL operator Comment
EQUAL  'equals'  '='
DIFFERENT  'notEquals'  '!='
GREATER_THAN  'gt'  '>'
GREATER_THAN_EQUAL  'gte'  '>='
LESSER_THAN  'lt'  '<'
LESSER_THAN_EQUAL  'lte'  '<='
BETWEEN  'between'  BETWEEN 'value1' AND 'value2 value must be an array with two indexes
IN  'in'  IN ('1' ,'3' , '5' ) value must be an array Or a query builder
NOT_IN  'notIn'  NOT IN ('1' ,'3' , '5' ) value must be an array Or a query builder
MATCH  'match'  LIKE 'value'
NOT_MATCH  'notMatch'  NOT LIKE 'value'
CONTAIN  'contains'  LIKE '%value%'
BEGIN_BY  'begin'  LIKE 'value%'
END_BY  'end'  LIKE '%value'
IS_NULL  'null' IS NULL set up value is ignored
IS_NOT_NULL  'notNull'  IS NOT NULL set up value is ignored

use oat\search\helper\SupportedOperatorHelper to see all supported operators

usage examples :

simple value example :

$query->add(RDFS_LABEL)->equals('foo');
$query->add(RDFS_LABEL)->gte(1);

OR :

$query->addCriterion(RDFS_LABEL , SupportedOperatorHelper::EQUAL , 'foo');
$query)->addCriterion(RDFS_LABEL , SupportedOperatorHelper::GREATER_THAN_EQUAL , 1);

between value example :

$query->add(RDFS_LABEL)->between(1 , 10);

OR :

$query->add(RDFS_LABEL)->between([1 , 10]);

OR :

$query->addCriterion(RDFS_LABEL,SupportedOperatorHelper::BETWEEN , [1 , 10]);

IN value example :

$query->add(RDFS_LABEL)->in(1 , 5 , 10);

OR

$query->add(RDFS_LABEL)->in([1 , 5 , 10]);

OR

$query->addCriterion( RDFS_LABEL ,SupportedOperatorHelper::IN , [1 , 5 , 10]);

NULL and NOT NULL value example :

$query->add(RDFS_LABEL)->null(NULL);
$query->add(RDFS_LABEL)->notNull(NULL);

OR

$query->addCriterion( RDFS_LABEL ,SupportedOperatorHelper::NULL , null);
$query->addCriterion( RDFS_LABEL ,SupportedOperatorHelper::NOT_NULL , null]);

sorting Query :

### Sort method is available on QueryBuilder :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query()->sort(
[
RDFS_LABEL => 'asc'
]
);

$query = $queryBuilder->add(RDFS_COMMENT)->contains('foo');
$queryBuilder->setCriteria($query);

$result = $search->getGateway()->search($queryBuilder);

Example for muliple sort :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

$queryBuilder = $search->query();

$query = $queryBuilder->add(RDFS_COMMENT)->contains('foo');

$queryBuilder->setCriteria($query)->sort(
[
RDFS_LABEL => 'asc',
RDFS_COMMENT => 'desc',
]
);
$result = $search->getGateway()->search($queryBuilder);

random sorting

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

$queryBuilder = $search->query();

$query = $queryBuilder->add(RDFS_COMMENT)->contains('foo');

$queryBuilder->setCriteria($query)->setRandom();

//fields are ignore when random is enable

$result = $search->getGateway()->search($queryBuilder);

Limit and Offset :

limit method is also available on QueryBuilder :

get 10 results :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

$query = $queryBuilder->newQuery()->add(RDFS_COMMENT)->contains('foo');

$queryBuilder->setCriteria($query)->setLimit(10);

$result = $search->getGateway()->search($queryBuilder);

get 10 results offset 5 :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query()->setLimit(10)->setOffset(5);

$query = $queryBuilder->newQuery()->add(RDFS_COMMENT)->contains('foo');

$queryBuilder->setCriteria($query);

$result = $search->getGateway()->search($queryBuilder);

NB : offset without limit is ignored

Gateway :

gateway is the highter component of complex search. It provide query builder and it execute query using default database driver.

only get number of result :

to get query number of result use count method :

$total = $search->getGateway()->count($queryBuilder);

debugging :

to debug query use printQuery method :

$search->getGateway()->serialyse($queryBuilder);
$search->getGateway()->printQuery();

Result Set :

a result set is returned by gateWay search method's. It's an arrayIterator adding total method which return full number for your query .

Each entry of result set is a tao resource Object.

basic usage :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);

$queryBuilder = $search->query();
$query = $queryBuilder->newQuery()->add(RDFS_COMMENT)->contains('foo');

$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder)

foreach($result as $resource) {
       echo $resource->getLabel();
}

use total :

$search = $this->getServiceManager()->get(\oat\generis\model\kernel\persistence\smoothsql\search\ComplexSearchService::SERVICE_ID);
$queryBuilder = $search->query();
$query = $queryBuilder->newQuery()->add(RDFS_COMMENT)->contains('foo');

$queryBuilder->setCriteria($query);
$result = $search->getGateway()->search($queryBuilder);

echo $result->total(); 
echo $result->count();

// 18
// 18