Honor position attribute, i.e. allow first-in-field search. To
[idzebra-moved-to-github.git] / index / zrpn.c
index a559a3d..4ae0937 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: zrpn.c,v 1.221 2006-06-23 11:21:38 adam Exp $
+/* $Id: zrpn.c,v 1.228 2006-09-08 14:40:53 adam Exp $
    Copyright (C) 1995-2006
    Index Data ApS
 
@@ -15,9 +15,9 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
 #include <stdio.h>
@@ -46,6 +46,8 @@ struct rpn_char_map_info
 static int log_level_set = 0;
 static int log_level_rpn = 0;
 
+#define TERMSET_DISABLE 1
+
 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;
@@ -147,7 +149,6 @@ static void add_isam_p(const char *name, const char *info,
     assert(*info == sizeof(*p->isam_p_buf));
     memcpy(p->isam_p_buf + p->isam_p_indx, info+1, sizeof(*p->isam_p_buf));
 
-#if 1
     if (p->termset)
     {
         const char *db;
@@ -156,7 +157,7 @@ static void add_isam_p(const char *name, const char *info,
         const char *index_name;
         int len = key_SU_decode (&ord, (const unsigned char *) name);
         
-        zebra_term_untrans  (p->zh, p->reg_type, term_tmp, name+len+1);
+        zebra_term_untrans  (p->zh, p->reg_type, term_tmp, name+len);
         yaz_log(log_level_rpn, "grep: %d %c %s", ord, name[len], term_tmp);
         zebraExplain_lookup_ord(p->zh->reg->zei,
                                 ord, 0 /* index_type */, &db, &index_name);
@@ -165,7 +166,6 @@ static void add_isam_p(const char *name, const char *info,
         resultSetAddTerm(p->zh, p->termset, name[len], db,
                         index_name, term_tmp);
     }
-#endif
     (p->isam_p_indx)++;
 }
 
@@ -927,7 +927,7 @@ static ZEBRA_RES term_limits_APT(ZebraHandle zh,
     AttrType hits_limit_attr;
     int term_ref_id_int;
  
-    attr_init_APT(&hits_limit_attr, zapt, 9);
+    attr_init_APT(&hits_limit_attr, zapt, 11);
     *hits_limit_value  = attr_find(&hits_limit_attr, NULL);
 
     attr_init_APT(&term_ref_id_attr, zapt, 10);
@@ -1323,7 +1323,6 @@ static ZEBRA_RES grep_info_prepare(ZebraHandle zh,
     grep_info->zh = zh;
     grep_info->reg_type = reg_type;
     grep_info->termset = 0;
-
     if (!zapt)
         return ZEBRA_OK;
     attr_init_APT(&termset, zapt, 8);
@@ -1331,6 +1330,10 @@ static ZEBRA_RES grep_info_prepare(ZebraHandle zh,
         attr_find_ex(&termset, NULL, &termset_value_string);
     if (termset_value_numeric != -1)
     {
+#if TERMSET_DISABLE
+        zebra_setError(zh, YAZ_BIB1_UNSUPP_SEARCH, "termset");
+        return ZEBRA_FAIL;
+#else
         char resname[32];
         const char *termset_name = 0;
         if (termset_value_numeric != -2)
@@ -1348,6 +1351,7 @@ static ZEBRA_RES grep_info_prepare(ZebraHandle zh,
            zebra_setError(zh, YAZ_BIB1_ILLEGAL_RESULT_SET_NAME, termset_name);
             return ZEBRA_FAIL;
         }
+#endif
     }
     return ZEBRA_OK;
 }
@@ -1355,6 +1359,7 @@ static ZEBRA_RES grep_info_prepare(ZebraHandle zh,
 /**
   \brief Create result set(s) for list of terms
   \param zh Zebra Handle
+  \param zapt Attributes Plust Term (RPN leaf)
   \param termz term as used in query but converted to UTF-8
   \param attributeSet default attribute set
   \param stream memory for result
@@ -1364,9 +1369,9 @@ static ZEBRA_RES grep_info_prepare(ZebraHandle zh,
   \param xpath_use use attribute for X-Path (-1 for no X-path)
   \param num_bases number of databases
   \param basenames array of databases
-  \param rset_mem memory for result sets
+  \param rset_nmem memory for result sets
   \param result_sets output result set for each term in list (output)
-  \param number number of output result sets
+  \param num_result_sets number of output result sets
   \param kc rset key control to be used for created result sets
 */
 static ZEBRA_RES term_list_trunc(ZebraHandle zh,
@@ -1432,6 +1437,93 @@ static ZEBRA_RES term_list_trunc(ZebraHandle zh,
     return ZEBRA_OK;
 }
 
+static ZEBRA_RES rpn_search_APT_position(ZebraHandle zh,
+                                         Z_AttributesPlusTerm *zapt,
+                                         oid_value attributeSet,
+                                         int reg_type,
+                                         int num_bases, char **basenames,
+                                         NMEM rset_nmem,
+                                         RSET *rset,
+                                         struct rset_key_control *kc)
+{
+    RSET *f_set;
+    int base_no;
+    int position_value;
+    int num_sets = 0;
+    AttrType position;
+
+    attr_init_APT(&position, zapt, 3);
+    position_value = attr_find(&position, NULL);
+    switch(position_value)
+    {
+    case 3:
+    case -1:
+        return ZEBRA_OK;
+    case 1:
+    case 2:
+        break;
+    default:
+        zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_POSITION_ATTRIBUTE,
+                            position_value);
+        return ZEBRA_FAIL;
+    }
+
+    if (!zebra_maps_is_first_in_field(zh->reg->zebra_maps, reg_type))
+    {
+        zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_POSITION_ATTRIBUTE,
+                            position_value);
+        return ZEBRA_FAIL;
+    }
+
+    if (!zh->reg->isamb)
+    {
+        zebra_setError_zint(zh, YAZ_BIB1_UNSUPP_POSITION_ATTRIBUTE,
+                            position_value);
+        return ZEBRA_FAIL;
+    }
+    f_set = xmalloc(sizeof(RSET) * num_bases);
+    for (base_no = 0; base_no < num_bases; base_no++)
+    {
+       int ord = -1;
+        char ord_buf[32];
+        char term_dict[100];
+        int ord_len;
+        char *val;
+        ISAM_P isam_p;
+
+        if (zebraExplain_curDatabase (zh->reg->zei, basenames[base_no]))
+        {
+           zebra_setError(zh, YAZ_BIB1_DATABASE_UNAVAILABLE,
+                          basenames[base_no]);
+            return ZEBRA_FAIL;
+        }
+        
+        if (zebra_apt_get_ord(zh, zapt, reg_type, 0,
+                              attributeSet, &ord) != ZEBRA_OK)
+            continue;
+
+        ord_len = key_SU_encode (ord, ord_buf);
+        memcpy(term_dict, ord_buf, ord_len);
+        strcpy(term_dict+ord_len, FIRST_IN_FIELD_STR);
+        val = dict_lookup(zh->reg->dict, term_dict);
+        if (!val)
+            continue;
+        assert(*val == sizeof(ISAM_P));
+        memcpy(&isam_p, val+1, sizeof(isam_p));
+        
+        f_set[num_sets++] = rsisamb_create(rset_nmem, kc, kc->scope,
+                                           zh->reg->isamb, isam_p, 0);
+        
+    }
+    if (num_sets)
+    {
+        *rset = rset_create_or(rset_nmem, kc, kc->scope,
+                               0 /* termid */, num_sets, f_set);
+    }
+    xfree(f_set);
+    return ZEBRA_OK;
+}
+                                         
 static ZEBRA_RES rpn_search_APT_phrase(ZebraHandle zh,
                                       Z_AttributesPlusTerm *zapt,
                                       const char *termz_org,
@@ -1454,8 +1546,30 @@ static ZEBRA_RES rpn_search_APT_phrase(ZebraHandle zh,
                        num_bases, basenames,
                        rset_nmem,
                        &result_sets, &num_result_sets, kc);
+
     if (res != ZEBRA_OK)
        return res;
+
+    if (num_result_sets > 0)
+    {
+        RSET first_set = 0;
+        res = rpn_search_APT_position(zh, zapt, attributeSet, 
+                                      reg_type,
+                                      num_bases, basenames,
+                                      rset_nmem, &first_set,
+                                      kc);
+        if (res != ZEBRA_OK)
+            return res;
+        if (first_set)
+        {
+            RSET *nsets = nmem_malloc(stream,
+                                      sizeof(RSET) * (num_result_sets+1));
+            nsets[0] = first_set;
+            memcpy(nsets+1, result_sets, sizeof(RSET) * num_result_sets);
+            result_sets = nsets;
+            num_result_sets++;
+        }
+    }
     if (num_result_sets == 0)
        *rset = rset_create_null(rset_nmem, kc, 0); 
     else if (num_result_sets == 1)
@@ -2473,6 +2587,10 @@ static int scan_handle (char *name, const char *info, int pos, void *client)
     else
         idx = - pos - 1;
 
+    /* skip special terms.. of no interest */
+    if (name[len_prefix] < 4)
+        return 1;
+
     if (idx < 0)
        return 0;
     scan_info->list[idx].term = (char *)
@@ -2629,7 +2747,7 @@ ZEBRA_RES rpn_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
     if (ord_no == 0)
     {
         *num_entries = 0;
-        return ZEBRA_OK;
+        return ZEBRA_FAIL;
     }
     /* prepare dictionary scanning */
     if (num < 1)