#include #include #include static grn_obj *bookmarks, *uri_column, *comment_column; static grn_obj *lexicon, *comment_index_column; static grn_obj * get (grn_ctx *context, const char *name) { return grn_ctx_get(context, name, strlen(name)); } static grn_obj * create_column (grn_ctx *context, grn_obj *table, const char *name, grn_obj *value_type, grn_obj_flags flags) { return grn_column_create(context, table, name, strlen(name), NULL, flags, value_type); } static void define_bookmarks_table (grn_ctx *context) { bookmarks = grn_table_create(context, "Bookmarks", strlen("Bookmarks"), NULL, GRN_OBJ_TABLE_NO_KEY, NULL, 0); uri_column = create_column(context, bookmarks, "uri", get(context, "ShortText"), 0); comment_column = create_column(context, bookmarks, "comment", get(context, "ShortText"), 0); } static void define_lexicon_table (grn_ctx *context) { lexicon = grn_table_create(context, "Lexicon", strlen("Lexicon"), NULL, GRN_OBJ_TABLE_PAT_KEY, get(context, "ShortText"), 0); grn_obj_set_info(context, lexicon, GRN_INFO_DEFAULT_TOKENIZER, get(context, "TokenBigram")); comment_index_column = create_column(context, lexicon, "comment-index", bookmarks, GRN_OBJ_COLUMN_INDEX | GRN_OBJ_WITH_POSITION); } static void assign_source (grn_ctx *context) { grn_obj source; grn_id source_id; source_id = grn_obj_id(context, comment_column); GRN_OBJ_INIT(&source, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, GRN_ID_NIL); GRN_TEXT_SET(context, &source, &source_id, sizeof(grn_id)); grn_obj_set_info(context, comment_index_column, GRN_INFO_SOURCE, &source); } static void add_bookmark (grn_ctx *context, const char *uri, const char *comment) { grn_id id; grn_obj value; id = grn_table_add(context, bookmarks, NULL, 0, NULL); GRN_OBJ_INIT(&value, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, GRN_ID_NIL); GRN_TEXT_SET(context, &value, uri, strlen(uri)); grn_obj_set_value(context, uri_column, id, &value, GRN_OBJ_SET); GRN_OBJ_INIT(&value, GRN_BULK, GRN_OBJ_DO_SHALLOW_COPY, GRN_ID_NIL); GRN_TEXT_SET(context, &value, comment, strlen(comment)); grn_obj_set_value(context, comment_column, id, &value, GRN_OBJ_SET); } static void print_result (grn_ctx *context, grn_obj *result) { grn_table_cursor *cursor; grn_id result_id; grn_obj *uri_accessor, *comment_accessor; uri_accessor = grn_obj_column(context, result, ".comment-index.uri", strlen(".comment-index.uri")); comment_accessor = grn_obj_column(context, result, ".comment-index.comment", strlen(".comment-index.comment")); printf("uri\t\t\t | comment\n"); cursor = grn_table_cursor_open(context, result, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_ASCENDING); while ((result_id = grn_table_cursor_next(context, cursor)) != GRN_ID_NIL) { grn_obj *uri, *comment; uri = grn_obj_get_value(context, uri_accessor, result_id, NULL); comment = grn_obj_get_value(context, comment_accessor, result_id, NULL); GRN_TEXT_PUTC(context, uri, '\0'); GRN_TEXT_PUTC(context, comment, '\0'); printf("%s\t | %s\n", GRN_BULK_HEAD(uri), GRN_BULK_HEAD(comment)); grn_obj_close(context, uri); grn_obj_close(context, comment); } grn_table_cursor_close(context, cursor); } static void search (grn_ctx *context, const char *word) { grn_obj *result; grn_obj *query; result = grn_table_create(context, NULL, 0, NULL, GRN_OBJ_TABLE_HASH_KEY, lexicon, 0); query = grn_obj_open(context, GRN_BULK, 0, 0); grn_bulk_write(context, query, word, strlen(word)); grn_obj_search(context, comment_index_column, query, result, GRN_OP_OR, NULL); grn_obj_close(context, query); printf("search result: <%s>: %d\n", word, grn_table_size(context, result)); print_result(context, result); grn_obj_close(context, result); printf("\n"); } int main (int argc, char **argv) { grn_ctx context; grn_init(); grn_ctx_init(&context, 0); grn_db_create(&context, NULL, NULL); define_bookmarks_table(&context); define_lexicon_table(&context); assign_source(&context); add_bookmark(&context, "http://groonga.org/", "an open-source fulltext search engine and column store"); add_bookmark(&context, "http://qwik.jp/senna/", "an embeddable fulltext search engine"); add_bookmark(&context, "http://cutter.sourceforge.net/", "a unit testing framework for C"); search(&context, "search"); search(&context, "testing"); grn_ctx_fin(&context); grn_fin(); return 0; }