1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
14 #include <yaz/rpn2solr.h>
15 #include <yaz/wrbuf.h>
16 #include <yaz/pquery.h>
18 static int compare(solr_transform_t ct, const char *pqf, const char *solr)
21 ODR odr = odr_createmem(ODR_ENCODE);
22 WRBUF w = wrbuf_alloc();
23 Z_RPNQuery *q = p_query_rpn(odr, pqf);
27 int r = solr_transform_rpn2solr_wrbuf(ct, w, q);
32 yaz_log(YLOG_LOG, "%s -> Error %d", pqf, r);
33 if (!solr) /* also expected error? */
38 yaz_log(YLOG_LOG, "%s -> %s", pqf, wrbuf_cstr(w));
39 if (solr && !strcmp(wrbuf_cstr(w), solr))
50 static void tst1(void)
52 solr_transform_t ct = solr_transform_create();
54 YAZ_CHECK(compare(ct, "abc", "abc"));
55 YAZ_CHECK(compare(ct, "\"a b c\"", "\"a b c\""));
57 /* Invalid PQF, so this will never work */
58 YAZ_CHECK(compare(ct, "a b", "a b"));
60 YAZ_CHECK(compare(ct, "@not a b", "a AND NOT b"));
61 YAZ_CHECK(compare(ct, "@and @or a b c", "(a OR b) AND c"));
62 YAZ_CHECK(compare(ct, "@and a b", "a AND b"));
63 YAZ_CHECK(compare(ct, "@or a b", "a OR b"));
64 YAZ_CHECK(compare(ct, "@attr 1=field abc", "field:abc"));
65 YAZ_CHECK(compare(ct, "@attr 1=field \"a b c\"", "field:\"a b c\""));
66 YAZ_CHECK(compare(ct, "@attr 1=4 abc", 0)); /* should fail */
68 solr_transform_define_pattern(ct, "index.title", "1=4");
69 YAZ_CHECK(compare(ct, "@attr 1=4 abc", "title:abc"));
71 solr_transform_define_pattern(ct, "index.foo", "1=bar");
72 YAZ_CHECK(compare(ct, "@attr 1=bar abc", "foo:abc"));
75 YAZ_CHECK(compare(ct, "@attr 5=1 water", "water*"));
76 YAZ_CHECK(compare(ct, "@attr 5=2 water", "*water"));
77 YAZ_CHECK(compare(ct, "@attr 5=3 water", "*water*"));
80 YAZ_CHECK(compare(ct, "@or @attr 1=1016 water @attr 7=1 @attr 1=4 0", "any:water rank:??");
83 solr_transform_close(ct);
86 static void tst2(void)
88 WRBUF w = wrbuf_alloc();
89 solr_transform_t ct = 0;
90 const char *srcdir = getenv("srcdir");
93 wrbuf_puts(w, srcdir);
96 wrbuf_puts(w, "../etc/pqf.properties");
98 ct = solr_transform_open_fname(wrbuf_cstr(w));
99 YAZ_CHECK(compare(ct, "@attr 1=4 abc", "dc.title:abc"));
101 YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=108 abc", "dc.title:abc"));
104 YAZ_CHECK(compare(ct, "@attr 1=4 @attr 3=1 @attr 6=1 abc", "dc.title:abc"));
105 YAZ_CHECK(compare(ct, "@attr 1=4 @attr 4=1 @attr 6=1 abc", "dc.title:abc"));
107 YAZ_CHECK(compare(ct, "@attr 1=1016 abc", "abc"));
108 YAZ_CHECK(compare(ct, "@attr 2=1 @attr 1=30 1980", "dc.date:[* to 1980]"));
109 YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=3 1980", "dc.date:1980"));
110 YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=5 1980", "dc.date:[* to 1980]"));
111 YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=2 1980", "dc.date:[* to 1980]"));
112 YAZ_CHECK(compare(ct, "@attr 1=30 @attr 2=4 1980", "dc.date:[1980 to *]"));
114 YAZ_CHECK(compare(ct, "@attr 2=103 @attr 1=_ALLRECORDS 1", "solr.allRecords=1"));
116 YAZ_CHECK(compare(ct, "@attr 1=500 abc", 0));
117 solr_transform_close(ct);
121 int main (int argc, char **argv)
123 YAZ_CHECK_INIT(argc, argv);
133 * c-file-style: "Stroustrup"
134 * indent-tabs-mode: nil
136 * vim: shiftwidth=4 tabstop=8 expandtab