Démonstration de l’API Hyper Node Estraier

Démo de l'API Hyper Estraier Node

Mon projet a décidé d'utiliserHyper Estraier comme moteur de recherche en texte intégral. Ici, j'ai créé une interface simple pour montrer comment programmer Hyper Estraier avec l'API Node en C.

This program is very simple
1) Demander à l'utilisateur de saisir insérer (1), mettre à jour (2), supprimer (3) ou répertorier (4)
indexés existants 2) Saisir l'URI (unique ID) et URL pour l'index

How to compile - veuillez changer en fonction du chemin de votre bibliothèque

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

Node URL - Ici, j'ai codé en dur l'URL de mon nœud

http://localhost:1978/node/pattern

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

image

Program Explanation
————————————–
Veuillez toujours vous référer àHyper Estraier Node API reference here

1) Insert or Update Node by using est_node_put_doc()
Si l'URI est le même, il mettra à jour / écrasera les détails du nœud existant

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) Supprimer le nœud en utilisant 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) Afficher le nœud en utilisant 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;
}

Voici le code source complet

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