1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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 const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
25 int server_choice = 1;
26 for (j = 0; j < attributes->num_attributes; j++)
28 Z_AttributeElement *ae = attributes->attributes[j];
29 if (*ae->attributeType == 1) /* use attribute */
31 if (ae->which == Z_AttributeValue_complex)
33 Z_ComplexAttribute *ca = ae->value.complex;
35 for (i = 0; i < ca->num_list; i++)
37 Z_StringOrNumeric *son = ca->list[i];
38 if (son->which == Z_StringOrNumeric_string)
42 server_choice = 0; /* not serverChoice because we have use attr */
46 return "cql.serverChoice";
50 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
53 for (j = 0; j < attributes->num_attributes; j++)
55 Z_AttributeElement *ae = attributes->attributes[j];
56 if (*ae->attributeType == 2) /* relation attribute */
58 if (ae->which == Z_AttributeValue_numeric)
60 /* Only support for numeric relation */
61 Odr_int *relation = ae->value.numeric;
62 /* map this numeric to representation in SOLR */
65 /* Unsure on whether this is the relation attribute constants? */
66 case Z_ProximityOperator_Prox_lessThan:
68 case Z_ProximityOperator_Prox_lessThanOrEqual:
70 case Z_ProximityOperator_Prox_equal:
72 case Z_ProximityOperator_Prox_greaterThanOrEqual:
74 case Z_ProximityOperator_Prox_greaterThan:
76 case Z_ProximityOperator_Prox_notEqual:
79 /* phonetic is not implemented */
82 /* stem is not not implemented */
85 /* relevance is supported in SOLR, but not implemented yet */
88 /* Invalid relation */
93 /* Can we have a complex relation value?
94 Should we implement something?
102 static int check_range(solr_transform_t ct, Z_Complex *q,
103 Z_AttributesPlusTerm **p_apt1,
104 Z_AttributesPlusTerm **p_apt2)
106 Z_Operator *op = q->roperator;
107 if (op->which == Z_Operator_and &&
108 q->s1->which == Z_RPNStructure_simple &&
109 q->s2->which == Z_RPNStructure_simple &&
110 q->s1->u.simple->which == Z_Operand_APT &&
111 q->s2->u.simple->which == Z_Operand_APT)
113 Z_AttributesPlusTerm *apt1 = q->s1->u.simple->u.attributesPlusTerm;
114 Z_AttributesPlusTerm *apt2 = q->s2->u.simple->u.attributesPlusTerm;
115 const char *i1 = solr_lookup_reverse(ct, "index.", apt1->attributes);
116 const char *i2 = solr_lookup_reverse(ct, "index.", apt2->attributes);
117 const char *rel1 = solr_lookup_reverse(ct, "relation.",
119 const char *rel2 = solr_lookup_reverse(ct, "relation.",
122 rel1 = lookup_relation_index_from_attr(apt1->attributes);
124 rel2 = lookup_relation_index_from_attr(apt2->attributes);
126 i1 = lookup_index_from_string_attr(apt1->attributes);
128 i2 = lookup_index_from_string_attr(apt2->attributes);
129 if (i1 && i2 && !strcmp(i1, i2) && rel1 && rel2)
131 if ((rel1[0] == '>' || rel1[0] == 'g') &&
132 (rel2[0] == '<' || rel2[0] == 'l'))
138 if ((rel2[0] == '>' || rel2[0] == 'g') &&
139 (rel1[0] == '<' || rel1[0] == 'l'))
150 static int rpn2solr_attr(solr_transform_t ct,
151 Z_AttributeList *attributes, WRBUF w)
153 const char *index = solr_lookup_reverse(ct, "index.", attributes);
154 const char *structure = solr_lookup_reverse(ct, "structure.", attributes);
156 /* if no real match, try string attribute */
158 index = lookup_index_from_string_attr(attributes);
160 return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
161 /* for serverChoice we omit index+relation+structure */
162 if (strcmp(index, "cql.serverChoice"))
164 wrbuf_puts(w, index);
168 if (strcmp(structure, "*"))
171 wrbuf_puts(w, structure);
179 static Odr_int get_truncation(Z_AttributesPlusTerm *apt)
182 Z_AttributeList *attributes = apt->attributes;
183 for (j = 0; j < attributes->num_attributes; j++)
185 Z_AttributeElement *ae = attributes->attributes[j];
186 if (*ae->attributeType == 5) /* truncation attribute */
188 if (ae->which == Z_AttributeValue_numeric)
190 return *(ae->value.numeric);
192 else if (ae->which == Z_AttributeValue_complex) {
194 //yaz_log(YLOG_DEBUG, "Z_Attribute_complex");
195 /* Complex: Shouldn't happen */
199 /* No truncation given */
203 #define SOLR_SPECIAL "+-&|!(){}[]^\"~*?:\\"
205 static int emit_term(solr_transform_t ct, WRBUF w, Z_Term *term, Odr_int trunc)
208 const char *sterm = 0;
212 lterm = term->u.general->len;
213 sterm = (const char *) term->u.general->buf;
216 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
218 case Z_Term_characterString:
219 sterm = term->u.characterString;
220 lterm = strlen(sterm);
223 return YAZ_BIB1_TERM_TYPE_UNSUPP;
231 for (i = 0 ; i < lterm; i++)
236 if (trunc == 2 || trunc == 3)
238 for (i = 0 ; i < lterm; i++)
240 if (sterm[i] == '\\' && i < lterm - 1)
243 if (strchr(SOLR_SPECIAL, sterm[i]))
245 wrbuf_putc(w, sterm[i]);
247 else if (sterm[i] == '?' && trunc == 104)
251 else if (sterm[i] == '#' && trunc == 104)
255 else if (strchr(SOLR_SPECIAL, sterm[i]))
258 wrbuf_putc(w, sterm[i]);
261 wrbuf_putc(w, sterm[i]);
263 if (trunc == 1 || trunc == 3)
271 static int rpn2solr_simple(solr_transform_t ct,
272 void (*pr)(const char *buf, void *client_data),
274 Z_AttributesPlusTerm *apt, WRBUF w,
275 Z_AttributesPlusTerm *apt2)
278 Z_Term *term = apt->term;
279 Odr_int trunc = get_truncation(apt);
280 const char *relation2 = 0;
281 const char *relation1 = solr_lookup_reverse(ct, "relation.",
283 /* Attempt to fix bug #2978: Look for a relation attribute */
285 relation1 = lookup_relation_index_from_attr(apt->attributes);
288 return YAZ_BIB1_UNSUPP_RELATION_ATTRIBUTE;
292 relation2 = solr_lookup_reverse(ct, "relation.",
295 relation2 = lookup_relation_index_from_attr(apt2->attributes);
298 ret = rpn2solr_attr(ct, apt->attributes, w);
301 if ((trunc >= 0 && trunc <= 3) || trunc == 100 || trunc == 104)
305 return YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
309 ret = emit_term(ct, w, term, trunc);
310 else if (relation1[0] == '<' || relation1[0] == 'l')
312 wrbuf_puts(w, "[* TO ");
313 ret = emit_term(ct, w, term, trunc);
314 if (!strcmp(relation1, "le") || !strcmp(relation1, "<="))
319 else if (relation1[0] == '>' || relation1[0] == 'g')
321 if (!strcmp(relation1, ">=") || !strcmp(relation1, "ge"))
325 ret = emit_term(ct, w, term, trunc);
326 wrbuf_puts(w, " TO ");
329 emit_term(ct, w, apt2->term, 0);
330 if (!relation2 || !strcmp(relation2, "<=") ||
331 !strcmp(relation2, "le"))
340 ret = emit_term(ct, w, term, trunc);
342 pr(wrbuf_cstr(w), client_data);
347 static int rpn2solr_structure(solr_transform_t ct,
348 void (*pr)(const char *buf, void *client_data),
350 Z_RPNStructure *q, int nested,
353 if (q->which == Z_RPNStructure_simple)
355 if (q->u.simple->which != Z_Operand_APT)
356 return YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM;
358 return rpn2solr_simple(ct, pr, client_data,
359 q->u.simple->u.attributesPlusTerm, w, 0);
363 Z_Operator *op = q->u.complex->roperator;
364 Z_AttributesPlusTerm *apt1, *apt2;
367 if (check_range(ct, q->u.complex, &apt1, &apt2))
368 return rpn2solr_simple(ct, pr, client_data, apt1, w, apt2);
370 pr("(", client_data);
372 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
378 pr(" AND ", client_data);
381 pr(" OR ", client_data);
383 case Z_Operator_and_not:
384 pr(" AND NOT ", client_data);
386 case Z_Operator_prox:
387 return YAZ_BIB1_UNSUPP_SEARCH;
389 r = rpn2solr_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
391 pr(")", client_data);
396 int solr_transform_rpn2solr_stream_r(solr_transform_t ct,
398 void (*pr)(const char *buf, void *client_data),
402 int r = rpn2solr_structure(ct, pr, client_data, q->RPNStructure,
403 /* nested*/ 0, addinfo);
405 wrbuf_rewind(addinfo);
409 int solr_transform_rpn2solr_stream(solr_transform_t ct,
410 void (*pr)(const char *buf, void *client_data),
414 WRBUF w = wrbuf_alloc();
415 int r = solr_transform_rpn2solr_stream_r(ct, w, pr, client_data, q);
417 solr_transform_set_error(ct, r, wrbuf_len(w) ? wrbuf_cstr(w) : 0);
422 int solr_transform_rpn2solr_wrbuf(solr_transform_t ct,
426 return solr_transform_rpn2solr_stream(ct, wrbuf_vp_puts, w, q);
432 * c-file-style: "Stroustrup"
433 * indent-tabs-mode: nil
435 * vim: shiftwidth=4 tabstop=8 expandtab