My project decided to use Hyper Estraier as Full Text Search Engine. Here i created a simple interface to demonstrate how to program Hyper Estraier with Node API in C.

This program is very simple
1) Ask user to key in insert(1), update(2), delete(3) or list(4) existing indexed
2) Key in URI (Unique ID) and URL for indexed

How to compile — please change according to your library path

gcc -o HyperIndex HyperIndex.c -L/usr/local/lib -lestraier -lresolv -lnsl -lpthread -lqdbm -lz -liconv -lm -lc

Node URL - Here i hard coded my node URL

http://localhost:1978/node/pattern

P.S i am not expert in c, please correct me if i am code something wrong.

Program Explanation
———————————–
Please always refer Hyper Estraier Node API reference here

1) Insert or Update Node by using est_node_put_doc()
If URI is same , it will update/overwrite existing node details

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
static void
putNode(ESTDOC *doc ,ESTNODE *node, char *cBufferUri ,char *cBufferUrl)
{
	/* add attributes to the document object */
	est_doc_add_attr(doc, "@uri", cBufferUri);
	est_doc_add_attr(doc, "@title", cBufferUri);
	est_doc_add_attr(doc, "url", cBufferUrl);//for include function
 
	/* add the body text to the document object */
	est_doc_add_text(doc, cBufferUrl);
 
	/* register the document object to the node */
	if(!est_node_put_doc(node, doc)){
	  fprintf(stderr, "error: %d\n", est_node_status(node));
	}
        printf("\n URL ID : %s Added/Updated \n", cBufferUri);
}

2) Delete Node by using est_node_out_doc_by_uri()

1
2
3
4
5
6
7
8
static void
outNode(ESTDOC *doc ,ESTNODE *node, char *cBufferUri)
{       
        if(!est_node_out_doc_by_uri(node, cBufferUri)){
	  fprintf(stderr, "error: %d\n", est_node_status(node));
	}
        printf("\n URL ID : %s Deleted\n", cBufferUri);
}

3) View Node by using est_node_get_doc_attr_by_uri()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
static void
listNode(ESTDOC *doc ,ESTNODE *node, char *cBufferUri)
{       
	char *pUrl;
 
        if(!(pUrl = est_node_get_doc_attr_by_uri(node, cBufferUri,"url"))){
	  fprintf(stderr, "error: %d\n", est_node_status(node));
	}
 
        printf("\n URL ID : %s\n", cBufferUri);
        printf(" URL Value : %s\n", pUrl);
 
	pUrl=NULL;
}

Here is the full source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * HyperIndex.c
 *
 *      Simple Interface For Hyper Estraier
 *
 *  	gcc -o HyperIndex HyperIndex.c -L/usr/local/lib -lestraier -lresolv -lnsl -lpthread -lqdbm -lz -liconv -lm -lc
 *    
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
#include <estraier.h>
#include <estnode.h>
#include <cabin.h>
#include <string.h>
 
static void
putNode(ESTDOC *doc ,ESTNODE *node, char *cBufferUri ,char *cBufferUrl)
{
	/* add attributes to the document object */
	est_doc_add_attr(doc, "@uri", cBufferUri);
	est_doc_add_attr(doc, "@title", cBufferUri);
	est_doc_add_attr(doc, "url", cBufferUrl);//for include function
 
	/* add the body text to the document object */
	est_doc_add_text(doc, cBufferUrl);
 
	/* register the document object to the node */
	if(!est_node_put_doc(node, doc)){
	  fprintf(stderr, "error: %d\n", est_node_status(node));
	}
 
        printf("\n URL ID : %s Added/Updated \n", cBufferUri);
 
}
 
static void
listNode(ESTDOC *doc ,ESTNODE *node, char *cBufferUri)
{       
	char *pUrl;
 
        if(!(pUrl = est_node_get_doc_attr_by_uri(node, cBufferUri,"url"))){
	  fprintf(stderr, "error: %d\n", est_node_status(node));
	}
 
        printf("\n URL ID : %s\n", cBufferUri);
        printf(" URL Value : %s\n", pUrl);
 
	pUrl=NULL;
}
 
 
static void
outNode(ESTDOC *doc ,ESTNODE *node, char *cBufferUri)
{       
 
        if(!est_node_out_doc_by_uri(node, cBufferUri)){
	  fprintf(stderr, "error: %d\n", est_node_status(node));
	}
 
        printf("\n URL ID : %s Deleted\n", cBufferUri);
 
}
 
int
main(int argc, char **argv)
{
    //calculate program time elapsed
    time_t start, stop;
    clock_t ticks; long count;
 
    //Get user input
    char cBufferFlag[1];
    char cBufferUri[256];
    char cBufferUrl[256];
 
    //start time
    time(&start);
 
    //ESTDOC *est_node_get_doc_by_uri(ESTNODE *node, const char *uri);
 
    ESTNODE *node;
    ESTDOC *doc;
    /* initialize the network environment */
    if(!est_init_net_env()){
      fprintf(stderr, "\nerror: network is unavailable\n");
      return 1;
    }
 
    /* create and configure the node connection object */
    node = est_node_new("http://localhost:1978/node/pattern");
    est_node_set_auth(node, "admin", "admin");
 
    /* create a document object */
    doc = est_doc_new();
 
    printf("\n 1=Insert 2=Update 3=Delete 4=List");
    printf("\n Please Enter Opereation : ");
    scanf("%s",cBufferFlag);
 
 
    if(!strcmp(cBufferFlag, "1") || !strcmp(cBufferFlag, "2"))
    {
    	printf(" Please Enter URL ID : ");
    	scanf("%s",cBufferUri);
 
    	printf(" Please Enter URL : ");
    	scanf("%s",cBufferUrl);
 
        //insert / update
    	putNode(doc,node,cBufferUri,cBufferUrl);
 
    }
    else if(!strcmp(cBufferFlag, "3"))
    {
    	printf(" Please Enter URL ID : ");
    	scanf("%s",cBufferUri);
        //delete url info
    	outNode(doc,node,cBufferUri);
 
    }
    else if(!strcmp(cBufferFlag, "4"))
    {
    	printf(" Please Enter URL ID : ");
    	scanf("%s",cBufferUri);
        //list url info
    	listNode(doc,node,cBufferUri);
 
    }
    else
    {
	exit(1);
    }
 
 
    /* destroy the document object */
    est_doc_delete(doc);
 
 
    /* destroy the node object */
    est_node_delete(node);
    /* free the networking environment */
    est_free_net_env();
 
    //stop time
    time(&stop);
 
    printf("\nFinished in about %.0f seconds. \n\n", difftime(stop, start));
 
    return 0;
}

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • StumbleUpon
  • SphereIt
  • Reddit
  • Google
  • YahooMyWeb
  • Technorati
  • Spurl
  • Sphinn
  • Mixx
  • connotea
  • BlinkList