-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
DOCSP-35959: search text #2889
Changes from 2 commits
2f9b58a
fee1259
a5f8a64
ea79bd0
cd6d025
802f50e
ee401a5
449c8aa
3aa265b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
|
@@ -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 | ||||||
</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"``: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
|
||||||
.. tabs:: | ||||||
|
||||||
.. tab:: Query Syntax | ||||||
:tabid: query-syntax | ||||||
|
||||||
Use the following syntax to specify the query: | ||||||
|
||||||
.. literalinclude:: /includes/fundamentals/read-operations/ReadOperationsTest.php | ||||||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
: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 | ||||||
{ | ||||||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
public function show() | ||||||
{ | ||||||
$movies = Movie::where('$text', ['$search' => "\"" . 'love story' . "\""]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question: There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
rustagir marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
: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 | ||||||
|
There was a problem hiding this comment.
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...").