zoomtst3: print event name
[yaz-moved-to-github.git] / src / rpn2solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file
8  * \brief Implements RPN to SOLR conversion
9  *
10  */
11
12 #include <assert.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <yaz/rpn2solr.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/diagbib1.h>
18 #include <yaz/z-core.h>
19 #include <yaz/wrbuf.h>
20
21 static void wrbuf_vputs(const char *buf, void *client_data)
22 {
23     wrbuf_write((WRBUF) client_data, buf, strlen(buf));
24 }
25
26 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
27 {
28     int j;
29     int server_choice = 1;
30     for (j = 0; j < attributes->num_attributes; j++)
31     {
32         Z_AttributeElement *ae = attributes->attributes[j];
33         if (*ae->attributeType == 1) /* use attribute */
34         {
35             if (ae->which == Z_AttributeValue_complex)
36             {
37                 Z_ComplexAttribute *ca = ae->value.complex;
38                 int i;
39                 for (i = 0; i < ca->num_list; i++)
40                 {
41                     Z_StringOrNumeric *son = ca->list[i];
42                     if (son->which == Z_StringOrNumeric_string)
43                         return son->u.string;
44                 }
45             }
46             server_choice = 0; /* not serverChoice because we have use attr */
47         }
48     }
49     if (server_choice)
50         return "solr.serverChoice";
51     return 0;
52 }
53
54 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
55 {
56     int j;
57     for (j = 0; j < attributes->num_attributes; j++)
58     {
59         Z_AttributeElement *ae = attributes->attributes[j];
60         if (*ae->attributeType == 2) /* relation attribute */
61         {
62             if (ae->which == Z_AttributeValue_numeric)
63             {
64                 /* Only support for numeric relation */
65                 Odr_int *relation = ae->value.numeric;
66                 /* map this numeric to representation in SOLR */
67                 switch (*relation)
68                 {
69                     /* Unsure on whether this is the relation attribute constants? */
70                 case Z_ProximityOperator_Prox_lessThan: 
71                     return 0;
72                 case Z_ProximityOperator_Prox_lessThanOrEqual: 
73                     return 0;
74                 case Z_ProximityOperator_Prox_equal: 
75                     return ":";
76                 case Z_ProximityOperator_Prox_greaterThanOrEqual: 
77                     return 0;
78                 case Z_ProximityOperator_Prox_greaterThan: 
79                     return 0;
80                 case Z_ProximityOperator_Prox_notEqual: 
81                     return 0;
82                 case 100: 
83                     /* phonetic is not implemented*/
84                     return 0; 
85                 case 101: 
86                     /* stem is not not implemented */
87                     return 0; 
88                 case 102: 
89                     /* relevance is supported in SOLR, but not implemented yet */
90                     return 0; 
91                 default:
92                     /* Invalid relation */
93                     return 0;
94                 }
95             }
96             else {
97                 /*  Can we have a complex relation value?
98                     Should we implement something?
99                 */
100             }
101         }
102     }
103     return ":";
104 }
105
106 static int rpn2solr_attr(solr_transform_t ct,
107                         Z_AttributeList *attributes, WRBUF w)
108 {
109     const char *relation = solr_lookup_reverse(ct, "relation.", attributes);
110     const char *index = solr_lookup_reverse(ct, "index.", attributes);
111     const char *structure = solr_lookup_reverse(ct, "structure.", attributes);
112
113     /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
114     if (!index)
115         index = lookup_index_from_string_attr(attributes);
116
117     /* Attempt to fix bug #2978: Look for a relation attribute */
118     if (!relation) 
119         relation = lookup_relation_index_from_attr(attributes);
120
121     if (!index)
122     {
123         solr_transform_set_error(ct,
124                                 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
125         return -1;
126     }
127     /* for serverChoice we omit index+relation+structure */
128     if (strcmp(index, "solr.serverChoice"))
129     {
130         wrbuf_puts(w, index);
131         if (relation)
132         {
133             if (!strcmp(relation, "exact"))
134                 /* TODO Verify if a exact  SOLR exists */
135                 relation = ":";
136             else if (!strcmp(relation, "eq"))
137                 relation = ":";
138             else if (!strcmp(relation, "le")) {
139                 /* TODO Not support as such, but could perhaps be transformed into a range
140                 relation = ":[ * to ";
141                 close_range = "]"
142                 */
143             }
144             else if (!strcmp(relation, "ge")) {
145                 /* TODO Not support as such, but could perhaps be transformed into a range
146                 relation = "[";
147                 relation = ":[ * to ";
148                 close_range = "]"
149                 */
150             }
151             /* Missing mapping of not equal, phonetic, stem and relevance */
152             wrbuf_puts(w, relation);
153         }
154         else
155             wrbuf_puts(w, ":");
156
157         if (structure)
158         {
159             if (strcmp(structure, "*"))
160             {
161                 wrbuf_puts(w, "/");
162                 wrbuf_puts(w, structure);
163                 wrbuf_puts(w, " ");
164             }
165         }
166     }
167     return 0;
168 }
169
170 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
171 static int checkForTruncation(int flag, Z_AttributeList *attributes)
172 {
173     int j;
174     for (j = 0; j < attributes->num_attributes; j++)
175     {
176         Z_AttributeElement *ae = attributes->attributes[j];
177         if (*ae->attributeType == 5) /* truncation attribute */
178         {
179             if (ae->which == Z_AttributeValue_numeric)
180             {
181                 Odr_int truncation = *(ae->value.numeric);
182                 /* This logic only works for Left, right and both. eg. 1,2,3 */
183                 if (truncation <= 3)
184                     return ((int) truncation & flag);
185             }
186             /* Complex: Shouldn't happen */
187         }
188     }
189     /* No truncation or unsupported */
190     return 0;
191 };
192
193 static int checkForLeftTruncation(Z_AttributeList *attributes) {
194         return checkForTruncation(1, attributes);
195 }
196
197 static int checkForRightTruncation(Z_AttributeList *attributes) {
198         return checkForTruncation(2, attributes);
199 };
200
201 static int rpn2solr_simple(solr_transform_t ct,
202                           void (*pr)(const char *buf, void *client_data),
203                           void *client_data,
204                           Z_Operand *q, WRBUF w)
205 {
206     int ret = 0;
207     if (q->which != Z_Operand_APT)
208     {
209         ret = -1;
210         solr_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
211     }
212     else
213     {
214         Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
215         Z_Term *term = apt->term;
216         const char *sterm = 0;
217         size_t lterm = 0;
218
219         wrbuf_rewind(w);
220         ret = rpn2solr_attr(ct, apt->attributes, w);
221
222         switch(term->which)
223         {
224         case Z_Term_general:
225             lterm = term->u.general->len;
226             sterm = (const char *) term->u.general->buf;
227             break;
228         case Z_Term_numeric:
229             wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
230             break;
231         case Z_Term_characterString:
232             sterm = term->u.characterString;
233             lterm = strlen(sterm);
234             break;
235         default:
236             ret = -1;
237             solr_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
238         }
239
240         if (term)
241         {
242             size_t i;
243             int must_quote = 0;
244             for (i = 0 ; i < lterm; i++)
245                 if (sterm[i] == ' ')
246                     must_quote = 1;
247             if (must_quote)
248                 wrbuf_puts(w, "\"");
249             /* Bug 2878: Check and add Truncation */
250                         if (checkForLeftTruncation(apt->attributes))
251                 wrbuf_puts(w, "*");
252             wrbuf_write(w, sterm, lterm);
253             /* Bug 2878: Check and add Truncation */
254                         if (checkForRightTruncation(apt->attributes))
255                 wrbuf_puts(w, "*");
256             if (must_quote)
257                 wrbuf_puts(w, "\"");
258         }
259         if (ret == 0)
260             pr(wrbuf_cstr(w), client_data);
261     }
262     return ret;
263 }
264
265
266 static int rpn2solr_structure(solr_transform_t ct,
267                              void (*pr)(const char *buf, void *client_data),
268                              void *client_data,
269                              Z_RPNStructure *q, int nested,
270                              WRBUF w)
271 {
272     if (q->which == Z_RPNStructure_simple)
273         return rpn2solr_simple(ct, pr, client_data, q->u.simple, w);
274     else
275     {
276         Z_Operator *op = q->u.complex->roperator;
277         int r;
278
279         if (nested)
280             pr("(", client_data);
281
282         r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
283         if (r)
284             return r;
285         switch(op->which)
286         {
287         case  Z_Operator_and:
288             pr(" AND ", client_data);
289             break;
290         case  Z_Operator_or:
291             pr(" OR ", client_data);
292             break;
293         case  Z_Operator_and_not:
294             pr(" AND NOT ", client_data);
295             break;
296         case  Z_Operator_prox:
297             solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
298             return -1;
299         }
300         r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
301         if (nested)
302             pr(")", client_data);
303         return r;
304     }
305 }
306
307 int solr_transform_rpn2solr_stream(solr_transform_t ct,
308                                  void (*pr)(const char *buf, void *client_data),
309                                  void *client_data,
310                                  Z_RPNQuery *q)
311 {
312     int r;
313     WRBUF w = wrbuf_alloc();
314     solr_transform_set_error(ct, 0, 0);
315     r = rpn2solr_structure(ct, pr, client_data, q->RPNStructure, 0, w);
316     wrbuf_destroy(w);
317     return r;
318 }
319
320
321 int solr_transform_rpn2solr_wrbuf(solr_transform_t ct,
322                                 WRBUF w,
323                                 Z_RPNQuery *q)
324 {
325     return solr_transform_rpn2solr_stream(ct, wrbuf_vputs, w, q);
326 }
327
328 /*
329  * Local variables:
330  * c-basic-offset: 4
331  * c-file-style: "Stroustrup"
332  * indent-tabs-mode: nil
333  * End:
334  * vim: shiftwidth=4 tabstop=8 expandtab
335  */
336