Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

I have problems to understand the difference between search and relatedTo. From my understanding, this is a fulltext search on all fields.

{% set entries = craft.entries.search(test).order('score') %}

Does the following approach mean the same?

{% set entries = craft.entries.relatedTo(test).order('score').find() %}

And if yes, which one delivers the better performance?

Thanks!

share|improve this question

1 Answer 1

up vote 6 down vote accepted

To be honest, those two parameters may seem to be slightly similar but provide a totally different functionality.

From their documentation on search:

search

Only fetch entries that match a given search query. (See Searching for the syntax and > available search attributes.)

Which means whatever you pass in .search(query), and there is a rather complex list here, will actually 'search' Craft for the query string you specify.

On a different note,

relatedTo

Only fetch entries that are related to certain other elements. (See Relations for the syntax options.)

This on the other hand only 'relates' to other entries, meaning its not performing a fill search on Craft, only the entries. A detailed reference on relations can be found here.

As far as performance, the two are unrelated as it really depends on what you are trying to accomplish.

Hope this clears it up for you!

share|improve this answer
    
Using search is going to be slower in about 99% of the cases as it's doing a MySQL Full Text Search (dev.mysql.com/doc/refman/5.0/en/fulltext-search.html), an inherently slow operation. –  Brad Bell Jul 25 at 17:03
    
Excellent point Brad. –  Jason McCallister Jul 25 at 17:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.