Hyper Estraier Node APIデモ

Hyper Estraier Node APIデモ

私のプロジェクトでは、Hyper Estraierを全文検索エンジンとして使用することにしました。 ここでは、CでNode APIを使用してHyper Estraierをプログラムする方法を示すために、簡単なインターフェイスを作成しました。

This program is very simple
1)ユーザーにinsert(1)、update(2)、delete(3)、またはlist(4)の既存のインデックス付き
を入力するように依頼します2)URIを入力します(一意ID)とインデックス付きのURL

How to compile —ライブラリパスに応じて変更してください

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

Node URL –ここでノードURLをハードコーディングしました

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
———————————–
常にHyper Estraier Node API reference hereを参照してください

1) Insert or Update Node by using est_node_put_doc()
URIが同じ場合、既存のノードの詳細を更新/上書きします

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

完全なソースコードはこちら

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