mnesia text search ?

ke han ke.han@REDACTED
Mon Apr 24 17:55:01 CEST 2006


yes, thanks...after fixing that syntax error,, I realized my other  
syntax errors ;-).  Now that I have a clean compile, I still can't  
get the function to work as expected.  Any ideas?  I am testing the  
simple case of product:findProduct("name", undefined, undefined).  I  
get back an empty list when I know that name matches the regexp of  
one of the fields.
I am suspicious that the cluase in Fun:
		RecordPattern = Record ->
is not a valid way to match.  But it complies and runs without error,  
so I'm a bit confused.
thanks, ke han


findProduct(SearchString, Industry, Client) ->
	RecordPattern = case {Industry, Client} of
		{undefined, undefined} 									->
			#product{};
		{Industry, undefined} when list(Industry)				->
			#product{industry = Industry};
		{undefined, Client} when list(Client)					->
			#product{client = Client};
		{Industry, Client} when list(Industry), list(Client)	->
			#product{industry = Industry, client = Client}
		end,
	
	SearchStringFun = case SearchString of
		undefined								->
			fun(Product) ->
				true end;
		SearchString when list(SearchString) ->
			{ok, RegExp} = regexp:parse(SearchString),
			fun(#product{clientProductId = ClientProductId, name = Name,  
description = Description}) ->
				case regexp:first_match(ClientProductId, RegExp) of
				{match, _} 	-> true;
				_			->
					case regexp:first_match(Name, RegExp) of
					{match, _} 	-> true;
					_			->	
						case regexp:first_match(Description, RegExp) of
						{match, _} 	-> true;
						_			-> false
						end
					end
				end
			end
		end,			

	Fun = fun(Record, NewAcc) ->
			case Record of
				RecordPattern = Record ->
					case SearchStringFun(Record) of
						true	-> [Record | NewAcc];
						_		-> NewAcc
					end;
				_ -> NewAcc
			end
		end,
	{atomic, Products} = mnesia:transaction(fun() ->
						mnesia:foldl(Fun, [], product)
						end),
	Products.

On Apr 24, 2006, at 11:24 PM, Ulf Wiger ((AL/EAB)) wrote:

>
> Aren't you missing an 'end' to close the
> SearchString = case ... construct?
>
> BR,
> Ulf W
>
>> -----Original Message-----
>> From: ke han [mailto:ke.han@REDACTED]
>> Sent: den 24 april 2006 16:56
>> To: Ulf Wiger (AL/EAB)
>> Cc: erlang-questions@REDACTED
>> Subject: Re: mnesia text search ?
>>
>> I have coded the following, which has a compile error (I
>> can't seem to find it).  erlc reports the error as being
>> syntax error before:
>> '.' on the last line.
>> Any ideas on the error as well as if this code is an
>> acceptable way to solve my search problem?
>> thanks, ke han
>>
>> findProduct(SearchString, Industry, Client) ->
>> 	RecordPattern = case {Industry, Client} of
>> 		{undefined, undefined} 				
>> 		->
>> 			Product#product{};
>> 		{Industry, undefined} when list(Industry)	
>> 		->
>> 			Product#product{industry = Industry};
>> 		{undefined, Client} when list(Client)		
>> 	->
>> 			Product#product{client = Client};
>> 		{Industry, Client} when list(Industry),
>> list(Client)	->
>> 			Product#product{industry = Industry,
>> client = Client}
>> 		end,
>> 	
>> 	SearchStringFun = case SearchString of
>> 		undefined					
>> 	->
>> 			fun(Product) ->
>> 				true end;
>> 		SearchString when list(SearchString) ->
>> 			{ok, RegExp} = regexp:parse(SearchString),
>> 			fun(Product#product{clientProductId =
>> ClientProductId, name = Name, description = Description}) ->
>> 				case
>> regexp:first_match(ClientProductId, RegExp) of
>> 				{match, _} 	-> true;
>> 				_			->
>> 					case
>> regexp:first_match(Name, RegExp) of
>> 					{match, _} 	-> true;
>> 					_			->	
>> 						case
>> regexp:first_match(Description, RegExp) of
>> 						{match, _} 	-> true;
>> 						_		
>> 	-> false
>> 						end
>> 					end
>> 				end
>> 			end,				
>>
>> 	Fun =
>> 		fun(Record, NewAcc) when Record = RecordPattern ->
>> 			case SearchStringFun(Record) of
>> 			true	-> [Record | NewAcc];
>> 			_		-> NewAcc
>> 			end;
>> 		(_, NewAcc) -> NewAcc
>> 		end,
>> 	mnesia:foldl(Fun, [], product).
>>
>>
>>
>>




More information about the erlang-questions mailing list