From 2feaf7c800248ec47164195c7e2c7376df5bf066 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Wed, 23 Oct 2024 12:55:19 +0200 Subject: [PATCH] Add docs about new SearchBuilder for single index (#453) The docs was not yet update. See https://github.com/schranz-search/schranz-search/issues/99 and https://github.com/schranz-search/schranz-search/pull/451. --- docs/cookbooks/create-own-adapter.rst | 15 +++---- docs/getting-started/index.rst | 6 +-- docs/search-and-filters/index.rst | 61 +++++++++------------------ 3 files changed, 28 insertions(+), 54 deletions(-) diff --git a/docs/cookbooks/create-own-adapter.rst b/docs/cookbooks/create-own-adapter.rst index d4e5c77d..0051e231 100644 --- a/docs/cookbooks/create-own-adapter.rst +++ b/docs/cookbooks/create-own-adapter.rst @@ -524,13 +524,12 @@ Search Engine to Search Engine. A common way is the following example: { // optimized single document query if ( - 1 === \count($search->indexes) - && 1 === \count($search->filters) + 1 === \count($search->filters) && $search->filters[0] instanceof Condition\IdentifierCondition && 0 === $search->offset && 1 === $search->limit ) { - $singleDocumentIndexName = $search->indexes[\array_key_first($search->indexes)]->name; + $singleDocumentIndexName = $search->index->name; $singleDocumentIdentifier = $search->filters[0]->identifier; try { @@ -541,13 +540,13 @@ Search Engine to Search Engine. A common way is the following example: } return new Result( - $this->hitsToDocuments($search->indexes, []), + $this->hitsToDocuments($search->index, []), 0, ); } return new Result( - $this->hitsToDocuments($search->indexes, [$data]), + $this->hitsToDocuments($search->index, [$data]), 1, ); } @@ -556,15 +555,13 @@ Search Engine to Search Engine. A common way is the following example: } /** - * @param Index[] $indexes + * @param Index $index * @param iterable> $hits * * @return \Generator> */ - private function hitsToDocuments(array $indexes, iterable $hits): \Generator + private function hitsToDocuments(Index $index, iterable $hits): \Generator { - $index = $indexes[\array_key_first($indexes)]; - foreach ($hits as $hit) { yield $this->marshaller->unmarshall($index->fields, $hit); } diff --git a/docs/getting-started/index.rst b/docs/getting-started/index.rst index 284a0904..1f8d429f 100644 --- a/docs/getting-started/index.rst +++ b/docs/getting-started/index.rst @@ -1843,8 +1843,7 @@ many exists in the given index. public function someMethod() { - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new \Schranz\Search\SEAL\Search\Condition\SearchCondition('first')) ->getResult(); @@ -1876,8 +1875,7 @@ we will filter by the ``tags`` field and get all documents which have the tag `` public function someMethod() { - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new \Schranz\Search\SEAL\Search\Condition\EqualCondition('tags', 'UI')); ->getResult(); diff --git a/docs/search-and-filters/index.rst b/docs/search-and-filters/index.rst index cfb33238..4827378a 100644 --- a/docs/search-and-filters/index.rst +++ b/docs/search-and-filters/index.rst @@ -16,8 +16,7 @@ The following shows the basic usage as already shown in the "Getting Started" do use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(/* ... */) ->getResult(); @@ -29,9 +28,7 @@ The following shows the basic usage as already shown in the "Getting Started" do .. note:: - Currently only the ``Elasticsearch`` and ``Opensearch`` adapters supports to search on - multiple indexes at once. The other adapters are not yet supporting to call ``addIndex`` - multiple times and will fail so with an exception if you try to do so. + It is also possible to change the index after creating the searchbuilder via ``$searchBuilder->index('other')``. Conditions ---------- @@ -47,8 +44,7 @@ The ``SearchCondition`` is the most basic condition and can be used to search fo use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\SearchCondition('Search Term')) ->getResult(); @@ -65,8 +61,7 @@ The ``EqualCondition`` is used to filter the result by a specific field value ma use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\EqualCondition('tags', 'UI')) ->getResult(); @@ -84,8 +79,7 @@ The ``NotEqualCondition`` is used to filter the result by a specific field value use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\NotEqualCondition('tags', 'UI')) ->getResult(); @@ -105,8 +99,7 @@ then using a ``EqualCondition``. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\IdentifierCondition('23b30f01-d8fd-4dca-b36a-4710e360a965')) ->getResult(); @@ -122,8 +115,7 @@ the given value. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\GreaterThanCondition('rating', 2.5)) ->getResult(); @@ -141,8 +133,7 @@ the given value. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\GreaterThanEqualCondition('rating', 2.5)) ->getResult(); @@ -160,8 +151,7 @@ the given value. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\LessThanCondition('rating', 2.5)) ->getResult(); @@ -179,8 +169,7 @@ the given value. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\LessThanEqualCondition('rating', 2.5)) ->getResult(); @@ -197,8 +186,7 @@ The ``GeoDistanceCondition`` is used to filter results within a radius by specif use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('restaurants') + $result = $this->engine->createSearchBuilder('restaurants') ->addFilter(new Condition\GeoDistanceCondition('location', 45.472735, 9.184019, 2000)) ->getResult(); @@ -215,8 +203,7 @@ The ``GeoBoundingBoxCondition`` is used to filter results within a bounding box use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('restaurants') + $result = $this->engine->createSearchBuilder('restaurants') ->addFilter(new Condition\GeoBoundingBoxCondition('location', 45.494181, 9.214024, 45.449484, 9.179175)) ->getResult(); @@ -239,8 +226,7 @@ The ``OrCondition`` is used to filter by two or more conditions where at least o use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\OrCondition( new Condition\GreaterThanCondition('rating', 2.5), new Condition\EqualCondition('isSpecial', true), @@ -262,8 +248,7 @@ in combination with ``OrCondition`` filters. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\AndCondition( new Condition\EqualCondition('tags', 'Tech'), new Condition\OrCondition( @@ -307,8 +292,7 @@ Need to be queried this way `.`: use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\LessThanEqualCondition('rating.value', 2.5)) ->getResult(); @@ -335,8 +319,7 @@ Need to be queried this way `..`: use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(new Condition\EqualCondition('header.image.media', 21)) ->getResult(); @@ -353,8 +336,7 @@ Beside the searches and filters you can also limit the result by a given ``limit engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(/* ... */) ->limit(10) ->offset(20) @@ -369,8 +351,7 @@ With the ``limit`` and ``offset`` also a basic pagination can be created this wa $page = 1; // get from query parameter $pageSize = 10; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addFilter(/* ... */) ->limit($pageSize) ->offset(($page - 1) * $pageSize) @@ -397,8 +378,7 @@ your results but also ``sort`` them by a given field. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addSortBy('rating', 'desc') ->getResult(); @@ -408,8 +388,7 @@ your results but also ``sort`` them by a given field. use Schranz\Search\SEAL\Search\Condition; - $result = $this->engine->createSearchBuilder() - ->addIndex('blog') + $result = $this->engine->createSearchBuilder('blog') ->addSortBy('rating', 'asc') ->getResult();