1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements RPN to SOLR conversion
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>
22 static void wrbuf_vputs(const char *buf, void *client_data)
24 wrbuf_write((WRBUF) client_data, buf, strlen(buf));
27 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
30 int server_choice = 1;
31 for (j = 0; j < attributes->num_attributes; j++)
33 Z_AttributeElement *ae = attributes->attributes[j];
34 if (*ae->attributeType == 1) /* use attribute */
36 if (ae->which == Z_AttributeValue_complex)
38 Z_ComplexAttribute *ca = ae->value.complex;
40 for (i = 0; i < ca->num_list; i++)
42 Z_StringOrNumeric *son = ca->list[i];
43 if (son->which == Z_StringOrNumeric_string)
47 server_choice = 0; /* not serverChoice because we have use attr */
51 return "cql.serverChoice";
55 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
58 for (j = 0; j < attributes->num_attributes; j++)
60 Z_AttributeElement *ae = attributes->attributes[j];
61 if (*ae->attributeType == 2) /* relation attribute */
63 if (ae->which == Z_AttributeValue_numeric)
65 /* Only support for numeric relation */
66 Odr_int *relation = ae->value.numeric;
67 /* map this numeric to representation in SOLR */
70 /* Unsure on whether this is the relation attribute constants? */
71 case Z_ProximityOperator_Prox_lessThan:
73 case Z_ProximityOperator_Prox_lessThanOrEqual:
75 case Z_ProximityOperator_Prox_equal:
77 case Z_ProximityOperator_Prox_greaterThanOrEqual:
79 case Z_ProximityOperator_Prox_greaterThan:
81 case Z_ProximityOperator_Prox_notEqual:
84 /* phonetic is not implemented */
87 /* stem is not not implemented */
90 /* relevance is supported in SOLR, but not implemented yet */
93 /* Invalid relation */
98 /* Can we have a complex relation value?
99 Should we implement something?
107 static int check_range(solr_transform_t ct, Z_Complex *q,
108 Z_AttributesPlusTerm **p_apt1,
109 Z_AttributesPlusTerm **p_apt2)
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)
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.",
124 const char *rel2 = solr_lookup_reverse(ct, "relation.",
127 rel1 = lookup_relation_index_from_attr(apt1->attributes);
129 rel2 = lookup_relation_index_from_attr(apt2->attributes);
131 i1 = lookup_index_from_string_attr(apt1->attributes);
133 i2 = lookup_index_from_string_attr(apt2->attributes);
134 if (i1 && i2 && !strcmp(i1, i2) && rel1 && rel2)
136 if ((rel1[0] == '>' || rel1[0] == 'g') &&
137 (rel2[0] == '<' || rel2[0] == 'l'))
143 if ((rel2[0] == '>' || rel2[0] == 'g') &&
144 (rel1[0] == '<' || rel1[0] == 'l'))
155 static int rpn2solr_attr(solr_transform_t ct,
156 Z_AttributeList *attributes, WRBUF w)
158 const char *index = solr_lookup_reverse(ct, "index.", attributes);
159 const char *structure = solr_lookup_reverse(ct, "structure.", attributes);
161 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
163 index = lookup_index_from_string_attr(attributes);
166 solr_transform_set_error(ct,
167 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
170 /* for serverChoice we omit index+relation+structure */
171 if (strcmp(index, "cql.serverChoice"))
173 wrbuf_puts(w, index);
177 if (strcmp(structure, "*"))
180 wrbuf_puts(w, structure);
188 static Odr_int get_truncation(Z_AttributesPlusTerm *apt)
191 Z_AttributeList *attributes = apt->attributes;
192 for (j = 0; j < attributes->num_attributes; j++)
194 Z_AttributeElement *ae = attributes->attributes[j];
195 if (*ae->attributeType == 5) /* truncation attribute */
197 if (ae->which == Z_AttributeValue_numeric)
199 return *(ae->value.numeric);
201 else if (ae->which == Z_AttributeValue_complex) {
203 //yaz_log(YLOG_DEBUG, "Z_Attribute_complex");
204 /* Complex: Shouldn't happen */
208 /* No truncation given */
212 #define SOLR_SPECIAL "+-&|!(){}[]^\"~*?:\\"
214 static int emit_term(solr_transform_t ct, WRBUF w, Z_Term *term, Odr_int trunc)
217 const char *sterm = 0;
221 lterm = term->u.general->len;
222 sterm = (const char *) term->u.general->buf;
225 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
227 case Z_Term_characterString:
228 sterm = term->u.characterString;
229 lterm = strlen(sterm);
232 solr_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
241 for (i = 0 ; i < lterm; i++)
246 for (i = 0 ; i < lterm; i++)
248 if (sterm[i] == '\\' && i < lterm - 1)
251 if (strchr(SOLR_SPECIAL, sterm[i]))
253 wrbuf_putc(w, sterm[i]);
255 else if (sterm[i] == '?' && trunc == 104)
259 else if (sterm[i] == '#' && trunc == 104)
263 else if (strchr(SOLR_SPECIAL, sterm[i]))
266 wrbuf_putc(w, sterm[i]);
269 wrbuf_putc(w, sterm[i]);
279 static int rpn2solr_simple(solr_transform_t ct,
280 void (*pr)(const char *buf, void *client_data),
282 Z_AttributesPlusTerm *apt, WRBUF w,
283 Z_AttributesPlusTerm *apt2)
286 Z_Term *term = apt->term;
287 Odr_int trunc = get_truncation(apt);
288 const char *relation2 = 0;
289 const char *relation1 = solr_lookup_reverse(ct, "relation.",
291 /* Attempt to fix bug #2978: Look for a relation attribute */
293 relation1 = lookup_relation_index_from_attr(apt->attributes);
296 solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE, 0);
301 relation2 = solr_lookup_reverse(ct, "relation.",
304 relation2 = lookup_relation_index_from_attr(apt2->attributes);
307 ret = rpn2solr_attr(ct, apt->attributes, w);
310 if (trunc == 0 || trunc == 1 || trunc == 100 || trunc == 104)
314 solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE, 0);
319 ret = emit_term(ct, w, term, trunc);
320 else if (relation1[0] == '<' || relation1[0] == 'l')
322 wrbuf_puts(w, "[* TO ");
323 ret = emit_term(ct, w, term, trunc);
324 if (!strcmp(relation1, "le") || !strcmp(relation1, "<="))
329 else if (relation1[0] == '>' || relation1[0] == 'g')
331 if (!strcmp(relation1, ">=") || !strcmp(relation1, "ge"))
335 ret = emit_term(ct, w, term, trunc);
336 wrbuf_puts(w, " TO ");
339 emit_term(ct, w, apt2->term, 0);
340 if (!relation2 || !strcmp(relation2, "<=") ||
341 !strcmp(relation2, "le"))
350 ret = emit_term(ct, w, term, trunc);
352 pr(wrbuf_cstr(w), client_data);
357 static int rpn2solr_structure(solr_transform_t ct,
358 void (*pr)(const char *buf, void *client_data),
360 Z_RPNStructure *q, int nested,
363 if (q->which == Z_RPNStructure_simple)
365 if (q->u.simple->which != Z_Operand_APT)
367 solr_transform_set_error(
368 ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
372 return rpn2solr_simple(ct, pr, client_data,
373 q->u.simple->u.attributesPlusTerm, w, 0);
377 Z_Operator *op = q->u.complex->roperator;
378 Z_AttributesPlusTerm *apt1, *apt2;
381 if (check_range(ct, q->u.complex, &apt1, &apt2))
382 return rpn2solr_simple(ct, pr, client_data, apt1, w, apt2);
384 pr("(", client_data);
386 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
392 pr(" AND ", client_data);
395 pr(" OR ", client_data);
397 case Z_Operator_and_not:
398 pr(" AND NOT ", client_data);
400 case Z_Operator_prox:
401 solr_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
404 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
406 pr(")", client_data);
411 int solr_transform_rpn2solr_stream(solr_transform_t ct,
412 void (*pr)(const char *buf, void *client_data),
417 WRBUF w = wrbuf_alloc();
418 solr_transform_set_error(ct, 0, 0);
419 r = rpn2solr_structure(ct, pr, client_data, q->RPNStructure, 0, w);
425 int solr_transform_rpn2solr_wrbuf(solr_transform_t ct,
429 return solr_transform_rpn2solr_stream(ct, wrbuf_vputs, w, q);
435 * c-file-style: "Stroustrup"
436 * indent-tabs-mode: nil
438 * vim: shiftwidth=4 tabstop=8 expandtab