match_object/2可以工作(您也可以使用select/2在單個操作中執行一些字段的搜索和提取)下面的代碼完成這項工作(例如ets用於快速測試,但應該與mnesia一起使用):
-module (test3).
-compile([export_all]).
-include_lib("stdlib/include/ms_transform.hrl").
init() ->
ets:new(test,[named_table,bag]),
ets:insert(test,{offline_msg,{<<"+1">>,<<"devlab">>},{1440,484625,227224},never,
{jid,<<"+111">>,<<"devlab">>,<<"xyz">>,<<"+111">>,<<"devlab">>,<<"xyz">>},
{jid,<<"+1">>,<<"devlab">>,<<>>,<<"+1">>,<<"devlab">>,<<>>},{xmlel,<<"message">>,
[{<<"to">>,<<"multicast.devlab">>},{<<"type">>,<<"group">>},{<<"id">>,<<"99897">>}],
[{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
[{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"[email protected]">>},{<<"desc">>,<<"2001,104">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"[email protected]">>},{<<"desc">>,<<"2001,104">>}],[]}]},
{xmlel,<<"subject">>,[],[{xmlcdata,<<"1440484625:GROUP">>}]},{xmlel,<<"body">>,[],
[{xmlcdata,<<"mod_archive,0,https://IP/downloadfiles/avatar/104.jpg">>}]},
{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}]}}),
ets:insert(test,{offline_msg,{<<"+1">>,<<"devlab">>},{1440,484625,227224},never,
{jid,<<"+111">>,<<"devlab">>,<<"xyz">>,<<"+111">>,<<"devlab">>,<<"xyz">>},
{jid,<<"+1">>,<<"devlab">>,<<>>,<<"+1">>,<<"devlab">>,<<>>},{xmlel,<<"message">>,
[{<<"to">>,<<"multicast.devlab">>},{<<"type">>,<<"group">>},{<<"id">>,<<"99898">>}],
[{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
[{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"[email protected]">>},{<<"desc">>,<<"2001,104">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"[email protected]">>},{<<"desc">>,<<"2001,104">>}],[]}]},
{xmlel,<<"subject">>,[],[{xmlcdata,<<"1440484625:GROUP">>}]},{xmlel,<<"body">>,[],
[{xmlcdata,<<"mod_archive,0,https://IP/downloadfiles/avatar/104.jpg">>}]},
{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}]}}),
ets:insert(test,{offline_msg,{<<"+1">>,<<"devlab">>},{1440,484625,227224},never,
{jid,<<"+111">>,<<"devlab">>,<<"xyz">>,<<"+111">>,<<"devlab">>,<<"xyz">>},
{jid,<<"+1">>,<<"devlab">>,<<>>,<<"+1">>,<<"devlab">>,<<>>},{xmlel,<<"message">>,
[{<<"to">>,<<"multicast.devlab">>},{<<"type">>,<<"group">>},{<<"id">>,<<"99897">>}],
[{xmlel,<<"addresses">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/address">>}],
[{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"[email protected]">>},{<<"desc">>,<<"2001,104">>}],[]},
{xmlel,<<"address">>,[{<<"type">>,<<"to">>},{<<"jid">>,<<"[email protected]">>},{<<"desc">>,<<"2001,104">>}],[]}]},
{xmlel,<<"subject">>,[],[{xmlcdata,<<"1440484625:GROUP">>}]},{xmlel,<<"body">>,[],
[{xmlcdata,<<"mod_archive,0,https://IP/downloadfiles/avatar/105.jpg">>}]},
{xmlel,<<"request">>,[{<<"xmlns">>,<<"urn:xmpp:receipts">>}],[]}]}}).
test(Id) ->
MS = {'_','_','_','_','_','_',{'_','_',['_','_',{<<"id">>,Id}],'_'}},
ets:match_object(test,MS).
但你可以看到它實際上還要考慮你的記錄的結構,特別是在鍵/值列表[{<<"to">>,<<"multicast.devlab">>},{<<"type">>,<<"group">>},{<<"id">>,<<"99897">>}]
的順序。如果你絕對相信這個句子的順序永遠不會改變,那就好了。如果您知道大小不會再改變,你可以使用匹配的列表規範,如:
test(Id) ->
MS = [{{'_','_','_','_','_','_',{'_','_',['_','_',{<<"id">>,Id}],'_'}},[],['$_']},
{{'_','_','_','_','_','_',{'_','_',['_',{<<"id">>,Id},'_'],'_'}},[],['$_']},
{{'_','_','_','_','_','_',{'_','_',[{<<"id">>,Id},'_','_'],'_'}},[],['$_']}],
ets:select(test,MS).
但我建議你存儲在腦海ETS保存的數據不同的搜索,你將不得不做,分裂您的數據存儲在更基本和明智的領域:在存儲數據之前,您擁有erlang的所有功能和速度來執行分析,而在matchspec中,您可以極大地減少複雜匹配的速度和速度。