Jump to content
patrick

search function for multi language website

Recommended Posts

Currently working on a search function for a multi language website (DE, FR, IT).

If I'm on the FR (or IT) version of the website and start a search with a DE term, I also get FR (or IT) pages in the search result (because the default language field has this DE term).

How can I achieve that the find() function searches only in the field of the current user language?

The current selector looks like this:

$selector = "title|body~=$q, limit=50";

 

Share this post


Link to post
Share on other sites

Hi Robin S

Thanks a lot for your reply.

I tried the example in your linked post and it works:

$field = $user->language->isDefault() ? "body" : "body.data" . $user->language;

But in case of Repeaters it seems not to work. The following code throws an Error Exception: Multi-dot 'a.b.c' type selectors may not be used with OR '|' fields.

$field = $user->language->isDefault() ? "repeater_element.body" : "repeater_element.body.data" . $user->language;

I tried with sub-selectors, but couldn't get it to work fo far:

 

  • Like 1

Share this post


Link to post
Share on other sites

Does anybody know how to write such a selector, which also works with Repeaters too?

Share this post


Link to post
Share on other sites

I don't have a multi-language installation to test on but it should be possible.

You'll have to work out the best way to use conditionals to construct your selector but the default language selector would look like...

$result = $pages->find("repeater_element.body%=foo");

...and the non-default language selector would look like...

$result = $pages->find("repeater_element=[body.data{$user->language}%=foo]");

 

  • Like 1

Share this post


Link to post
Share on other sites

Hi Robin

Thanks a lot for your answer.

Your example is working perfect with a single field, but there are no matches (with the same query) if I search in multiple fields:

$result = $pages->find("repeater_element=[title.data{$user->language}%=foo], repeater_element=[body.data{$user->language}%=foo]");

I also tried this (which is not working too):

$title = "title.data{$user->language}";
$body  = "body.data{$user->language}";
$titleRepeater = "content_element_matrix=[content_element_title.data{$user->language}]";
$bodyRepeater  = "content_element_matrix=[body.data{$user->language}]";
        
$result = $pages->find($title|$body|$titleRepeater|$bodyRepeater%=$q");

Every hint is much appreciated.
Thank you.

Share this post


Link to post
Share on other sites
7 hours ago, patrick said:

but there are no matches (with the same query) if I search in multiple fields:


$result = $pages->find("repeater_element=[title.data{$user->language}%=foo], repeater_element=[body.data{$user->language}%=foo]");

Every comma separated clause in the selector string constitutes an AND condition. So in this selector you are saying "find pages where foo is in the title AND foo is in the body of a repeater_element". I think you probably meant to say "find pages where foo is in the title OR foo is in the body of a repeater_element".

So you can do this with OR-groups...

$result = $pages->find("(repeater_element=[title.data{$user->language}%=foo]), (repeater_element=[body.data{$user->language}%=foo])");

...or perhaps with an OR condition in the field portion of the selector (try it and see if it works)....

$result = $pages->find("repeater_element=[title.data{$user->language}|body.data{$user->language}%=foo]");

 

7 hours ago, patrick said:

I also tried this (which is not working too):


$title = "title.data{$user->language}";
$body  = "body.data{$user->language}";
$titleRepeater = "content_element_matrix=[content_element_title.data{$user->language}]";
$bodyRepeater  = "content_element_matrix=[body.data{$user->language}]";
        
$result = $pages->find($title|$body|$titleRepeater|$bodyRepeater%=$q");

This won't work because the search term has to be within the square brackets of the sub-selector.

  • Like 1

Share this post


Link to post
Share on other sites

Perfect. Thanks a lot Robin, for your answer and explanation!

I tried both and both are working. In the end I decided to go with the OR groups:

$title = "title.data{$user->language}%=$q";
$body  = "body.data{$user->language}%=$q";
$titleRepeater = "content_element_matrix=[content_element_title.data{$user->language}%=$q]";
$bodyRepeater  = "content_element_matrix=[body.data{$user->language}%=$q]";
        
$result = $pages->find("($title), ($body), ($titleRepeater), ($bodyRepeater), limit=50");

Thanks again.

Have a good day.
Patrick

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...