我的项目决定使用Hyper Estraier作为全文搜索引擎。 在这里,我创建了一个简单的界面来演示如何使用C语言中的Node API编程Hyper Estraier。
这个程序很简单
1)要求用户键入insert(1),update(2),delete(3)或list(4)现有索引
2)键入URI(唯一ID)和用于索引的URL
如何编译 -请根据您的库路径进行更改
gcc -o HyperIndex HyperIndex.c -L/usr/local/lib -lestraier -lresolv -lnsl -lpthread -lqdbm -lz -liconv -lm -lc
节点URL –在这里,我对节点URL进行了硬编码
http://localhost:1978/node/pattern
附言:我不是C语言方面的专家,如果我编写的代码有误,请更正我。
程序说明
————————————
请始终在此处参考Hyper Estraier Node API参考
1)使用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; }
翻译自: https://mkyong.com/hyper-estraier/hyper-estraier-node-api-demo/