Get rid of typedefs iSAMS_P, ISAMC_P, ISAMB_P and use ISAM_P instead.
[idzebra-moved-to-github.git] / index / zrpn.c
index bc273e7..caccbcd 100644 (file)
@@ -1,6 +1,6 @@
-/* $Id: zrpn.c,v 1.160 2004-11-15 22:57:25 adam Exp $
-   Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
-   Index Data Aps
+/* $Id: zrpn.c,v 1.173 2005-04-13 13:03:47 adam Exp $
+   Copyright (C) 1995-2005
+   Index Data ApS
 
 This file is part of the Zebra server.
 
@@ -36,6 +36,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 #include <charmap.h>
 #include <rset.h>
 
+/* maximum number of terms in an and/or/phrase item */
+#define TERM_LIST_LENGTH_MAX 256
 
 static const struct key_control it_ctrl =
 { 
@@ -64,6 +66,9 @@ typedef struct
 } AttrType;
 
 
+static int log_level_set = 0;
+static int log_level_rpn = 0;
+
 static const char **rpn_char_map_handler(void *vp, const char **from, int len)
 {
     struct rpn_char_map_info *p = (struct rpn_char_map_info *) vp;
@@ -72,10 +77,10 @@ static const char **rpn_char_map_handler(void *vp, const char **from, int len)
     if (out && *out)
     {
         const char *outp = *out;
-        yaz_log(LOG_LOG, "---");
+        yaz_log(YLOG_LOG, "---");
         while (*outp)
         {
-            yaz_log(LOG_LOG, "%02X", *outp);
+            yaz_log(YLOG_LOG, "%02X", *outp);
             outp++;
         }
     }
@@ -175,7 +180,7 @@ struct grep_info {
 #ifdef TERM_COUNT        
     int *term_no;        
 #endif        
-    ISAMC_P *isam_p_buf;
+    ISAM_P *isam_p_buf;
     int isam_p_size;        
     int isam_p_indx;
     ZebraHandle zh;
@@ -190,7 +195,7 @@ static void term_untrans(ZebraHandle zh, int reg_type,
     while (*src)
     {
         const char *cp = zebra_maps_output(zh->reg->zebra_maps,
-                                            reg_type, &src);
+                                          reg_type, &src);
         if (!cp && len < IT_MAX_WORD-1)
             dst[len++] = *src++;
         else
@@ -201,17 +206,22 @@ static void term_untrans(ZebraHandle zh, int reg_type,
 }
 
 static void add_isam_p(const char *name, const char *info,
-                        struct grep_info *p)
+                      struct grep_info *p)
 {
+    if (!log_level_set)
+    {
+        log_level_rpn = yaz_log_module_level("rpn");
+        log_level_set = 1;
+    }
     if (p->isam_p_indx == p->isam_p_size)
     {
-        ISAMC_P *new_isam_p_buf;
+        ISAM_P *new_isam_p_buf;
 #ifdef TERM_COUNT        
         int *new_term_no;        
 #endif
         p->isam_p_size = 2*p->isam_p_size + 100;
-        new_isam_p_buf = (ISAMC_P *) xmalloc(sizeof(*new_isam_p_buf) *
-                                             p->isam_p_size);
+        new_isam_p_buf = (ISAM_P *) xmalloc(sizeof(*new_isam_p_buf) *
+                                           p->isam_p_size);
         if (p->isam_p_buf)
         {
             memcpy(new_isam_p_buf, p->isam_p_buf,
@@ -221,8 +231,7 @@ static void add_isam_p(const char *name, const char *info,
         p->isam_p_buf = new_isam_p_buf;
 
 #ifdef TERM_COUNT
-        new_term_no = (int *) xmalloc(sizeof(*new_term_no) *
-                                       p->isam_p_size);
+        new_term_no = (int *) xmalloc(sizeof(*new_term_no) * p->isam_p_size);
         if (p->term_no)
         {
             memcpy(new_term_no, p->isam_p_buf,
@@ -245,10 +254,10 @@ static void add_isam_p(const char *name, const char *info,
         int len = key_SU_decode (&su_code, name);
         
         term_untrans  (p->zh, p->reg_type, term_tmp, name+len+1);
-        yaz_log(LOG_LOG, "grep: %d %c %s", su_code, name[len], term_tmp);
+        yaz_log(log_level_rpn, "grep: %d %c %s", su_code, name[len], term_tmp);
         zebraExplain_lookup_ord (p->zh->reg->zei,
                                  su_code, &db, &set, &use);
-        yaz_log(LOG_LOG, "grep:  set=%d use=%d db=%s", set, use, db);
+        yaz_log(log_level_rpn, "grep:  set=%d use=%d db=%s", set, use, db);
         
         resultSetAddTerm(p->zh, p->termset, name[len], db,
                         set, use, term_tmp);
@@ -286,14 +295,41 @@ static int term_pre(ZebraMaps zebra_maps, int reg_type, const char **src,
     return *s0;
 }
 
+
+static void esc_str(char *out_buf, int out_size,
+                   const char *in_buf, int in_size)
+{
+    int k;
+
+    assert(out_buf);
+    assert(in_buf);
+    assert(out_size > 20);
+    *out_buf = '\0';
+    for (k = 0; k<in_size; k++)
+    {
+       int c = in_buf[k] & 0xff;
+       int pc;
+       if (c < 32 || c > 126)
+           pc = '?';
+       else
+           pc = c;
+       sprintf(out_buf +strlen(out_buf), "%02X:%c  ", c, pc);
+       if (strlen(out_buf) > out_size-20)
+       {
+           strcat(out_buf, "..");
+           break;
+       }
+    }
+}
+
 #define REGEX_CHARS " []()|.*+?!"
 
-/* term_100: handle term, where trunc=none(no operators at all) */
+/* term_100: handle term, where trunc = none(no operators at all) */
 static int term_100(ZebraMaps zebra_maps, int reg_type,
                      const char **src, char *dst, int space_split,
                      char *dst_term)
 {
-    const char *s0, *s1;
+    const char *s0;
     const char **map;
     int i = 0;
     int j = 0;
@@ -306,8 +342,10 @@ static int term_100(ZebraMaps zebra_maps, int reg_type,
     s0 = *src;
     while (*s0)
     {
-        s1 = s0;
-        map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
+        const char *s1 = s0;
+       int q_map_match = 0;
+        map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0), 
+                               &q_map_match);
         if (space_split)
         {
             if (**map == *CHR_SPACE)
@@ -334,14 +372,26 @@ static int term_100(ZebraMaps zebra_maps, int reg_type,
                 space_start = space_end = 0;
             }
         }
-        /* add non-space char */
-        while (s1 < s0)
-        {
-            if (strchr(REGEX_CHARS, *s1))
-                dst[i++] = '\\';
-            dst_term[j++] = *s1;
-            dst[i++] = *s1++;
-        }
+       /* add non-space char */
+       memcpy(dst_term+j, s1, s0 - s1);
+       j += (s0 - s1);
+       if (!q_map_match)
+       {
+           while (s1 < s0)
+           {
+               if (strchr(REGEX_CHARS, *s1))
+                   dst[i++] = '\\';
+               dst[i++] = *s1++;
+           }
+       }
+       else
+       {
+           char tmpbuf[80];
+           esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
+           
+           strcpy(dst + i, map[0]);
+           i += strlen(map[0]);
+       }
     }
     dst[i] = '\0';
     dst_term[j] = '\0';
@@ -349,12 +399,12 @@ static int term_100(ZebraMaps zebra_maps, int reg_type,
     return i;
 }
 
-/* term_101: handle term, where trunc=Process # */
+/* term_101: handle term, where trunc = Process # */
 static int term_101(ZebraMaps zebra_maps, int reg_type,
                      const char **src, char *dst, int space_split,
                      char *dst_term)
 {
-    const char *s0, *s1;
+    const char *s0;
     const char **map;
     int i = 0;
     int j = 0;
@@ -372,17 +422,33 @@ static int term_101(ZebraMaps zebra_maps, int reg_type,
         }
         else
         {
-            s1 = s0;
-            map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
+           const char *s1 = s0;
+           int q_map_match = 0;
+           map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0), 
+                                   &q_map_match);
             if (space_split && **map == *CHR_SPACE)
                 break;
-            while (s1 < s0)
-            {
-                if (strchr(REGEX_CHARS, *s1))
-                    dst[i++] = '\\';
-                dst_term[j++] = *s1;
-                dst[i++] = *s1++;
-            }
+
+           /* add non-space char */
+           memcpy(dst_term+j, s1, s0 - s1);
+           j += (s0 - s1);
+           if (!q_map_match)
+           {
+               while (s1 < s0)
+               {
+                   if (strchr(REGEX_CHARS, *s1))
+                       dst[i++] = '\\';
+                   dst[i++] = *s1++;
+               }
+           }
+           else
+           {
+               char tmpbuf[80];
+               esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
+               
+               strcpy(dst + i, map[0]);
+               i += strlen(map[0]);
+           }
         }
     }
     dst[i] = '\0';
@@ -391,21 +457,21 @@ static int term_101(ZebraMaps zebra_maps, int reg_type,
     return i;
 }
 
-/* term_103: handle term, where trunc=re-2 (regular expressions) */
+/* term_103: handle term, where trunc = re-2 (regular expressions) */
 static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
                      char *dst, int *errors, int space_split,
                      char *dst_term)
 {
     int i = 0;
     int j = 0;
-    const char *s0, *s1;
+    const char *s0;
     const char **map;
 
     if (!term_pre(zebra_maps, reg_type, src, "^\\()[].*+?|", "(", !space_split))
         return 0;
     s0 = *src;
     if (errors && *s0 == '+' && s0[1] && s0[2] == '+' && s0[3] &&
-        isdigit(s0[1]))
+        isdigit(((const unsigned char *)s0)[1]))
     {
         *errors = s0[1] - '0';
         s0 += 3;
@@ -421,26 +487,43 @@ static int term_103(ZebraMaps zebra_maps, int reg_type, const char **src,
         }
         else
         {
-            s1 = s0;
-            map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
-            if (**map == *CHR_SPACE)
+           const char *s1 = s0;
+           int q_map_match = 0;
+           map = zebra_maps_search(zebra_maps, reg_type, &s0, strlen(s0), 
+                                   &q_map_match);
+            if (space_split && **map == *CHR_SPACE)
                 break;
-            while (s1 < s0)
-            {
-                if (strchr(REGEX_CHARS, *s1))
-                    dst[i++] = '\\';
-                dst_term[j++] = *s1;
-                dst[i++] = *s1++;
-            }
+
+           /* add non-space char */
+           memcpy(dst_term+j, s1, s0 - s1);
+           j += (s0 - s1);
+           if (!q_map_match)
+           {
+               while (s1 < s0)
+               {
+                   if (strchr(REGEX_CHARS, *s1))
+                       dst[i++] = '\\';
+                   dst[i++] = *s1++;
+               }
+           }
+           else
+           {
+               char tmpbuf[80];
+               esc_str(tmpbuf, sizeof(tmpbuf), map[0], strlen(map[0]));
+               
+               strcpy(dst + i, map[0]);
+               i += strlen(map[0]);
+           }
         }
     }
     dst[i] = '\0';
     dst_term[j] = '\0';
     *src = s0;
+    
     return i;
 }
 
-/* term_103: handle term, where trunc=re-1 (regular expressions) */
+/* term_103: handle term, where trunc = re-1 (regular expressions) */
 static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
                      char *dst, int space_split, char *dst_term)
 {
@@ -449,7 +532,7 @@ static int term_102 (ZebraMaps zebra_maps, int reg_type, const char **src,
 }
 
 
-/* term_104: handle term, where trunc=Process # and ! */
+/* term_104: handle term, where trunc = Process # and ! */
 static int term_104(ZebraMaps zebra_maps, int reg_type,
                      const char **src, char *dst, int space_split,
                      char *dst_term)
@@ -500,6 +583,7 @@ static int term_104(ZebraMaps zebra_maps, int reg_type,
             dst[i++] = '.';
             dst_term[j++] = *s0++;
         }
+       else
         {
             s1 = s0;
             map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
@@ -520,7 +604,7 @@ static int term_104(ZebraMaps zebra_maps, int reg_type,
     return i;
 }
 
-/* term_105/106: handle term, where trunc=Process * and ! and right trunc */
+/* term_105/106: handle term, where trunc = Process * and ! and right trunc */
 static int term_105 (ZebraMaps zebra_maps, int reg_type,
                      const char **src, char *dst, int space_split,
                      char *dst_term, int right_truncate)
@@ -546,6 +630,7 @@ static int term_105 (ZebraMaps zebra_maps, int reg_type,
             dst[i++] = '.';
             dst_term[j++] = *s0++;
         }
+       else
         {
             s1 = s0;
             map = zebra_maps_input(zebra_maps, reg_type, &s0, strlen(s0), 0);
@@ -584,7 +669,7 @@ static void gen_regular_rel(char *dst, int val, int islt)
     int pos = 0;
     char numstr[20];
 
-    yaz_log(LOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
+    yaz_log(YLOG_DEBUG, "gen_regular_rel. val=%d, islt=%d", val, islt);
     if (val >= 0)
     {
         if (islt)
@@ -672,7 +757,7 @@ static void gen_regular_rel(char *dst, int val, int islt)
     {
         /* match everything less than 10^(pos-1) */
         strcat(dst, "0*");
-        for (i=1; i<pos; i++)
+        for (i = 1; i<pos; i++)
             strcat(dst, "[0-9]?");
     }
     else
@@ -716,7 +801,7 @@ static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     attr_init(&relation, zapt, 2);
     relation_value = attr_find(&relation, NULL);
 
-    yaz_log(LOG_DEBUG, "string relation value=%d", relation_value);
+    yaz_log(YLOG_DEBUG, "string relation value=%d", relation_value);
     switch (relation_value)
     {
     case 1:
@@ -724,7 +809,7 @@ static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                        term_sub, term_component,
                        space_split, term_dst))
             return 0;
-        yaz_log(LOG_DEBUG, "Relation <");
+        yaz_log(log_level_rpn, "Relation <");
         
         *term_tmp++ = '(';
         for (i = 0; term_component[i]; )
@@ -757,7 +842,7 @@ static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                        term_sub, term_component,
                        space_split, term_dst))
             return 0;
-        yaz_log(LOG_DEBUG, "Relation <=");
+        yaz_log(log_level_rpn, "Relation <=");
 
         *term_tmp++ = '(';
         for (i = 0; term_component[i]; )
@@ -790,7 +875,7 @@ static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         if (!term_100 (zh->reg->zebra_maps, reg_type,
                        term_sub, term_component, space_split, term_dst))
             return 0;
-        yaz_log(LOG_DEBUG, "Relation >");
+        yaz_log(log_level_rpn, "Relation >");
 
         *term_tmp++ = '(';
         for (i = 0; term_component[i];)
@@ -825,7 +910,7 @@ static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
                        term_component, space_split, term_dst))
             return 0;
-        yaz_log(LOG_DEBUG, "Relation >=");
+        yaz_log(log_level_rpn, "Relation >=");
 
         *term_tmp++ = '(';
         for (i = 0; term_component[i];)
@@ -861,7 +946,7 @@ static int string_relation(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         break;
     case 3:
     default:
-        yaz_log(LOG_DEBUG, "Relation =");
+        yaz_log(log_level_rpn, "Relation =");
         if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub,
                        term_component, space_split, term_dst))
             return 0;
@@ -897,7 +982,7 @@ static RSET term_trunc(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                      term_dst, xpath_use);
     if (r < 1)
         return 0;
-    yaz_log(LOG_DEBUG, "term: %s", term_dst);
+    yaz_log(log_level_rpn, "term: %s", term_dst);
     return rset_trunc(zh, grep_info->isam_p_buf,
                        grep_info->isam_p_indx, term_dst,
                        strlen(term_dst), rank_type, 1 /* preserve pos */,
@@ -938,15 +1023,16 @@ static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     rpn_char_map_prepare (zh->reg, reg_type, &rcmi);
     attr_init (&use, zapt, 1);
     use_value = attr_find_ex (&use, &curAttributeSet, &use_string);
-    yaz_log(LOG_DEBUG, "string_term, use value %d", use_value);
+    yaz_log(log_level_rpn, "string_term, use value %d", use_value);
     attr_init (&truncation, zapt, 5);
     truncation_value = attr_find (&truncation, NULL);
-    yaz_log(LOG_DEBUG, "truncation value %d", truncation_value);
+    yaz_log(log_level_rpn, "truncation value %d", truncation_value);
 
     if (use_value == -1)    /* no attribute - assumy "any" */
         use_value = 1016;
     for (base_no = 0; base_no < num_bases; base_no++)
     {
+       int ord = -1;
        int attr_ok = 0;
        int regex_range = 0;
        int init_pos = 0;
@@ -965,25 +1051,50 @@ static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         }
         if (xpath_use > 0 && use_value == -2) 
         {
-            use_value = xpath_use;
+           /* xpath mode and we have a string attribute */
             attp.local_attributes = &id_xpath_attr;
             attp.attset_ordinal = VAL_IDXPATH;
             id_xpath_attr.next = 0;
+
+            use_value = xpath_use;  /* xpath_use as use-attribute now */
             id_xpath_attr.local = use_value;
         }
-        else if (curAttributeSet == VAL_IDXPATH)
+        else if (curAttributeSet == VAL_IDXPATH && use_value >= 0)
         {
+           /* X-Path attribute, use numeric value directly */
             attp.local_attributes = &id_xpath_attr;
             attp.attset_ordinal = VAL_IDXPATH;
             id_xpath_attr.next = 0;
             id_xpath_attr.local = use_value;
         }
-        else
+       else if (use_string &&
+                (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
+                                                    use_string)) >= 0)
+       {
+           /* we have a match for a raw string attribute */
+            char ord_buf[32];
+            int i, ord_len;
+
+            if (prefix_len)
+                term_dict[prefix_len++] = '|';
+            else
+                term_dict[prefix_len++] = '(';
+            
+            ord_len = key_SU_encode (ord, ord_buf);
+            for (i = 0; i<ord_len; i++)
+            {
+                term_dict[prefix_len++] = 1;
+                term_dict[prefix_len++] = ord_buf[i];
+            }
+            attp.local_attributes = 0;  /* no more attributes */
+       }
+        else 
         {
-            if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
-                                            use_string)))
+           /* lookup in the .att files . Allow string as well */
+            if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
+                                     use_string)))
             {
-                yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
+                yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
                       curAttributeSet, use_value, r);
                 if (r == -1)
                 {
@@ -1010,29 +1121,29 @@ static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
                 continue;
             }
         }
-        for (local_attr = attp.local_attributes; local_attr;
-             local_attr = local_attr->next)
-        {
-            int ord;
-            char ord_buf[32];
-            int i, ord_len;
-            
-            ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
-                                         local_attr->local);
-            if (ord < 0)
-                continue;
-            if (prefix_len)
-                term_dict[prefix_len++] = '|';
-            else
-                term_dict[prefix_len++] = '(';
-            
-            ord_len = key_SU_encode (ord, ord_buf);
-            for (i = 0; i<ord_len; i++)
-            {
-                term_dict[prefix_len++] = 1;
-                term_dict[prefix_len++] = ord_buf[i];
-            }
-        }
+       for (local_attr = attp.local_attributes; local_attr;
+            local_attr = local_attr->next)
+       {
+           char ord_buf[32];
+           int i, ord_len;
+           
+           ord = zebraExplain_lookup_attr_su(zh->reg->zei,
+                                             attp.attset_ordinal,
+                                             local_attr->local);
+           if (ord < 0)
+               continue;
+           if (prefix_len)
+               term_dict[prefix_len++] = '|';
+           else
+               term_dict[prefix_len++] = '(';
+           
+           ord_len = key_SU_encode (ord, ord_buf);
+           for (i = 0; i<ord_len; i++)
+           {
+               term_dict[prefix_len++] = 1;
+               term_dict[prefix_len++] = ord_buf[i];
+           }
+       }
         if (!prefix_len)
         {
 #if 1
@@ -1052,7 +1163,7 @@ static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         term_dict[prefix_len++] = ')';
         term_dict[prefix_len++] = 1;
         term_dict[prefix_len++] = reg_type;
-        yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
+        yaz_log(log_level_rpn, "reg_type = %d", term_dict[prefix_len-1]);
         term_dict[prefix_len] = '\0';
         j = prefix_len;
         switch (truncation_value)
@@ -1136,12 +1247,18 @@ static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         }
        if (attr_ok)
        {
-           yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_dict+prefix_len);
+           char buf[80];
+           const char *input = term_dict + prefix_len;
+           esc_str(buf, sizeof(buf), input, strlen(input));
+       }
+       if (attr_ok)
+       {
+           yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_dict+prefix_len);
            r = dict_lookup_grep(zh->reg->dict, term_dict, regex_range,
                                 grep_info, &max_pos, init_pos,
                                 grep_handle);
            if (r)
-               yaz_log(LOG_WARN, "dict_lookup_grep fail %d", r);
+               yaz_log(YLOG_WARN, "dict_lookup_grep fail %d", r);
        }
     }
     if (!bases_ok)
@@ -1151,7 +1268,7 @@ static int string_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         return -1;
     }
     *term_sub = termp;
-    yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
+    yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
     return 1;
 }
 
@@ -1333,7 +1450,7 @@ static int grep_info_prepare (ZebraHandle zh,
         }
         else
             termset_name = termset_value_string;
-        yaz_log(LOG_LOG, "creating termset set %s", termset_name);
+        yaz_log(log_level_rpn, "creating termset set %s", termset_name);
         grep_info->termset = resultSetAdd (zh, termset_name, 1);
         if (!grep_info->termset)
         {
@@ -1357,8 +1474,8 @@ static RSET rpn_search_APT_phrase (ZebraHandle zh,
                                    NMEM rset_nmem)
 {
     char term_dst[IT_MAX_WORD+1];
-    RSET rset[60], result;
-    int rset_no = 0;
+    RSET rset[TERM_LIST_LENGTH_MAX], result;
+    size_t rset_no = 0;
     struct grep_info grep_info;
     char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
     const char *termp = termz;
@@ -1366,9 +1483,9 @@ static RSET rpn_search_APT_phrase (ZebraHandle zh,
     *term_dst = 0;
     if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
         return 0;
-    while (1)
+    for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
     { 
-        yaz_log(LOG_DEBUG, "APT_phrase termp=%s", termp);
+        yaz_log(log_level_rpn, "APT_phrase termp=%s", termp);
         rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
                                     stream, &grep_info,
                                     reg_type, complete_flag,
@@ -1377,8 +1494,6 @@ static RSET rpn_search_APT_phrase (ZebraHandle zh,
                                     xpath_use,rset_nmem);
         if (!rset[rset_no])
             break;
-        if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
-            break;
     }
     grep_info_delete (&grep_info);
     if (rset_no == 0)
@@ -1405,17 +1520,17 @@ static RSET rpn_search_APT_or_list (ZebraHandle zh,
                                     NMEM rset_nmem)
 {
     char term_dst[IT_MAX_WORD+1];
-    RSET rset[60];
-    int rset_no = 0;
+    RSET rset[TERM_LIST_LENGTH_MAX];
+    size_t rset_no = 0;
     struct grep_info grep_info;
     char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
     const char *termp = termz;
 
     if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
         return 0;
-    while (1)
+    for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
     { 
-        yaz_log(LOG_DEBUG, "APT_or_list termp=%s", termp);
+        yaz_log(log_level_rpn, "APT_or_list termp=%s", termp);
         rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
                                     stream, &grep_info,
                                     reg_type, complete_flag,
@@ -1424,14 +1539,12 @@ static RSET rpn_search_APT_or_list (ZebraHandle zh,
                                     xpath_use,rset_nmem);
         if (!rset[rset_no])
             break;
-        if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
-            break;
     }
     grep_info_delete (&grep_info);
     if (rset_no == 0)
         return rsnull_create (rset_nmem,key_it_ctrl);  
-    return rsmultior_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
-                            rset_no, rset);
+    return rsmulti_or_create(rset_nmem, key_it_ctrl,key_it_ctrl->scope,
+                            rset_no, rset);
 }
 
 static RSET rpn_search_APT_and_list (ZebraHandle zh,
@@ -1446,17 +1559,17 @@ static RSET rpn_search_APT_and_list (ZebraHandle zh,
                                      NMEM rset_nmem)
 {
     char term_dst[IT_MAX_WORD+1];
-    RSET rset[60]; /* FIXME - bug 160 - should be dynamic somehow */
-    int rset_no = 0;
+    RSET rset[TERM_LIST_LENGTH_MAX];
+    size_t rset_no = 0;
     struct grep_info grep_info;
     char *termz = normalize_term(zh, zapt, termz_org, stream, reg_type);
     const char *termp = termz;
 
     if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
         return 0;
-    while (1)
+    for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
     { 
-        yaz_log(LOG_DEBUG, "APT_and_list termp=%s", termp);
+        yaz_log(log_level_rpn, "APT_and_list termp=%s", termp);
         rset[rset_no] = term_trunc(zh, zapt, &termp, attributeSet,
                                     stream, &grep_info,
                                     reg_type, complete_flag,
@@ -1465,16 +1578,13 @@ static RSET rpn_search_APT_and_list (ZebraHandle zh,
                                     xpath_use, rset_nmem);
         if (!rset[rset_no])
             break;
-        assert (rset[rset_no]);
-        if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
-            break;
     }
     grep_info_delete (&grep_info);
     if (rset_no == 0)
-        return rsnull_create (rset_nmem,key_it_ctrl); 
-
-    return rsmultiand_create( rset_nmem, key_it_ctrl, key_it_ctrl->scope,
-                              rset_no, rset);
+        return rsnull_create(rset_nmem,key_it_ctrl); 
+    
+    return rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
+                             rset_no, rset);
 }
 
 static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
@@ -1495,7 +1605,7 @@ static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     attr_init (&relation, zapt, 2);
     relation_value = attr_find (&relation, NULL);
 
-    yaz_log(LOG_DEBUG, "numeric relation value=%d", relation_value);
+    yaz_log(log_level_rpn, "numeric relation value=%d", relation_value);
 
     if (!term_100 (zh->reg->zebra_maps, reg_type, term_sub, term_tmp, 1,
                    term_dst))
@@ -1504,32 +1614,32 @@ static int numeric_relation (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     switch (relation_value)
     {
     case 1:
-        yaz_log(LOG_DEBUG, "Relation <");
+        yaz_log(log_level_rpn, "Relation <");
         gen_regular_rel (term_tmp, term_value-1, 1);
         break;
     case 2:
-        yaz_log(LOG_DEBUG, "Relation <=");
+        yaz_log(log_level_rpn, "Relation <=");
         gen_regular_rel (term_tmp, term_value, 1);
         break;
     case 4:
-        yaz_log(LOG_DEBUG, "Relation >=");
+        yaz_log(log_level_rpn, "Relation >=");
         gen_regular_rel (term_tmp, term_value, 0);
         break;
     case 5:
-        yaz_log(LOG_DEBUG, "Relation >");
+        yaz_log(log_level_rpn, "Relation >");
         gen_regular_rel (term_tmp, term_value+1, 0);
         break;
     case 3:
     default:
-        yaz_log(LOG_DEBUG, "Relation =");
+        yaz_log(log_level_rpn, "Relation =");
         sprintf (term_tmp, "(0*%d)", term_value);
     }
-    yaz_log(LOG_DEBUG, "dict_lookup_grep: %s", term_tmp);
+    yaz_log(log_level_rpn, "dict_lookup_grep: %s", term_tmp);
     r = dict_lookup_grep(zh->reg->dict, term_dict, 0, grep_info, max_pos,
                           0, grep_handle);
     if (r)
-        yaz_log(LOG_WARN, "dict_lookup_grep fail, rel=gt: %d", r);
-    yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
+        yaz_log(YLOG_WARN, "dict_lookup_grep fail, rel = gt: %d", r);
+    yaz_log(log_level_rpn, "%d positions", grep_info->isam_p_indx);
     return 1;
 }
 
@@ -1585,10 +1695,10 @@ static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         }
         else
         {
-            if ((r=att_getentbyatt (zh, &attp, curAttributeSet, use_value,
+            if ((r = att_getentbyatt (zh, &attp, curAttributeSet, use_value,
                                             use_string)))
             {
-                yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
+                yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d r=%d",
                       curAttributeSet, use_value, r);
                 if (r == -1)
                 {
@@ -1613,8 +1723,9 @@ static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
             char ord_buf[32];
             int i, ord_len;
 
-            ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
-                                          local_attr->local);
+            ord = zebraExplain_lookup_attr_su(zh->reg->zei,
+                                             attp.attset_ordinal,
+                                             local_attr->local);
             if (ord < 0)
                 continue;
             if (prefix_len)
@@ -1639,7 +1750,7 @@ static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         term_dict[prefix_len++] = ')';        
         term_dict[prefix_len++] = 1;
         term_dict[prefix_len++] = reg_type;
-        yaz_log(LOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
+        yaz_log(YLOG_DEBUG, "reg_type = %d", term_dict[prefix_len-1]);
         term_dict[prefix_len] = '\0';
         if (!numeric_relation (zh, zapt, &termp, term_dict,
                                attributeSet, grep_info, &max_pos, reg_type,
@@ -1653,7 +1764,7 @@ static int numeric_term(ZebraHandle zh, Z_AttributesPlusTerm *zapt,
         return -1;
     }
     *term_sub = termp;
-    yaz_log(LOG_DEBUG, "%d positions", grep_info->isam_p_indx);
+    yaz_log(YLOG_DEBUG, "%d positions", grep_info->isam_p_indx);
     return 1;
 }
 
@@ -1669,16 +1780,17 @@ static RSET rpn_search_APT_numeric (ZebraHandle zh,
 {
     char term_dst[IT_MAX_WORD+1];
     const char *termp = termz;
-    RSET rset[60]; /* FIXME - hard-coded magic number */
-    int  r, rset_no = 0;
+    RSET rset[TERM_LIST_LENGTH_MAX];
+    int  r;
+    size_t rset_no = 0;
     struct grep_info grep_info;
 
-    yaz_log(LOG_DEBUG, "APT_numeric t='%s'",termz);
+    yaz_log(log_level_rpn, "APT_numeric t='%s'",termz);
     if (grep_info_prepare (zh, zapt, &grep_info, reg_type, stream))
         return 0;
-    while (1)
+    for (; rset_no < sizeof(rset)/sizeof(*rset); rset_no++)
     { 
-        yaz_log(LOG_DEBUG, "APT_numeric termp=%s", termp);
+        yaz_log(YLOG_DEBUG, "APT_numeric termp=%s", termp);
         grep_info.isam_p_indx = 0;
         r = numeric_term(zh, zapt, &termp, attributeSet, &grep_info,
                           reg_type, complete_flag, num_bases, basenames,
@@ -1686,24 +1798,23 @@ static RSET rpn_search_APT_numeric (ZebraHandle zh,
                           stream);
         if (r < 1)
             break;
-        yaz_log(LOG_DEBUG, "term: %s", term_dst);
+        yaz_log(YLOG_DEBUG, "term: %s", term_dst);
         rset[rset_no] = rset_trunc(zh, grep_info.isam_p_buf,
                                     grep_info.isam_p_indx, term_dst,
                                     strlen(term_dst), rank_type,
                                     0 /* preserve position */,
                                     zapt->term->which, rset_nmem, 
                                     key_it_ctrl,key_it_ctrl->scope);
-        assert (rset[rset_no]);
-        if (++rset_no >= (int) (sizeof(rset)/sizeof(*rset)))
-            break;
+       if (!rset[rset_no])
+           break;
     }
     grep_info_delete (&grep_info);
     if (rset_no == 0)
-        return rsnull_create (rset_nmem,key_it_ctrl);
+        return rsnull_create(rset_nmem,key_it_ctrl);
     if (rset_no == 1)
         return rset[0];
-    return rsmultiand_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
-               rset_no, rset);
+    return rsmulti_and_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
+                             rset_no, rset);
 }
 
 static RSET rpn_search_APT_local (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
@@ -1854,7 +1965,7 @@ static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
     char term_dict[2048];
     char ord_buf[32];
     int prefix_len = 0;
-    int ord = zebraExplain_lookupSU (zh->reg->zei, curAttributeSet, use);
+    int ord = zebraExplain_lookup_attr_su(zh->reg->zei, curAttributeSet, use);
     int ord_len, i, r, max_pos;
     int term_type = Z_Term_characterString;
     const char *flags = "void";
@@ -1884,7 +1995,7 @@ static RSET xpath_trunc(ZebraHandle zh, NMEM stream,
     grep_info.isam_p_indx = 0;
     r = dict_lookup_grep(zh->reg->dict, term_dict, 0,
                           &grep_info, &max_pos, 0, grep_handle);
-    yaz_log (LOG_LOG, "%s %d positions", term,
+    yaz_log (YLOG_DEBUG, "%s %d positions", term,
              grep_info.isam_p_indx);
     rset = rset_trunc(zh, grep_info.isam_p_buf,
                        grep_info.isam_p_indx, term, strlen(term),
@@ -1908,10 +2019,10 @@ static RSET rpn_search_xpath (ZebraHandle zh,
     if (xpath_len < 0)
         return rset;
 
-    yaz_log (LOG_LOG, "len=%d", xpath_len);
+    yaz_log (YLOG_DEBUG, "xpath len=%d", xpath_len);
     for (i = 0; i<xpath_len; i++)
     {
-        yaz_log (LOG_LOG, "XPATH %d %s", i, xpath[i].part);
+        yaz_log (log_level_rpn, "XPATH %d %s", i, xpath[i].part);
 
     }
 
@@ -1925,14 +2036,14 @@ static RSET rpn_search_xpath (ZebraHandle zh,
 
       /      ->    none
 
-   a[@attr=value]/b[@other=othervalue]
+   a[@attr = value]/b[@other = othervalue]
 
  /e/@a val      range(e/,range(@a,freetext(w,1015,val),@a),e/)
  /a/b val       range(b/a/,freetext(w,1016,val),b/a/)
  /a/b/@c val    range(b/a/,range(@c,freetext(w,1016,val),@c),b/a/)
- /a/b[@c=y] val range(b/a/,freetext(w,1016,val),b/a/,@c=y)
- /a[@c=y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c=y)
- /a[@c=x]/b[@c=y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c=y),a/,@c=x)
+ /a/b[@c = y] val range(b/a/,freetext(w,1016,val),b/a/,@c = y)
+ /a[@c = y]/b val range(a/,range(b/a/,freetext(w,1016,val),b/a/),a/,@c = y)
+ /a[@c = x]/b[@c = y] range(a/,range(b/a/,freetext(w,1016,val),b/a/,@c = y),a/,@c = x)
       
     */
 
@@ -2017,7 +2128,7 @@ static RSET rpn_search_xpath (ZebraHandle zh,
                 if (!first_path)
                     continue;
             }
-            yaz_log (LOG_LOG, "xpath_rev (%d) = %s", level, xpath_rev);
+            yaz_log (log_level_rpn, "xpath_rev (%d) = %s", level, xpath_rev);
             if (strlen(xpath_rev))
             {
                 rset_start_tag = xpath_trunc(zh, stream, '0', 
@@ -2026,18 +2137,10 @@ static RSET rpn_search_xpath (ZebraHandle zh,
                 rset_end_tag = xpath_trunc(zh, stream, '0', 
                         xpath_rev, 2, curAttributeSet, rset_nmem);
 
-                /*
-                parms.key_size = sizeof(struct it_key);
-                parms.cmp = key_compare_it;
-                parms.rset_l = rset_start_tag;
-                parms.rset_m = rset;
-                parms.rset_r = rset_end_tag;
-                parms.rset_attr = rset_attr;
-                parms.printer = key_print_it;
-                rset = rset_create (rset_kind_between, &parms);
-                */
-                rset=rsbetween_create( rset_nmem,key_it_ctrl,key_it_ctrl->scope,
-                        rset_start_tag, rset, rset_end_tag, rset_attr);
+                rset = rsbetween_create(rset_nmem, key_it_ctrl,
+                                       key_it_ctrl->scope,
+                                       rset_start_tag, rset,
+                                       rset_end_tag, rset_attr);
             }
             first_path = 0;
         }
@@ -2065,13 +2168,18 @@ static RSET rpn_search_APT (ZebraHandle zh, Z_AttributesPlusTerm *zapt,
     int xpath_use = 0;
     struct xpath_location_step xpath[10];
 
+    if (!log_level_set)
+    {
+        log_level_rpn = yaz_log_module_level("rpn");
+        log_level_set = 1;
+    }
     zebra_maps_attr (zh->reg->zebra_maps, zapt, &reg_id, &search_type,
                      rank_type, &complete_flag, &sort_flag);
     
-    yaz_log(LOG_DEBUG, "reg_id=%c", reg_id);
-    yaz_log(LOG_DEBUG, "complete_flag=%d", complete_flag);
-    yaz_log(LOG_DEBUG, "search_type=%s", search_type);
-    yaz_log(LOG_DEBUG, "rank_type=%s", rank_type);
+    yaz_log(YLOG_DEBUG, "reg_id=%c", reg_id);
+    yaz_log(YLOG_DEBUG, "complete_flag=%d", complete_flag);
+    yaz_log(YLOG_DEBUG, "search_type=%s", search_type);
+    yaz_log(YLOG_DEBUG, "rank_type=%s", rank_type);
 
     if (zapt_term_to_utf8(zh, zapt, termz))
         return 0;
@@ -2162,12 +2270,12 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
         switch (zop->which)
         {
         case Z_Operator_and:
-            r=rsmultiand_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
-                        2, rsets);
+            r = rsmulti_and_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
+                                  2, rsets);
             break;
         case Z_Operator_or:
-            r=rsmultior_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
-                        2, rsets);
+            r = rsmulti_or_create(rset_nmem, key_it_ctrl, key_it_ctrl->scope,
+                                 2, rsets);
             break;
         case Z_Operator_and_not:
             r = rsbool_create_not(rset_nmem,key_it_ctrl, key_it_ctrl->scope,
@@ -2190,7 +2298,7 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
             else
             {
                 /* new / old prox */
-                r=rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
+                r = rsprox_create(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
                          2, rsets, 
                          *zop->u.prox->ordered,
                          (!zop->u.prox->exclusion ? 
@@ -2208,14 +2316,14 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
     {
         if (zs->u.simple->which == Z_Operand_APT)
         {
-            yaz_log(LOG_DEBUG, "rpn_search_APT");
+            yaz_log(YLOG_DEBUG, "rpn_search_APT");
             r = rpn_search_APT (zh, zs->u.simple->u.attributesPlusTerm,
                                 attributeSet, stream, sort_sequence,
                                 num_bases, basenames,rset_nmem);
         }
         else if (zs->u.simple->which == Z_Operand_resultSetId)
         {
-            yaz_log(LOG_DEBUG, "rpn_search_ref");
+            yaz_log(YLOG_DEBUG, "rpn_search_ref");
             r = resultSetRef (zh, zs->u.simple->u.resultSetId);
             if (!r)
             {
@@ -2243,10 +2351,10 @@ static RSET rpn_search_structure (ZebraHandle zh, Z_RPNStructure *zs,
 }
 
 
-RSET rpn_search (ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
-                 Z_RPNQuery *rpn, int num_bases, char **basenames, 
-                 const char *setname,
-                 ZebraSet sset)
+RSET rpn_search(ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
+               Z_RPNQuery *rpn, int num_bases, char **basenames, 
+               const char *setname,
+               ZebraSet sset)
 {
     RSET rset;
     oident *attrset;
@@ -2276,7 +2384,7 @@ RSET rpn_search (ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
         return 0;
 
     if (zh->errCode)
-        yaz_log(LOG_DEBUG, "search error: %d", zh->errCode);
+        yaz_log(YLOG_DEBUG, "search error: %d", zh->errCode);
     
     for (i = 0; sort_sequence->specs[i]; i++)
         ;
@@ -2285,12 +2393,12 @@ RSET rpn_search (ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
         resultSetRank (zh, sset, rset, rset_nmem);
     else
     {
-        yaz_log(LOG_DEBUG, "resultSetSortSingle in rpn_search");
+        yaz_log(YLOG_DEBUG, "resultSetSortSingle in rpn_search");
         resultSetSortSingle (zh, nmem, sset, rset,
                              sort_sequence, &sort_status);
         if (zh->errCode)
         {
-            yaz_log(LOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
+            yaz_log(YLOG_DEBUG, "resultSetSortSingle status = %d", zh->errCode);
         }
     }
     return rset;
@@ -2298,7 +2406,7 @@ RSET rpn_search (ZebraHandle zh, NMEM nmem, NMEM rset_nmem,
 
 struct scan_info_entry {
     char *term;
-    ISAMC_P isam_p;
+    ISAM_P isam_p;
 };
 
 struct scan_info {
@@ -2322,8 +2430,8 @@ static int scan_handle (char *name, const char *info, int pos, void *client)
     scan_info->list[idx].term = (char *)
         odr_malloc(scan_info->odr, strlen(name + len_prefix)+1);
     strcpy(scan_info->list[idx].term, name + len_prefix);
-    assert (*info == sizeof(ISAMC_P));
-    memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAMC_P));
+    assert (*info == sizeof(ISAM_P));
+    memcpy (&scan_info->list[idx].isam_p, info+1, sizeof(ISAM_P));
     return 0;
 }
 
@@ -2366,7 +2474,7 @@ static void count_set (RSET r, int *count)
     struct it_key key;
     RSFD rfd;
 
-    yaz_log(LOG_DEBUG, "count_set");
+    yaz_log(YLOG_DEBUG, "count_set");
 
     *count = 0;
     rfd = rset_open (r, RSETF_READ);
@@ -2380,7 +2488,7 @@ static void count_set (RSET r, int *count)
         kno++;
     }
     rset_close (rfd);
-    yaz_log(LOG_DEBUG, "%d keys, %d records", kno, *count);
+    yaz_log(YLOG_DEBUG, "%d keys, %d records", kno, *count);
 }
 
 void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
@@ -2413,7 +2521,7 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
     char rank_type[128];
     int complete_flag;
     int sort_flag;
-    NMEM rset_nmem=NULL; 
+    NMEM rset_nmem = NULL; 
 
     *list = 0;
 
@@ -2446,7 +2554,7 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
         }
     }
         
-    yaz_log (LOG_DEBUG, "position = %d, num = %d set=%d",
+    yaz_log (YLOG_DEBUG, "position = %d, num = %d set=%d",
              pos, num, attributeset);
         
     attr_init (&use, zapt, 1);
@@ -2459,50 +2567,64 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
         zh->errCode = 113;
         return ;
     }
-    yaz_log (LOG_DEBUG, "use_value = %d", use_value);
+    yaz_log (YLOG_DEBUG, "use_value = %d", use_value);
 
     if (use_value == -1)
         use_value = 1016;
     for (base_no = 0; base_no < num_bases && ord_no < 32; base_no++)
     {
-        int r;
-        attent attp;
-        data1_local_attribute *local_attr;
+       data1_local_attribute *local_attr;
+       attent attp;
+       int ord;
 
-        if ((r=att_getentbyatt (zh, &attp, attributeset, use_value,
-                                use_string)))
-        {
-            yaz_log(LOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
-                  attributeset, use_value);
-            if (r == -1)
-            {
-                char val_str[32];
-                sprintf (val_str, "%d", use_value);
-                errCode = 114;
-                errString = odr_strdup (stream, val_str);
-            }   
-            else
-                errCode = 121;
-            continue;
-        }
-        if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
-        {
-            zh->errString = basenames[base_no];
-            zh->errCode = 109; /* Database unavailable */
-            *num_entries = 0;
-            return;
-        }
-        bases_ok++;
-        for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
-             local_attr = local_attr->next)
-        {
-            int ord;
+       if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
+       {
+           zh->errString = basenames[base_no];
+           zh->errCode = 109; /* Database unavailable */
+           *num_entries = 0;
+           return;
+       }
 
-            ord = zebraExplain_lookupSU (zh->reg->zei, attp.attset_ordinal,
-                                         local_attr->local);
-            if (ord > 0)
-                ords[ord_no++] = ord;
-        }
+       if (use_string &&
+           (ord = zebraExplain_lookup_attr_str(zh->reg->zei,
+                                               use_string)) >= 0)
+       {
+           /* we have a match for a raw string attribute */
+           if (ord > 0)
+               ords[ord_no++] = ord;
+            attp.local_attributes = 0;  /* no more attributes */
+       }
+       else
+       {
+           int r;
+           
+           if ((r = att_getentbyatt (zh, &attp, attributeset, use_value,
+                                     use_string)))
+           {
+               yaz_log(YLOG_DEBUG, "att_getentbyatt fail. set=%d use=%d",
+                       attributeset, use_value);
+               if (r == -1)
+               {
+                   char val_str[32];
+                   sprintf (val_str, "%d", use_value);
+                   errCode = 114;
+                   errString = odr_strdup (stream, val_str);
+               }   
+               else
+                   errCode = 121;
+               continue;
+           }
+       }
+       bases_ok++;
+       for (local_attr = attp.local_attributes; local_attr && ord_no < 32;
+            local_attr = local_attr->next)
+       {
+           ord = zebraExplain_lookup_attr_su(zh->reg->zei,
+                                             attp.attset_ordinal,
+                                             local_attr->local);
+           if (ord > 0)
+               ords[ord_no++] = ord;
+       }
     }
     if (!bases_ok && errCode)
     {
@@ -2512,11 +2634,6 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
     }
     if (ord_no == 0)
     {
-        char val_str[32];
-        sprintf (val_str, "%d", use_value);
-        zh->errCode = 114;
-        zh->errString = odr_strdup (stream, val_str);
-
         *num_entries = 0;
         return;
     }
@@ -2550,7 +2667,7 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
 
         if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id))
             return ;
-                    
+       
         dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
                  scan_info, scan_handle);
     }
@@ -2574,7 +2691,7 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
         for (j = 0; j < ord_no; j++)
         {
             if (ptr[j] < before+after &&
-                (tst=scan_info_array[j].list[ptr[j]].term) &&
+                (tst = scan_info_array[j].list[ptr[j]].term) &&
                 (!mterm || strcmp (tst, mterm) < 0))
             {
                 j0 = j;
@@ -2586,104 +2703,116 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
         scan_term_untrans (zh, stream->mem, reg_id,
                            &glist[i+before].term, mterm);
         rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
-                           glist[i+before].term, strlen(glist[i+before].term),
-                           NULL, 0, zapt->term->which, rset_nmem, 
-                           key_it_ctrl,key_it_ctrl->scope);
+                         glist[i+before].term, strlen(glist[i+before].term),
+                         NULL, 0, zapt->term->which, rset_nmem, 
+                         key_it_ctrl,key_it_ctrl->scope);
         ptr[j0]++;
         for (j = j0+1; j<ord_no; j++)
         {
             if (ptr[j] < before+after &&
-                (tst=scan_info_array[j].list[ptr[j]].term) &&
+                (tst = scan_info_array[j].list[ptr[j]].term) &&
                 !strcmp (tst, mterm))
             {
-                RSET rset2;
-
-                rset2 =
-                   rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
-                               glist[i+before].term,
-                               strlen(glist[i+before].term), NULL, 0,
-                               zapt->term->which,rset_nmem,
-                               key_it_ctrl, key_it_ctrl->scope);
-                rset = rsbool_create_or(rset_nmem,key_it_ctrl,
-                               key_it_ctrl->scope, rset, rset2);
-                /* FIXME - Use a proper multi-or */
-
+                RSET rsets[2];
+               
+               rsets[0] = rset;
+                rsets[1] =
+                   rset_trunc(zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
+                              glist[i+before].term,
+                              strlen(glist[i+before].term), NULL, 0,
+                              zapt->term->which,rset_nmem,
+                              key_it_ctrl, key_it_ctrl->scope);
+                rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
+                                        2, key_it_ctrl->scope, rsets);
                 ptr[j]++;
             }
         }
         if (limit_set)
-            rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
-                            rset, rset_dup(limit_set));
-        count_set (rset, &glist[i+before].occurrences);
-        rset_delete (rset);
+       {
+           RSET rsets[2];
+           rsets[0] = rset;
+           rsets[1] = rset_dup(limit_set);
+           
+           rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
+                                     key_it_ctrl->scope, 2, rsets);
+       }
+       count_set(rset, &glist[i+before].occurrences);
+       rset_delete(rset);
     }
     if (i < after)
     {
-        *num_entries -= (after-i);
-        *is_partial = 1;
+       *num_entries -= (after-i);
+       *is_partial = 1;
     }
-
+    
     /* consider terms before main term */
     for (i = 0; i<ord_no; i++)
-        ptr[i] = 0;
-
+       ptr[i] = 0;
+    
     for (i = 0; i<before; i++)
     {
-        int j, j0 = -1;
-        const char *mterm = NULL;
-        const char *tst;
-        RSET rset;
-        
-        for (j = 0; j <ord_no; j++)
-        {
-            if (ptr[j] < before &&
-                (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
-                (!mterm || strcmp (tst, mterm) > 0))
-            {
-                j0 = j;
-                mterm = tst;
-            }
-        }
-        if (j0 == -1)
-            break;
-
-        scan_term_untrans (zh, stream->mem, reg_id,
-                           &glist[before-1-i].term, mterm);
-
-        rset = rset_trunc
-               (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
-                glist[before-1-i].term, strlen(glist[before-1-i].term),
-                NULL, 0, zapt->term->which,rset_nmem,
-                key_it_ctrl,key_it_ctrl->scope);
-
-        ptr[j0]++;
-
-        for (j = j0+1; j<ord_no; j++)
-        {
-            if (ptr[j] < before &&
-                (tst=scan_info_array[j].list[before-1-ptr[j]].term) &&
-                !strcmp (tst, mterm))
-            {
-                RSET rset2;
-
-                rset2 = rset_trunc(
+       int j, j0 = -1;
+       const char *mterm = NULL;
+       const char *tst;
+       RSET rset;
+       
+       for (j = 0; j <ord_no; j++)
+       {
+           if (ptr[j] < before &&
+               (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
+               (!mterm || strcmp (tst, mterm) > 0))
+           {
+               j0 = j;
+                   mterm = tst;
+           }
+       }
+       if (j0 == -1)
+           break;
+       
+       scan_term_untrans (zh, stream->mem, reg_id,
+                          &glist[before-1-i].term, mterm);
+       
+       rset = rset_trunc
+           (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
+            glist[before-1-i].term, strlen(glist[before-1-i].term),
+            NULL, 0, zapt->term->which,rset_nmem,
+            key_it_ctrl,key_it_ctrl->scope);
+       
+       ptr[j0]++;
+       
+       for (j = j0+1; j<ord_no; j++)
+       {
+           if (ptr[j] < before &&
+               (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
+               !strcmp (tst, mterm))
+           {
+               RSET rsets[2];
+               
+               rsets[0] = rset;
+               rsets[1] = rset_trunc(
                    zh,
                    &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
                    glist[before-1-i].term,
                    strlen(glist[before-1-i].term), NULL, 0,
                    zapt->term->which, rset_nmem,
                    key_it_ctrl, key_it_ctrl->scope);
-                rset = rsbool_create_and(rset_nmem,key_it_ctrl,
-                                        key_it_ctrl->scope, rset, rset2);
-                /* FIXME - multi-and ?? */
-                ptr[j]++;
-            }
-        }
+               rset = rsmulti_or_create(rset_nmem, key_it_ctrl,
+                                        2, key_it_ctrl->scope, rsets);
+               
+               ptr[j]++;
+           }
+       }
         if (limit_set)
-            rset = rsbool_create_and(rset_nmem,key_it_ctrl,key_it_ctrl->scope,
-                            rset, rset_dup(limit_set));
-        count_set (rset, &glist[before-1-i].occurrences);
-        rset_delete (rset);
+       {
+           RSET rsets[2];
+           rsets[0] = rset;
+           rsets[1] = rset_dup(limit_set);
+           
+           rset = rsmulti_and_create(rset_nmem, key_it_ctrl,
+                                     key_it_ctrl->scope, 2, rsets);
+       }
+       count_set (rset, &glist[before-1-i].occurrences);
+       rset_delete (rset);
     }
     i = before-i;
     if (i)
@@ -2692,13 +2821,13 @@ void rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
         *position -= i;
         *num_entries -= i;
     }
-
+    
     nmem_destroy(rset_nmem);
     *list = glist + i;               /* list is set to first 'real' entry */
     
-    yaz_log(LOG_DEBUG, "position = %d, num_entries = %d",
-          *position, *num_entries);
+    yaz_log(YLOG_DEBUG, "position = %d, num_entries = %d",
+           *position, *num_entries);
     if (zh->errCode)
-        yaz_log(LOG_DEBUG, "scan error: %d", zh->errCode);
+        yaz_log(YLOG_DEBUG, "scan error: %d", zh->errCode);
 }
-              
+