Skip to content

Commit

Permalink
Merge pull request #19 from indexdata/paz-1050-union-of-filter-keys
Browse files Browse the repository at this point in the history
Paz 1050 union of filter keys
  • Loading branch information
adamdickmeiss committed Nov 14, 2017
2 parents 19867b6 + 3b0756a commit 4aff737
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 10 deletions.
14 changes: 10 additions & 4 deletions doc/pazpar2_protocol.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,15 +218,21 @@ search.pz?command=settings&session=2044502273&pz:allow[search.com:210/db1]=1
Limits the search to a given set of targets specified by the
filter. The filter consists of a comma-separated list of
<emphasis>setting</emphasis>+<emphasis>operator</emphasis>+<emphasis>args</emphasis>
pairs. The <emphasis>setting</emphasis> is a Pazpar2 setting
pairs all of which must be satisfied (matched) for Pazpar2 to
include the target.
The <emphasis>setting</emphasis> is a Pazpar2 setting
(such as <literal>pz:id</literal>).
The <emphasis>operator</emphasis> is either
<literal>=</literal> (string match)
or <literal>~</literal> (substring match).
The <emphasis>args</emphasis> is a list of values separated
by <literal>|</literal> (or , one of the values).
The idea is that only targets with a setting matching one of
the values given will be included in the search.
by <literal>|</literal>. If either of these values match, the
key-value pair is matched.
</para>
<para>
For Pazpar2 1.13.0 the filter can be prefixed with vertical bar
(<literal>|</literal>) as first character. In this case, if any of the
key-value pairs matches, Pazpar2 includes the target.
</para>
</listitem>
</varlistentry>
Expand Down
26 changes: 21 additions & 5 deletions src/database.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ struct database_criterion_value {
};

struct database_criterion {
int union_filter;
char *name;
enum pazpar2_database_criterion_type type;
struct database_criterion_value *values;
Expand Down Expand Up @@ -190,9 +191,15 @@ static struct database_criterion *create_database_criterion(NMEM m,
char **values;
int num;
int i;
int union_filter = 0;

if (!buf || !*buf)
return 0;
if (*buf == '|')
{
union_filter = 1;
buf++;
}
nmem_strsplit(m, ",", buf, &values, &num);
for (i = 0; i < num; i++)
{
Expand All @@ -201,6 +208,8 @@ static struct database_criterion *create_database_criterion(NMEM m,
int subi;
struct database_criterion *new = nmem_malloc(m, sizeof(*new));
char *eq;

new->union_filter = union_filter;
for (eq = values[i]; *eq; eq++)
if (*eq == '=')
{
Expand Down Expand Up @@ -240,13 +249,20 @@ static int database_match_criteria(struct setting **settings,
struct conf_service *service,
struct database_criterion *cl)
{
for (; cl; cl = cl->next)
if (!match_criterion(settings, num_settings, service, cl))
break;
if (cl) // one of the criteria failed to match -- skip this db
if (cl && cl->union_filter)
{
for (; cl; cl = cl->next)
if (match_criterion(settings, num_settings, service, cl))
return 1;
return 0;
}
else
return 1;
{
for (; cl; cl = cl->next)
if (!match_criterion(settings, num_settings, service, cl))
return 0;
}
return 1;
}

// Cycles through databases, calling a handler function on the ones for
Expand Down
3 changes: 2 additions & 1 deletion test/test_filter.urls
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ http://localhost:9763/search.pz2?session=1&command=termlist&name=author,date,sub
http://localhost:9763/search.pz2?session=1&command=search&query=the%207&filter=pz%3Aid%3Dlocalhost%3A9999
http://localhost:9763/search.pz2?session=1&command=show&block=1
http://localhost:9763/search.pz2?session=1&command=termlist&name=author,date,subject,xtargets
http://localhost:9763/search.pz2?session=1&command=search&query=the%207&filter=pz%3Aid%3Dlocalhost%3A9999
http://localhost:9763/search.pz2?session=1&command=search&query=the%207&filter=%7Cpz%3Aid%3Dlocalhost%3A9999,pz%3Aid%3Dunknown
http://localhost:9763/search.pz2?session=1&command=show
http://localhost:9763/search.pz2?session=1&command=termlist&name=author,date,subject,xtargets
http://localhost:9763/search.pz2?session=1&command=search&query=the&filter=pz%3Aid%3Dz3950.indexdata.com%2Fmarc
Expand All @@ -15,3 +15,4 @@ http://localhost:9763/search.pz2?command=init
http://localhost:9763/search.pz2?session=2&command=search&query=the%2022&filter=pz%3Aid%3Dlocalhost%3A9999
http://localhost:9763/search.pz2?session=2&command=show&sort=title:0&block=1
http://localhost:9763/search.pz2?session=2&command=show&sort=title:0&block=1
http://localhost:9763/search.pz2?session=2&command=search&query=the%2022&filter=%7Cpz%3Aid%3Dunknown
2 changes: 2 additions & 0 deletions test/test_filter_18.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<error code="8" msg="No targets"></error>

0 comments on commit 4aff737

Please sign in to comment.