Merge branch 'master' into sru_2_0
[yaz-moved-to-github.git] / src / rpn2solr.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2013 Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief Implements RPN to SOLR conversion
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <yaz/rpn2solr.h>
17 #include <yaz/xmalloc.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz/z-core.h>
20 #include <yaz/wrbuf.h>
21
22 static void wrbuf_vputs(const char *buf, void *client_data)
23 {
24     wrbuf_write((WRBUF) client_data, buf, strlen(buf));
25 }
26
27 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
28 {
29     int j;
30     int server_choice = 1;
31     for (j = 0; j < attributes->num_attributes; j++)
32     {
33         Z_AttributeElement *ae = attributes->attributes[j];
34         if (*ae->attributeType == 1) /* use attribute */
35         {
36             if (ae->which == Z_AttributeValue_complex)
37             {
38                 Z_ComplexAttribute *ca = ae->value.complex;
39                 int i;
40                 for (i = 0; i < ca->num_list; i++)
41                 {
42                     Z_StringOrNumeric *son = ca->list[i];
43                     if (son->which == Z_StringOrNumeric_string)
44                         return son->u.string;
45                 }
46             }
47             server_choice = 0; /* not serverChoice because we have use attr */
48         }
49     }
50     if (server_choice)
51         return "cql.serverChoice";
52     return 0;
53 }
54
55 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
56 {
57     int j;
58     for (j = 0; j < attributes->num_attributes; j++)
59     {
60         Z_AttributeElement *ae = attributes->attributes[j];
61         if (*ae->attributeType == 2) /* relation attribute */
62         {
63             if (ae->which == Z_AttributeValue_numeric)
64             {
65                 /* Only support for numeric relation */
66                 Odr_int *relation = ae->value.numeric;
67                 /* map this numeric to representation in SOLR */
68                 switch (*relation)
69                 {
70                     /* Unsure on whether this is the relation attribute constants? */
71                 case Z_ProximityOperator_Prox_lessThan:
72                     return "<";
73                 case Z_ProximityOperator_Prox_lessThanOrEqual:
74                     return "le";
75                 case Z_ProximityOperator_Prox_equal:
76                     return ":";
77                 case Z_ProximityOperator_Prox_greaterThanOrEqual:
78                     return "ge";
79                 case Z_ProximityOperator_Prox_greaterThan:
80                     return ">";
81                 case Z_ProximityOperator_Prox_notEqual:
82                     return 0;
83                 case 100:
84                     /* phonetic is not implemented */
85                     return 0;
86                 case 101:
87                     /* stem is not not implemented */
88                     return 0;
89                 case 102:
90                     /* relevance is supported in SOLR, but not implemented yet */
91                     return 0;
92                 default:
93                     /* Invalid relation */
94                     return 0;
95                 }
96             }
97             else {
98                 /*  Can we have a complex relation value?
99                     Should we implement something?
100                 */
101             }
102         }
103     }
104     return ":";
105 }
106
107 static int check_range(solr_transform_t ct, Z_Complex *q,
108                        Z_AttributesPlusTerm **p_apt1,
109                        Z_AttributesPlusTerm **p_apt2)
110 {
111     Z_Operator *op = q->roperator;
112     if (op->which == Z_Operator_and &&
113         q->s1->which == Z_RPNStructure_simple &&
114         q->s2->which == Z_RPNStructure_simple &&
115         q->s1->u.simple->which == Z_Operand_APT &&
116         q->s2->u.simple->which == Z_Operand_APT)
117     {
118         Z_AttributesPlusTerm *apt1 = q->s1->u.simple->u.attributesPlusTerm;
119         Z_AttributesPlusTerm *apt2 = q->s2->u.simple->u.attributesPlusTerm;
120         const char *i1 = solr_lookup_reverse(ct, "index.", apt1->attributes);
121         const char *i2 = solr_lookup_reverse(ct, "index.", apt2->attributes);
122         const char *rel1 = solr_lookup_reverse(ct, "relation.",
123                                                apt1->attributes);
124         const char *rel2 = solr_lookup_reverse(ct, "relation.",
125                                                apt2->attributes);
126         if (!rel1)
127             rel1 = lookup_relation_index_from_attr(apt1->attributes);
128         if (!rel2)
129             rel2 = lookup_relation_index_from_attr(apt2->attributes);
130         if (!i1)
131             i1 = lookup_index_from_string_attr(apt1->attributes);
132         if (!i2)
133             i2 = lookup_index_from_string_attr(apt2->attributes);
134         if (i1 && i2 && !strcmp(i1, i2) && rel1 && rel2)
135         {
136             if ((rel1[0] == '>' || rel1[0] == 'g') &&
137                 (rel2[0] == '<' || rel2[0] == 'l'))
138             {
139                 *p_apt1 = apt1;
140                 *p_apt2 = apt2;
141                 return 1;
142             }
143             if ((rel2[0] == '>' || rel2[0] == 'g') &&
144                 (rel1[0] == '<' || rel1[0] == 'l'))
145             {
146                 *p_apt1 = apt2;
147                 *p_apt2 = apt1;
148                 return 1;
149             }
150         }
151     }
152     return 0;
153 }
154
155 static int rpn2solr_attr(solr_transform_t ct,
156                          Z_AttributeList *attributes, WRBUF w)
157 {
158     const char *index = solr_lookup_reverse(ct, "index.", attributes);
159     const char *structure = solr_lookup_reverse(ct, "structure.", attributes);
160
161     /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
162     if (!index)
163         index = lookup_index_from_string_attr(attributes);
164     if (!index)
165     {
166         solr_transform_set_error(ct,
167                                  YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
168         return -1;
169     }
170     /* for serverChoice we omit index+relation+structure */
171     if (strcmp(index, "cql.serverChoice"))
172     {
173         wrbuf_puts(w, index);
174         wrbuf_puts(w, ":");
175         if (structure)
176         {
177             if (strcmp(structure, "*"))
178             {
179                 wrbuf_puts(w, "/");
180                 wrbuf_puts(w, structure);
181                 wrbuf_puts(w, " ");
182             }
183         }
184     }
185     return 0;
186 }
187
188 static Odr_int get_truncation(Z_AttributesPlusTerm *apt)
189 {
190     int j;
191     Z_AttributeList *attributes = apt->attributes;
192     for (j = 0; j < attributes->num_attributes; j++)
193     {
194         Z_AttributeElement *ae = attributes->attributes[j];
195         if (*ae->attributeType == 5) /* truncation attribute */
196         {
197             if (ae->which == Z_AttributeValue_numeric)
198             {
199                 return *(ae->value.numeric);
200             }
201             else if (ae->which == Z_AttributeValue_complex) {
202                 ;
203                 //yaz_log(YLOG_DEBUG, "Z_Attribute_complex");
204                 /* Complex: Shouldn't happen */
205             }
206         }
207     }
208     /* No truncation given */
209     return 0;
210 }
211
212 #define SOLR_SPECIAL "+-&|!(){}[]^\"~*?:\\"
213
214 static int emit_term(solr_transform_t ct, WRBUF w, Z_Term *term, Odr_int trunc)
215 {
216     size_t lterm = 0;
217     const char *sterm = 0;
218     switch (term->which)
219     {
220     case Z_Term_general:
221         lterm = term->u.general->len;
222         sterm = (const char *) term->u.general->buf;
223         break;
224     case Z_Term_numeric:
225         wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
226         break;
227     case Z_Term_characterString:
228         sterm = term->u.characterString;
229         lterm = strlen(sterm);
230         break;
231     default:
232         solr_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
233         return -1;
234     }
235     if (sterm)
236     {
237         size_t i;
238         int must_quote = 0;
239
240         for (i = 0 ; i < lterm; i++)
241             if (sterm[i] == ' ')
242                 must_quote = 1;
243         if (must_quote)
244             wrbuf_puts(w, "\"");
245         for (i = 0 ; i < lterm; i++)
246         {
247             if (sterm[i] == '\\' && i < lterm - 1)
248             {
249                 i++;
250                 if (strchr(SOLR_SPECIAL, sterm[i]))
251                     wrbuf_putc(w, '\\');
252                 wrbuf_putc(w, sterm[i]);
253             }
254             else if (sterm[i] == '?' && trunc == 104)
255             {
256                 wrbuf_putc(w, '*');
257             }
258             else if (sterm[i] == '#' && trunc == 104)
259             {
260                 wrbuf_putc(w, '?');
261             }
262             else if (strchr(SOLR_SPECIAL, sterm[i]))
263             {
264                 wrbuf_putc(w, '\\');
265                 wrbuf_putc(w, sterm[i]);
266             }
267             else
268                 wrbuf_putc(w, sterm[i]);
269         }
270         if (trunc == 1)
271             wrbuf_puts(w, "*");
272         if (must_quote)
273             wrbuf_puts(w, "\"");
274     }
275     return 0;
276 }
277
278 static int rpn2solr_simple(solr_transform_t ct,
279                            void (*pr)(const char *buf, void *client_data),
280                            void *client_data,
281                            Z_AttributesPlusTerm *apt, WRBUF w,
282                            Z_AttributesPlusTerm *apt2)
283  {
284      int ret = 0;
285      Z_Term *term = apt->term;
286      Odr_int trunc = get_truncation(apt);
287      const char *relation2 = 0;
288      const char *relation1 = solr_lookup_reverse(ct, "relation.",
289                                                  apt->attributes);
290      /* Attempt to fix bug #2978: Look for a relation attribute */
291      if (!relation1)
292          relation1 = lookup_relation_index_from_attr(apt->attributes);
293      if (!relation1)
294      {
295          solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE, 0);
296          return -1;
297      }
298      if (apt2)
299      {
300          relation2 = solr_lookup_reverse(ct, "relation.",
301                                          apt2->attributes);
302          if (!relation2)
303              relation2 = lookup_relation_index_from_attr(apt2->attributes);
304      }
305      wrbuf_rewind(w);
306      ret = rpn2solr_attr(ct, apt->attributes, w);
307      if (ret)
308          return ret;
309      if (trunc == 0 || trunc == 1 || trunc == 100 || trunc == 104)
310              ;
311      else
312      {
313          solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE, 0);
314          return -1;
315      }
316
317      if (!relation1)
318          ret = emit_term(ct, w, term, trunc);
319      else if (relation1[0] == '<' || relation1[0] == 'l')
320      {
321          wrbuf_puts(w, "[* TO ");
322          ret = emit_term(ct, w, term, trunc);
323          if (!strcmp(relation1, "le") || !strcmp(relation1, "<="))
324              wrbuf_puts(w, "]");
325          else
326              wrbuf_puts(w, "}");
327      }
328      else if (relation1[0] == '>' || relation1[0] == 'g')
329      {
330          if (!strcmp(relation1, ">=") || !strcmp(relation1, "ge"))
331              wrbuf_puts(w, "[");
332          else
333              wrbuf_puts(w, "{");
334          ret = emit_term(ct, w, term, trunc);
335          wrbuf_puts(w, " TO ");
336          if (apt2)
337          {
338              emit_term(ct, w, apt2->term, 0);
339              if (!relation2 || !strcmp(relation2, "<=") ||
340                  !strcmp(relation2, "le"))
341                  wrbuf_puts(w, "]");
342              else
343                  wrbuf_puts(w, "}");
344          }
345          else
346              wrbuf_puts(w, "*]");
347      }
348      else
349          ret = emit_term(ct, w, term, trunc);
350      if (ret == 0)
351          pr(wrbuf_cstr(w), client_data);
352      return ret;
353  }
354
355
356 static int rpn2solr_structure(solr_transform_t ct,
357                               void (*pr)(const char *buf, void *client_data),
358                                void *client_data,
359                               Z_RPNStructure *q, int nested,
360                               WRBUF w)
361 {
362     if (q->which == Z_RPNStructure_simple)
363     {
364         if (q->u.simple->which != Z_Operand_APT)
365         {
366             solr_transform_set_error(
367                 ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
368             return -1;
369         }
370         else
371             return rpn2solr_simple(ct, pr, client_data,
372                                    q->u.simple->u.attributesPlusTerm, w, 0);
373     }
374     else
375     {
376         Z_Operator *op = q->u.complex->roperator;
377         Z_AttributesPlusTerm *apt1, *apt2;
378         int r;
379
380         if (check_range(ct, q->u.complex, &apt1, &apt2))
381             return rpn2solr_simple(ct, pr, client_data, apt1, w, apt2);
382         if (nested)
383             pr("(", client_data);
384
385         r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
386         if (r)
387             return r;
388         switch(op->which)
389         {
390         case  Z_Operator_and:
391             pr(" AND ", client_data);
392             break;
393         case  Z_Operator_or:
394             pr(" OR ", client_data);
395             break;
396         case  Z_Operator_and_not:
397             pr(" AND NOT ", client_data);
398             break;
399         case  Z_Operator_prox:
400             solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
401             return -1;
402         }
403         r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
404         if (nested)
405             pr(")", client_data);
406         return r;
407     }
408 }
409
410 int solr_transform_rpn2solr_stream(solr_transform_t ct,
411                                    void (*pr)(const char *buf, void *client_data),
412                                    void *client_data,
413                                    Z_RPNQuery *q)
414 {
415     int r;
416     WRBUF w = wrbuf_alloc();
417     solr_transform_set_error(ct, 0, 0);
418     r = rpn2solr_structure(ct, pr, client_data, q->RPNStructure, 0, w);
419     wrbuf_destroy(w);
420     return r;
421 }
422
423
424 int solr_transform_rpn2solr_wrbuf(solr_transform_t ct,
425                                   WRBUF w,
426                                   Z_RPNQuery *q)
427 {
428     return solr_transform_rpn2solr_stream(ct, wrbuf_vputs, w, q);
429 }
430
431 /*
432  * Local variables:
433  * c-basic-offset: 4
434  * c-file-style: "Stroustrup"
435  * indent-tabs-mode: nil
436  * End:
437  * vim: shiftwidth=4 tabstop=8 expandtab
438  */
439