egg 介面執行個體

來源:互聯網
上載者:User

執行個體

編譯:gcc -g -Wall -O0 test.c -o test `pkg-config glib-2.0 --cflags --libs` -lscholar -legg2

#include <egg2/Egg2.h>
#include <scholar/ImLexAnalyzer.h>
#include <scholar/ImTokenList.h>
#include <scholar/ImCnLexAnalyzer.h>
#include <scholar/ImCwsLexAnalyzer.h>
int main()
{
HEGGDIRECTORY hDirectory = eggDirectory_open("/ape/ImRoBot5/index/bas/k2d/");
HEGGINDEXREADER hIndexReader = eggIndexReader_open(hDirectory);
HEGGINDEXSEARCHER hIndexSearcher = eggIndexSearcher_new(hIndexReader);
HEGGQUERY h1, h2, h3;
h1 = eggQuery_new_string("title", "new", 3, ANALYZER_CWSLEX);
h2 = eggQuery_new_string("content", "new", 3, ANALYZER_CWSLEX);
// h2 = eggQuery_new_string("body", "some description", 16, p_la);
// h3 = eggQuery_new_int32("price", 199);
// h2 = eggQuery_and(h3, h2);
h1 = eggQuery_or(h2, h1);
// h3 = h2 = 0;
HEGGTOPCOLLECTOR hTopCollector = eggTopCollector_new(0);
int ret = eggIndexSearcher_search_with_query(hIndexSearcher, hTopCollector, h1);
if (ret == EGG_TRUE)
{
eggTopCollector_normalized(hTopCollector, EGG_TOPSORT_SCORE);
// eggTopCollector_normalized(hTopCollector, EGG_TOPSORT_NOT);
HEGGSCOREDOC lp_score_doc = eggTopCollector_top_docs(hTopCollector);
count_t cnt = eggTopCollector_total_hits(hTopCollector);
printf("have hit %u documents\n", cnt);

if (cnt > 0)
{
printf("last document: id[%llu]\n", lp_score_doc[cnt-1].idDoc);
HEGGDOCUMENT lp_eggDocument = EGG_NULL;
eggIndexReader_get_document(hIndexReader,
lp_score_doc[cnt-1].idDoc, &lp_eggDocument);
HEGGFIELD lp_field = eggDocument_get_field(lp_eggDocument, "content");
unsigned len = 0;
char *val = eggField_get_value(lp_field, &len);
printf("last document: body[%.*s]\n", len, val);
lp_field = 0;
eggDocument_delete(lp_eggDocument);
}
}

eggTopCollector_delete(hTopCollector);
eggQuery_delete(h1);
eggIndexSearcher_delete(hIndexSearcher);
eggIndexReader_close(hIndexReader);
eggDirectory_close(hDirectory);
}

新版egg

 #include <egg2/Egg2.h>
#include <scholar/ImLexAnalyzer.h>
#include <scholar/ImTokenList.h>
int main()
{
HEGGHANDLE hEggHandle = eggCluster_open("192.168.1.135:4000:basnews");
HEGGINDEXREADER hIndexReader = eggIndexReader_open(hEggHandle);

HEGGINDEXSEARCHER hIndexSearcher = eggIndexSearcher_new(hIndexReader);
HEGGQUERY h1, h2, h3;
h1 = eggQuery_new_string("content", "日本的攝影記者", strlen("日本的攝影記者"), ANALYZER_CWSLEX);
h2 = eggQuery_new_int32("age", 20);
h3 = eggQuery_new_string("name", "小明", strlen("小明"), EGG_NULL);
h2 = eggQuery_and(h3, h2);
h1 = eggQuery_or(h2, h1);
h3 = h2 = 0;

//填0取所有結果,非0按填的值取個數
HEGGTOPCOLLECTOR hTopCollector = eggTopCollector_new(0);

//對最後結果進行排序
//EGG_TOPSORT_WEIGHT: 按document的weight排序
//EGG_TOPSORT_SCORE: 按查詢關鍵字的相關度排序(打分排序)
//EGG_TOPSORT_NOT: 不排序
//不調該函數預設是EGG_TOPSORT_NOT
eggTopCollector_set_sortType(hTopCollector, EGG_TOPSORT_SCORE);
int ret = eggIndexSearcher_search_with_query(hIndexSearcher, hTopCollector, h1);
if (ret == EGG_TRUE)
{


HEGGSCOREDOC lp_score_doc = eggTopCollector_top_docs(hTopCollector);
count_t cnt = eggTopCollector_total_hits(hTopCollector);
printf("have hit %u documents\n", cnt);

if (cnt > 0)
{
HEGGDOCUMENT lp_eggDocument = EGG_NULL;

eggIndexReader_get_document(hIndexReader,
lp_score_doc[cnt-1].idDoc, &lp_eggDocument);

HEGGFIELD lp_field = eggDocument_get_field(lp_eggDocument, "content");
unsigned len = 0;
char *val = eggField_get_value(lp_field, &len);
lp_field = 0;
eggDocument_delete(lp_eggDocument);
}
}
eggTopCollector_delete(hTopCollector);
eggQuery_delete(h1);
eggIndexSearcher_delete(hIndexSearcher);
eggIndexReader_close(hIndexReader);
eggCluster_close(hEggHandle);

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.