partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 7d50640b0662d8eafd3418e940d61ef4eb65aa3c
parent 7f1f50eac0e6da5f3999dc1d9bc2ddd92a14ca42
Author: Felicitus <felicitus@felicitus.org>
Date:   Tue,  1 May 2012 20:09:32 +0200

Added full text searching draft

Diffstat:
Asrc/backend/de/RaumZeitLabor/PartKeepr/FulltextSearch/FulltextSearch.php | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/backend/de/RaumZeitLabor/PartKeepr/Part/PartFulltextSearch.php | 30++++++++++++++++++++++++++++++
Msrc/backend/de/RaumZeitLabor/PartKeepr/Part/PartService.php | 8++++++--
3 files changed, 131 insertions(+), 2 deletions(-)

diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/FulltextSearch/FulltextSearch.php b/src/backend/de/RaumZeitLabor/PartKeepr/FulltextSearch/FulltextSearch.php @@ -0,0 +1,94 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\FulltextSearch; + +use de\RaumZeitLabor\PartKeepr\PartKeepr; + +abstract class FulltextSearch { + + /** + * Creates a new fulltext search based on the given query. + * + * The query can contain different query strings, separated by space. The query returns only results which contain + * all query strings in any fields. + * + * If the query is "100 ohm", it would only consider entities which have both "100" and "ohm", no matter which field + * the value is in. + * + * @param string $query The query to search for + */ + public function __construct ($query) { + $this->query = explode(" ", $query); + } + + /** + * Fires the query and returns the results. + * + * @param none + * @return array An array with all IDs which match the query + */ + public function query () { + return $this->fallbackSearch(); + } + + /** + * A fallback search in case no fulltext search engine is available. This directly queries the database, which is + * slow. + */ + protected function fallbackSearch () { + $qb = PartKeepr::getEM()->createQueryBuilder(); + + $qb ->select("q.id") + ->from($this->getEntityName(), "q") + ->where("1=1"); + + $dqlChunks = array(); + + foreach ($this->query as $id => $queryString) { + $partDqlChunks = array(); + foreach ($this->getFields() as $field) { + $partDqlChunks[] = "LOWER(q." . $field.") LIKE :filter".$id; + } + + $dqlChunks[] = "(".implode(" OR ", $partDqlChunks).")"; + + $qb->setParameter("filter".$id, "%".str_replace("%", "\\%", strtolower($queryString))."%"); + } + + $qb->andWhere(implode(" AND " ,$dqlChunks)); + + $query = $qb->getQuery(); + + + + $result = $query->getArrayResult(); + + if (count($result) > 0) { + $results = array(); + + foreach ($result as $value) { + $results[] = $value["id"]; + } + + return $results; + } else { + return array(0); + } + } + + /** + * Returns the FQDN name of the entity. This needs to be overridden in child classes. + * + * @param none + * @return string the FQDN of the entity to query, e.g. de\RaumZeitLabor\PartKeepr\Part\Part + */ + abstract protected function getEntityName (); + + /** + * Returns the fields in which the fulltext search should search in. + * + * @param none + * @return array An array of field names, e.g. array("name", "description") + */ + abstract protected function getFields (); + +}+ \ No newline at end of file diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartFulltextSearch.php b/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartFulltextSearch.php @@ -0,0 +1,29 @@ +<?php +namespace de\RaumZeitLabor\PartKeepr\Part; + +use de\RaumZeitLabor\PartKeepr\FulltextSearch\FulltextSearch; + +/** + * Implements the part fulltext search + */ +class PartFulltextSearch extends FulltextSearch { + /** + * Returns the FQDN of the part entity + * + * (non-PHPdoc) + * @see de\RaumZeitLabor\PartKeepr\FulltextSearch.FulltextSearch::getEntityName() + */ + protected function getEntityName () { + return 'de\RaumZeitLabor\PartKeepr\Part\Part'; + } + + /** + * Returns the fields to be searched in + * + * (non-PHPdoc) + * @see de\RaumZeitLabor\PartKeepr\FulltextSearch.FulltextSearch::getFields() + */ + protected function getFields () { + return array("comment", "name"); + } +}+ \ No newline at end of file diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartService.php b/src/backend/de/RaumZeitLabor/PartKeepr/Part/PartService.php @@ -35,8 +35,12 @@ class PartService extends Service implements RestfulService { * Applies text-based filtering */ if ($this->hasParameter("query") && $this->getParameter("query") != "") { - $queryBuilder->where("LOWER(q.name) LIKE :filter"); - $queryBuilder->setParameter("filter", "%".strtolower($this->getParameter("query"))."%"); + + $fulltextSearch = new PartFulltextSearch($this->getParameter("query")); + $fulltextSearchResults = $fulltextSearch->query(); + + $queryBuilder->andWhere("q.id IN (".implode(",", $fulltextSearchResults).")"); + } /**