Forums / Extensions / eZ Find / search, filter multiple fields and OR condition

search, filter multiple fields and OR condition

Author Message

Saad Tazi

Friday 28 August 2009 10:35:17 am

Hi,
I just want to share with you some difficulties I had with the ezfind search function.

I was trying to search only the recipe objects that have a specific value (let say 'breakfast') in specific fields (categories_*) only, not in all the fields. For example, if "Breakfast" was present in the ingredient field, I don't want this object in my result.

I tried to do a search on multiple fields but using an OR condition:

def $keyword = 'Breakfast'
$recipe_list = fetch(ezfind, search, 
			hash(
				query, $keyword,
				'limit', $recipes_per_page,
				'offset', $offset,
				filter, array('or',
								array('and',
								concat('recipe/categories_meals:',$keyword),
								'contentclass_id:48'
								),
								array('and',
								concat('recipe/categories_courses:',$keyword),
								'contentclass_id:48'
								),
								array('and',
								concat('recipe/categories_milkproducts:',$keyword),
								'contentclass_id:48'
								),
								array('and',
								concat('recipe/categories_milkcalendar:',$keyword),
								'contentclass_id:48'
								),
								array('and',
								concat('recipe/categories_moreoptions:',$keyword),
								'contentclass_id:48'
								)
								
								
								
								
				)
				

			)
		)

But I noticed that the $filterQuery param in ezfezpsolrquerybuilder.php was:

array:
0=>...(don't think it's relevant)...
1=>
( meta_contentclass_id_si:48 AND attr_categories_meals_t:Breakfast AND meta_contentclass_id_si:48 ) 
OR ( meta_contentclass_id_si:48 AND attr_categories_courses_t:Breakfast AND meta_contentclass_id_si:48 ) 
OR ( meta_contentclass_id_si:48 AND attr_categories_milkproducts_t:Breakfast AND meta_contentclass_id_si:48 ) 
OR ( meta_contentclass_id_si:48 AND attr_categories_milkcalendar_t:Breakfast AND meta_contentclass_id_si:48 ) 
OR ( meta_contentclass_id_si:48 AND attr_categories_moreoptions_t:Breakfast AND meta_contentclass_id_si:48 ) 
OR meta_language_code_s:eng-CA
 

This was returning more result than excepted because of the last condition <b>OR meta_language_code_s:eng-CA</b>. the search was returning a recipe that has "Breakfast" in another field ("tips").

So this is how I fix the problem: I started the filter with an "AND" condition:

				$recipe_list = fetch(ezfind, search, 
			hash(
				query, $keyword,
				'limit', $recipes_per_page,
				'offset', $offset,
				'contentclass_id',48,
				filter, array('and',
							array('or',
								array('and',
								concat('recipe/categories_meals:',$keyword)
								),
								array('and',
								concat('recipe/categories_courses:',$keyword)
								),
								array('and',
								concat('recipe/categories_milkproducts:',$keyword)
								),
								array('and',
								concat('recipe/categories_milkcalendar:',$keyword)
								),
								array('and',
								concat('recipe/categories_moreoptions:',$keyword)
								)
							)			
				)
			)
		)

THat's probably not the best way to bild the query, but it works for me.

Ivo Lukac

Tuesday 01 September 2009 5:50:36 am

I head similar problem and reported it:
http://issues.ez.no/IssueView.php?Id=15186&activeItem=2

http://www.linkedin.com/in/ivolukac
http://www.netgen.hr/eng/blog
http://twitter.com/ilukac

Max Keil

Tuesday 08 September 2009 10:41:19 am

The same problem as yours,

after 10 minutes debugging I found a dirty solution:

'filter', array( array( 'or',
                           array( 'and',
                                    'class_identifier:auktion',
                                    'auktion/beendet:0'
                                   ),
                          array( 'and',
                                   'class_identifier:sofortkauf',
                                   'sofortkauf/beendet:0'
                                  )
                             )
                ),

the whole 'OR' condition should be in a separate array because of the language code!

Mit freundlichen Grüßen
Best regards

Max Keil

____________________________________________________________
eZ Publish Gold Partner - http://www.all2e.com
http://ez.no/partners/worldwide_partners/all2e_gmbh