Skip to content

DOCSP-35959: search text #2889

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 119 additions & 0 deletions docs/fundamentals/read-operations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This guide shows you how to perform the following tasks:

- :ref:`laravel-retrieve-matching`
- :ref:`laravel-retrieve-all`
- :ref:`laravel-retrieve-text-search`
- :ref:`Modify Find Operation Behavior <laravel-modify-find>`

Before You Get Started
Expand Down Expand Up @@ -175,6 +176,124 @@ Use the following syntax to run a find operation that matches all documents:
more information about ``take()``, see the :ref:`laravel-modify-find` section of this
guide.

.. _laravel-retrieve-text-search:

Search Text Fields
------------------

You can perform a text search by using the :manual:`$text
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

I think it could be better to swap this paragraph (describes how to perform one) with the one following it (describes what a text search is). The first paragraph also seems more connected to the third paragraph ("After building your query...").

</reference/operator/query/text>` operator followed
by the ``$search`` field in your query filter that you pass to the
``where()`` method. The ``$text`` operator performs a text search on the
text-indexed fields. The ``$search`` field specifies the text to search for.

A text search retrieves documents that contain a **term** or a **phrase** in the
text-indexed fields. A term is a sequence of characters that excludes
whitespace characters. A phrase is a sequence of terms with any number
of whitespace characters.

.. note::

Before you can perform a text search, you must create a :manual:`text
index </core/indexes/index-types/index-text/>` on
the text-valued field. To learn more about creating
indexes, see the :ref:`laravel-eloquent-indexes` section of the
Schema Builder guide.

After building your query with the ``where()`` method, chain the ``get()``
method to retrieve the query results.

This example calls the ``where()`` method on the ``Movie`` Eloquent model to
retrieve documents in which the ``plot`` field contains the phrase
``"love story"``:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue:

I think the creation of the index needs to be mentioned or shown to understand that "plot" is the indexed field.

Suggestion:
I think either mentioning that the text index was created on the "plot" field or showing an example of creating the index similar to this Java documentation would help a reader understand the connection.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned this in the note, but I can also add a sentence that explains that the example assumes a text index on the plot field


.. tabs::

.. tab:: Query Syntax
:tabid: query-syntax

Use the following syntax to specify the query:

.. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
:language: php
:dedent:
:start-after: start-text
:end-before: end-text

.. tab:: Controller Method
:tabid: controller

To see the query results in the ``browse_movies`` view, edit the ``show()`` function
in the ``MovieController.php`` file to resemble the following code:

.. io-code-block::
:copyable: true

.. input::
:language: php

class MovieController
{
public function show()
{
$movies = Movie::where('$text', ['$search' => "\"" . 'love story' . "\""])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure it's better like this. The doubles quotes will probably come from user input.

Suggested change
$movies = Movie::where('$text', ['$search' => "\"" . 'love story' . "\""])
$movies = Movie::where('$text', ['$search' => '"love story"'])

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this improvement! Was struggling to make this work, didnt realize the solution was so simple.

->get();

return view('browse_movies', [
'movies' => $movies
]);
}
}

.. output::
:language: none
:visible: false

Title: Cafè de Flore
Year: 2011
Runtime: 120
IMDB Rating: 7.4
IMDB Votes: 9663
Plot: A love story between a man and woman ...

Title: Paheli
Year: 2005
Runtime: 140
IMDB Rating: 6.7
IMDB Votes: 8909
Plot: A folk tale - supernatural love story about a ghost ...

Title: Por un puèado de besos
Year: 2014
Runtime: 98
IMDB Rating: 6.1
IMDB Votes: 223
Plot: A girl. A boy. A love story ...

...

.. note::

To specify a phrase as the text search criteria, you must include it
with escaped quotes in the query filter.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question:
Perhaps a question for the technical reviewer, but is it possible to use single quotes instead of double quotes to represent the value of the $search key (which would hopefully avoid having to escape the quotes and concatenate strings)?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use single quote everywhere, unless I want to use variable injection with double quotes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested this improvement and it works! removing the admonition


A text search assigns a numerical text score to indicate how closely
each result matches the string in your query filter. You can sort the
results by relevance by using the ``orderBy()`` method to sort on the
``textScore`` metadata:

.. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php
:language: php
:dedent:
:start-after: start-text-relevance
:end-before: end-text-relevance
:emphasize-lines: 2

.. tip::

To learn more about the ``orderBy()`` method, see the
:ref:`laravel-sort` section of this guide.

.. _laravel-modify-find:

Modify Behavior
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ protected function setUp(): void
['year' => 1999, 'countries' => ['Indonesia'], 'title' => 'Title 4'],
['year' => 1999, 'countries' => ['Canada'], 'title' => 'Title 5'],
['year' => 1999, 'runtime' => 30],
['title' => 'movie_a', 'plot' => 'this is a love story'],
['title' => 'movie_b', 'plot' => 'love is a long story'],
['title' => 'movie_c', 'plot' => 'went on a trip'],
]);
}

Expand Down Expand Up @@ -94,4 +97,41 @@ public function testFirst(): void
$this->assertNotNull($movie);
$this->assertInstanceOf(Movie::class, $movie);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testText(): void
{
// start-text
$movies = Movie::where('$text', ['$search' => "\"" . 'love story' . "\""])
->get();
// end-text

$this->assertNotNull($movies);
$this->assertCount(2, $movies);
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testTextRelevance(): void
{

$collection = MongoDB::connection('mongodb')->collection('movies');
$index = ['plot' => 'text'];
$collection->createIndex($index);

// start-text-relevance
$movies = Movie::where('$text', ['$search' => "\"" . 'love story' . "\""])
->orderBy('score', ['$meta' => 'textScore'])
->get();
// end-text-relevance

$this->assertNotNull($movies);
$this->assertCount(2, $movies);
}

}