Hyper Estraier Node API Demo

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

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()

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()

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

/*
 * 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 
#include 
#include 

#include 
#include 
#include 
#include 

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;
}

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
0 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors
newest oldest most voted