Added support for temr hit counts. This was not in place for earlier
[idzebra-moved-to-github.git] / include / idzebra / api.h
1 /* $Id: api.h,v 1.24 2005-05-24 11:35:41 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 /** \file api.h
24     \brief Zebra API
25     
26     Return codes:
27     Most functions return ZEBRA_RES, where ZEBRA_FAIL indicates
28     failure; ZEBRA_OK indicates success.
29 */
30
31 #ifndef IDZEBRA_API_H
32 #define IDZEBRA_API_H
33
34 #include <yaz/odr.h>
35 #include <yaz/oid.h>
36 #include <yaz/proto.h>
37 #include <idzebra/res.h>
38 #include <idzebra/version.h>
39
40 YAZ_BEGIN_CDECL
41
42 typedef struct {
43     int processed;
44     int inserted;
45     int updated;
46     int deleted;
47     long utime;
48     long stime;
49 } ZebraTransactionStatus;
50
51 /** Retrieval Record Descriptor */
52 typedef struct {
53     int errCode;         /* non-zero if error when fetching this */
54     char *errString;     /* error string */
55     int position;        /* position of record in result set (1,2,..) */
56     char *buf;           /* record buffer (void pointer really) */
57     int len;             /* length */
58     oid_value format;    /* record syntax */
59     char *base; 
60     SYSNO sysno;
61     int  score;
62 } ZebraRetrievalRecord;
63
64 /** Scan Term Descriptor */
65 typedef struct {
66     int occurrences;     /* scan term occurrences */
67     char *term;          /* scan term string */
68 } ZebraScanEntry;
69
70 /** \var ZebraHandle
71     \brief a Zebra Handle - (session)
72 */
73 typedef struct zebra_session *ZebraHandle;
74
75 /** \var ZebraService
76     \brief a Zebra Service handle
77 */
78 typedef struct zebra_service *ZebraService;
79
80 /** \fn ZebraService zebra_start(const char *configName)
81     \brief starts a Zebra service. 
82     \param configName name of configuration file
83     
84     This function is a simplified version of zebra_start_res.
85 */
86 YAZ_EXPORT
87 ZebraService zebra_start(const char *configName);
88
89 /** \fn ZebraService zebra_start_res(const char *configName, \
90                                      Res def_res, Res over_res)
91     \brief starts a Zebra service with resources.
92     \param configName name of configuration file
93     \param def_res default resources
94     \param over_res overriding resources
95     
96     This function typically called once in a program. A Zebra Service
97     acts as a factory for Zebra session handles.
98 */
99 YAZ_EXPORT
100 ZebraService zebra_start_res(const char *configName,
101                              Res def_res, Res over_res);
102
103 /** \fn ZEBRA_RES zebra_stop(ZebraService zs)
104     \brief stops a Zebra service.
105     \param zs service handle
106     
107     Frees resources used by the service.
108 */
109 YAZ_EXPORT
110 ZEBRA_RES zebra_stop(ZebraService zs);
111
112 /** \fn void zebra_filter_info(ZebraService zs, void *cd, \
113                                void(*cb)(void *cd, const char *name))
114     \brief lists enabled Zebra filters
115     \param zs service handle
116     \param cd callback parameter (opaque)
117     \param cb callback function
118  */
119 YAZ_EXPORT
120 void zebra_filter_info(ZebraService zs, void *cd,
121                        void (*cb)(void *cd, const char *name));
122
123
124 /** \fn ZebraHandle zebra_open(ZebraService zs)
125     \brief creates a Zebra session handle within service.
126     \param zs service handle.
127     
128     There should be one handle for each thread doing something
129     with zebra, be that searching or indexing. In simple apps 
130     one handle is sufficient 
131 */
132 YAZ_EXPORT
133 ZebraHandle zebra_open(ZebraService zs);
134
135 /** \fn int zebra_close(ZebraHandle zh)
136     \brief destroys Zebra session handle.
137     \param zh zebra session handle.
138  */
139 YAZ_EXPORT
140 ZEBRA_RES zebra_close(ZebraHandle zh);
141
142 /** \fn int zebra_errCode(ZebraHandle zh)
143     \brief returns error code for last error
144     \param zh zebra session handle.
145 */
146 YAZ_EXPORT
147 int zebra_errCode(ZebraHandle zh);
148
149 /** \fn const char *zebra_errString(ZebraHandle zh)
150     \brief returns error string for last error
151     \param zh zebra session handle.
152 */
153 YAZ_EXPORT
154 const char *zebra_errString(ZebraHandle zh);
155
156 /** \fn char *zebra_errAdd(ZebraHandle zh)
157     \brief returns additional info for last error
158     \param zh zebra session handle.
159 */
160 YAZ_EXPORT
161 char *zebra_errAdd(ZebraHandle zh);
162
163 /** \fn int zebra_result(ZebraHandle zh, int *code, char **addinfo)
164     \brief returns error code and additional info for last error
165     \param zh zebra session handle.
166     \param code pointer to returned error code
167     \param addinfo pointer to returned additional info
168 */
169 YAZ_EXPORT
170 void zebra_result(ZebraHandle zh, int *code, char **addinfo);
171
172 /** \fn void zebra_clearError(ZebraHandle zh)
173     \brief clears last error.
174     \param zh zebra session handle.
175  */
176 YAZ_EXPORT
177 void zebra_clearError(ZebraHandle zh);
178
179 /** \fn ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query, \
180                 const char *setname, zint *hits)
181     \brief Search using PQF Query 
182     \param zh session handle
183     \param pqf_query query
184     \param setname name of resultset
185     \param hits of hits is returned
186  */
187 YAZ_EXPORT
188 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
189                            const char *setname, zint *hits);
190
191 /** \fn ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query, \
192                 const char *setname, zint *hits)
193     \brief Search using RPN Query 
194     \param zh session handle
195     \param o ODR handle
196     \param query RPN query using YAZ structure
197     \param setname name of resultset
198     \param hits number of hits is returned
199  */
200 YAZ_EXPORT
201 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
202                               const char *setname, zint *hits);
203
204 /** 
205     \fn ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream, \
206                 const char *setname, Z_RecordComposition *comp, \
207                 oid_value input_format, int num_recs, \
208                 ZebraRetrievalRecord *recs)
209     \brief retrieve records from result set (after search)
210     \param zh session handle
211     \param stream allocate records returned using this ODR
212     \param setname name of result set to retrieve records from
213     \param comp Z39.50 record composition
214     \param input_format transfer syntax (OID)
215     \param num_recs number of records to retrieve
216     \param recs store records in this structure (size is num_recs)
217 */
218 YAZ_EXPORT
219 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
220                                  const char *setname,
221                                  Z_RecordComposition *comp,
222                                  oid_value input_format,
223                                  int num_recs,
224                                  ZebraRetrievalRecord *recs);
225 /**
226    \fn int zebra_deleteResultSet(ZebraHandle zh, int function, \
227                 int num_setnames, char **setnames, int *statuses)
228    \brief delete one or more resultsets 
229    \param zh session handle
230    \param function Z_DeleteResultSetRequest_{list,all}
231    \param num_setnames number of result sets
232    \param setnames result set names
233    \param statuses status result
234 */
235 YAZ_EXPORT
236 int zebra_deleteResultSet(ZebraHandle zh, int function,
237                           int num_setnames, char **setnames,
238                           int *statuses);
239
240
241
242 YAZ_EXPORT
243 ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname,
244                                    int *num_terms);
245
246 YAZ_EXPORT
247 ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname,
248                                      int no, zint *count, int *approx,
249                                      char *termbuf, size_t *termlen);
250
251
252 /**
253    \fn ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, \
254                 Z_AttributesPlusTerm *zapt, oid_value attributeset, \
255                 int *position, int *num_entries, ZebraScanEntry **list, \
256                 int *is_partial)
257    \brief performs Scan (Z39.50 style)
258    \param zh session handle
259    \param stream ODR handle for result
260    \param zapt Attribute plus Term (start term)
261    \param attributeset Attributeset for Attribute plus Term
262    \param position input/output position
263    \param num_entries number of terms requested / returned 
264    \param list list of resulting terms (ODR allocated)
265    \param is_partial upon return 1=partial, 0=complete
266 */
267 YAZ_EXPORT ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream,
268                                 Z_AttributesPlusTerm *zapt,
269                                 oid_value attributeset,
270                                 int *position, int *num_entries,
271                                 ZebraScanEntry **list,
272                                 int *is_partial);
273
274 /**
275    \fn ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, \
276                 const char *query, int *position, int *num_entries, \
277                 ZebraScanEntry **list, int *is_partial)
278    \brief performs Scan (taking PQF string)
279    \param zh session handle
280    \param stream ODR handle for result
281    \param query PQF scan query
282    \param position input/output position
283    \param num_entries number of terms requested / returned 
284    \param list list of resulting terms (ODR allocated)
285    \param is_partial upon return 1=partial, 0=complete
286 */
287 YAZ_EXPORT
288 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
289                     int *position, int *num_entries, ZebraScanEntry **entries,
290                     int *is_partial);
291 /**
292    \fn ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
293    \brief authenticate user. Returns 0 if OK, != 0 on failure
294    \param zh session handle
295    \param user user name
296    \param pass password
297  */
298 YAZ_EXPORT
299 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass);
300
301 /**
302    \fn int zebra_string_norm(ZebraHandle zh, unsigned reg_id, \
303                 const char *input_str, int input_len, \
304                 char *output_str, int output_len)
305    \brief normalize zebra term for register (subject to change!)
306    \param zh session handle
307    \param reg_id register ID, 'w', 'p',..
308    \param input_str input string buffer
309    \param input_len input string length
310    \param output_str output string buffer
311    \param output_len output string length
312  */
313 YAZ_EXPORT
314 int zebra_string_norm(ZebraHandle zh, unsigned reg_id, const char *input_str, 
315                 int input_len, char *output_str, int output_len);
316
317 /**
318    \fn ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
319    \brief creates a database
320    \param zh session handle
321    \param db database to be created
322 */
323 YAZ_EXPORT
324 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db);
325
326 /**
327    \fn int zebra_drop_database(ZebraHandle zh, const char *db)
328    \brief deletes a database (drop)
329    \param zh session handle
330    \param db database to be deleted
331 */
332 YAZ_EXPORT
333 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db);
334
335 YAZ_EXPORT
336 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh);
337
338 YAZ_EXPORT
339 ZEBRA_RES zebra_admin_start(ZebraHandle zh);
340
341 YAZ_EXPORT
342 ZEBRA_RES zebra_shutdown(ZebraService zs);
343
344 YAZ_EXPORT
345 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
346                                    const char *record_type);
347
348 YAZ_EXPORT 
349 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh,
350                                      Z_Segment *segment);
351
352 YAZ_EXPORT 
353 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh);
354
355 YAZ_EXPORT 
356 ZEBRA_RES zebra_admin_exchange_record(ZebraHandle zh,
357                                       const char *rec_buf,
358                                       size_t rec_len,
359                                       const char *recid_buf, size_t recid_len,
360                                       int action);
361
362 YAZ_EXPORT 
363 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw);
364
365 YAZ_EXPORT
366 ZEBRA_RES zebra_end_trans(ZebraHandle zh);
367
368 YAZ_EXPORT
369 ZEBRA_RES zebra_end_transaction(ZebraHandle zh,
370                                 ZebraTransactionStatus *stat);
371
372 YAZ_EXPORT
373 ZEBRA_RES zebra_commit(ZebraHandle zh);
374
375 YAZ_EXPORT
376 ZEBRA_RES zebra_clean(ZebraHandle zh);
377
378 YAZ_EXPORT
379 ZEBRA_RES zebra_init(ZebraHandle zh);
380
381 YAZ_EXPORT
382 ZEBRA_RES zebra_compact(ZebraHandle zh);
383
384 YAZ_EXPORT int zebra_repository_update(ZebraHandle zh, const char *path);
385 YAZ_EXPORT int zebra_repository_delete(ZebraHandle zh, const char *path);
386 YAZ_EXPORT int zebra_repository_show(ZebraHandle zh, const char *path);
387
388 YAZ_EXPORT int zebra_add_record(ZebraHandle zh, const char *buf, int buf_size);
389                                
390 YAZ_EXPORT 
391 ZEBRA_RES zebra_insert_record(ZebraHandle zh, 
392                               const char *recordType,
393                               SYSNO *sysno, const char *match,
394                               const char *fname,
395                               const char *buf, int buf_size,
396                               int force_update);
397 YAZ_EXPORT
398 ZEBRA_RES zebra_update_record(ZebraHandle zh, 
399                               const char *recordType,
400                               SYSNO *sysno, const char *match,
401                               const char *fname,
402                               const char *buf, int buf_size,
403                               int force_update);
404 YAZ_EXPORT 
405 ZEBRA_RES zebra_delete_record(ZebraHandle zh, 
406                               const char *recordType,
407                               SYSNO *sysno, const char *match, const char *fname,
408                               const char *buf, int buf_size,
409                               int force_update);
410
411 YAZ_EXPORT 
412 int zebra_resultSetTerms(ZebraHandle zh, const char *setname, 
413                          int no, zint *count, 
414                          int *type, char *out, size_t *len);
415
416 YAZ_EXPORT 
417 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
418                      int num_input_setnames,
419                      const char **input_setnames,
420                      const char *output_setname,
421                      Z_SortKeySpecList *sort_sequence,
422                      int *sort_status);
423
424 YAZ_EXPORT
425 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases, 
426                                  const char **basenames);
427
428 YAZ_EXPORT
429 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename);
430
431 YAZ_EXPORT
432 void zebra_shadow_enable(ZebraHandle zh, int value);
433
434 YAZ_EXPORT
435 int zebra_register_statistics(ZebraHandle zh, int dumpdict);
436
437 YAZ_EXPORT
438 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding);
439
440 YAZ_EXPORT
441 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding);
442
443 /* Resources */
444 YAZ_EXPORT
445 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value);
446 YAZ_EXPORT
447 const char *zebra_get_resource(ZebraHandle zh, 
448                                const char *name, const char *defaultvalue);
449
450
451 YAZ_EXPORT
452 void zebra_pidfname(ZebraService zs, char *path);
453
454 typedef struct {
455     char *term;
456     char *db;
457     zint sysno;
458     int score;
459 } ZebraMetaRecord;
460
461 YAZ_EXPORT
462 ZebraMetaRecord *zebra_meta_records_create(ZebraHandle zh,
463                                            const char *name,
464                                            int num, zint *positions);
465
466
467 YAZ_EXPORT
468 ZebraMetaRecord *zebra_meta_records_create_range(ZebraHandle zh,
469                                                  const char *name, 
470                                                  zint start, int num);
471
472 YAZ_EXPORT
473 void zebra_meta_records_destroy(ZebraHandle zh, ZebraMetaRecord *records,
474                                 int num);
475
476 YAZ_EXPORT 
477 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh);
478
479 YAZ_EXPORT
480 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids);
481
482 YAZ_END_CDECL                                 
483
484 /** \mainpage Zebra
485  *
486  * \section intro_sec Introduction
487  *
488  * Zebra is a search engine for structure data, such as XML, MARC
489  * and others. The following chapters briefly describe each of
490  * Zebra major modules/components.
491  *
492  * \section util Base Utilities
493  *
494  * The Zebra utilities (util.h) defines fundamental types and a few
495  * utilites for Zebra.
496  *
497  * \section res Resources
498  *
499  * The resources system (res.h) is a manager of configuration 
500  * resources. The resources can be viewed as a simple database.
501  * Resources can be read from a configurtion file, they can be
502  * read or written by an application. Resources can also be written,
503  * but that facility is not currently in use.
504  *
505  * \section bfile Bfiles
506  *
507  * The Bfiles (bfile.h) provides a portable interface to the
508  * local file system. It also provides a facility for safe updates
509  * (shadow updates). All file system access is handle by this module
510  * (except for trival reads of configuration files).
511  *
512  * \section dict Dictionary
513  *
514  * The Zebra dictionary (dict.h) maps a search term (key) to a value. The
515  * value is a reference to the list of records identifers in which
516  * the term occurs. Zebra uses an ISAM data structure for the list
517  * of term occurrences. The Dictionary uses \ref bfile.
518  *
519  * \section isam ISAM
520  *
521  * Zebra maintains an ISAM for each term where each ISAM is a list
522  * of record identifiers corresponding to the records in which the
523  * term occur. Unlike traditional ISAM systems, the Zebra ISAM
524  * is compressed. The ISAM system uses \ref bfile.
525  *
526  * Zebra has more than one ISAM system. The old and stable ISAM system
527  * is named isamc (see isamc.h). Another version isams is a write-once
528  * isam system that is quite compact - suitable for CD-ROMs (isams.h). 
529  * The newest ISAM system, isamb, is implemented as a B-Tree (see isamb.h).
530  *
531  * \section data1 Data-1
532  *
533  * The data1 (data1.h) module deals with structured documents. The module can
534  * can read, modify and write documents. The document structure was
535  * originally based on GRS-1 - a Z39.50 v3 structure that predates
536  * DOM. These days the data1 structure may describe XML/SGML as well.
537  * The data1, like DOM, is a tree structure. Each node in the tree
538  * can be of type element, text (cdata), preprocessing instruction,
539  * comment. Element nodes can point to attribute nodes.
540  *
541  * \section recctrl Record Control
542  *
543  * The record control module (recctrl.h) is responsible for
544  * managing the various record types ("classes" or filters).
545  *
546  * \section rset Result-Set
547  *
548  * The Result-Set module (rset.h) defines an interface that all
549  * Zebra Search Results must implement. Each operation (AND, OR, ..)
550  * correspond to an implementation of that interface.
551  *
552  * \section dfa DFA
553  *
554  * DFA (dfa.h) Deterministic Finite Automa is a regular expression engine.
555  * The module compiles a regular expression to a DFA. The DFA can then
556  * be used in various application to perform fast match against the
557  * origianl expression. The \ref Dict uses DFA to perform lookup
558  * using regular expressions.
559  */
560
561 #endif