Class: Groonga::IndexColumn

Inherits:
Column show all
Defined in:
ext/groonga/rb-grn-index-column.c,
lib/groonga/index-column.rb,
ext/groonga/rb-grn-index-column.c

Overview

転置索引エントリを格納するカラム。このカラムを利用するこ とにより高速な全文検索を実現できる。

テーブルにGroonga::IndexColumnを定義する方法は Table#define_index_column を参照。

Instance Method Summary collapse

Methods inherited from Column

#clear_lock, #data?, #find_indexes, #index?, #indexes, #local_name, #lock, #locked?, #reference?, #rename, #scalar?, #select, #table, #truncate, #unlock, #vector?, #weight_vector?

Methods included from Flushable

#flush

Methods inherited from Object

#==, #[]=, #accessor?, #append, #builtin?, #close, #closed?, #column?, #dirty?, #domain, #function_procedure?, #id, #index_column?, #key_accessor?, #last_modified, #name, #path, #persistent?, #prepend, #procedure?, #range, #reference_column?, #remove, #scorer_procedure?, #selector_only_procedure?, #selector_procedure?, #table?, #temporary?, #touch, #unlink, #window_function_procedure?

Instance Method Details

#[]Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'ext/groonga/rb-grn-index-column.c', line 201

static VALUE
rb_grn_index_column_array_reference (VALUE self, VALUE rb_token)
{
    grn_ctx *context = NULL;
    grn_obj *column;
    grn_obj *domain;
    grn_id token_id;
    grn_obj *value;
    VALUE rb_value;

    rb_grn_index_column_deconstruct(SELF(self),
                                    &column,
                                    &context,
                                    NULL,
                                    &domain,
                                    &value,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL,
                                    NULL);

    token_id = RVAL2GRNID(rb_token, context, domain, self);
    GRN_BULK_REWIND(value);
    grn_obj_get_value(context, column, token_id, value);
    rb_grn_context_check(context, self);
    rb_value = GRNVALUE2RVAL(context, value, NULL, self);

    return rb_value;
}

#add(record, value, options = {}) ⇒ void

Adds a record that has @value@ content to inverted index for fast fulltext serach. Normally, this method is not used explicitly. Inverted index for fulltext search is updated automatically by using @:source@ option of Table#define_index_column.

Examples:

Adds sentences of an article to index

articles = Groonga::Array.create(:name => "Articles")
articles.define_column("title", "ShortText")
articles.define_column("content", "Text")

terms = Groonga::Hash.create(:name => "Terms",
                             :key_type => "ShortText",
                             :default_tokenizer => "TokenBigram")
content_index = terms.define_index_column("content", articles,
                                          :with_position => true,
                                          :with_section => true)

content = <<-CONTENT
Groonga is a fast and accurate full text search engine based on
inverted index. One of the characteristics of groonga is that a
newly registered document instantly appears in search
results. Also, groonga allows updates without read locks. These
characteristics result in superior performance on real-time
applications.

Groonga is also a column-oriented database management system
(DBMS). Compared with well-known row-oriented systems, such as
MySQL and PostgreSQL, column-oriented systems are more suited for
aggregate queries. Due to this advantage, groonga can cover
weakness of row-oriented systems.

The basic functions of groonga are provided in a C library. Also,
libraries for using groonga in other languages, such as Ruby, are
provided by related projects. In addition, groonga-based storage
engines are provided for MySQL and PostgreSQL. These libraries
and storage engines allow any application to use groonga. See
usage examples.
CONTENT

groonga = articles.add(:title => "groonga", :content => content)

content.split(/\n{2,}/).each_with_index do |sentence, i|
  content_index.add(groonga, sentence, :section => i + 1)
end

content_index.search("engine").each do |record|
  p record.key["title"] # -> "groonga"
end

This method returns an undefined value.

Parameters:

  • record (Groonga::Record, Integer)

    The record that has a @value@ as its value. It can be Integer as record id.

  • value (String)

    The value of the @record@.

  • options (::Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :section (Integer) — default: 1

    The section number. It is one-origin.

    You must specify @=> true@ in Table#define_index_column to use this option.

Since:

  • 3.0.2



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'ext/groonga/rb-grn-index-column.c', line 301

static VALUE
rb_grn_index_column_add (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *column, *range;
    grn_rc rc;
    grn_id id;
    unsigned int section;
    grn_obj *new_value;
    VALUE rb_record, rb_value, rb_options, rb_section;

    rb_scan_args(argc, argv, "21", &rb_record, &rb_value, &rb_options);

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    &new_value, NULL, NULL,
                                    NULL, &range,
                                    NULL, NULL);

    id = RVAL2GRNID(rb_record, context, range, self);

    GRN_BULK_REWIND(new_value);
    RVAL2GRNBULK(rb_value, context, new_value);

    rb_grn_scan_options(rb_options,
                        "section", &rb_section,
                        NULL);

    if (NIL_P(rb_section)) {
        section = 1;
    } else {
        section = NUM2UINT(rb_section);
    }

    rc = grn_column_index_update(context, column, id, section, NULL, new_value);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return self;
}

#delete(record, value, options = {}) ⇒ void

Deletes a record that has @value@ content from inverted index. Normally, this method is not used explicitly. Inverted index for fulltext search is updated automatically by using @:source@ option of Table#define_index_column.

Examples:

Deletes sentences of an article to index

articles = Groonga::Array.create(:name => "Articles")
articles.define_column("title", "ShortText")
articles.define_column("content", "Text")

terms = Groonga::Hash.create(:name => "Terms",
                             :key_type => "ShortText",
                             :default_tokenizer => "TokenBigram")
content_index = terms.define_index_column("content", articles,
                                          :with_position => true,
                                          :with_section => true)

content = <<-CONTENT
Groonga is a fast and accurate full text search engine based on
inverted index. One of the characteristics of groonga is that a
newly registered document instantly appears in search
results. Also, groonga allows updates without read locks. These
characteristics result in superior performance on real-time
applications.

Groonga is also a column-oriented database management system
(DBMS). Compared with well-known row-oriented systems, such as
MySQL and PostgreSQL, column-oriented systems are more suited for
aggregate queries. Due to this advantage, groonga can cover
weakness of row-oriented systems.

The basic functions of groonga are provided in a C library. Also,
libraries for using groonga in other languages, such as Ruby, are
provided by related projects. In addition, groonga-based storage
engines are provided for MySQL and PostgreSQL. These libraries
and storage engines allow any application to use groonga. See
usage examples.
CONTENT

groonga = articles.add(:title => "groonga", :content => content)

content.split(/\n{2,}/).each_with_index do |sentence, i|
  content_index.add(groonga, sentence, :section => i + 1)
end

content_index.search("engine").each do |record|
  p record.key["title"] # -> "groonga"
end

content.split(/\n{2,}/).each_with_index do |sentence, i|
  content_index.delete(groonga, sentence, :section => i + 1)
end

p content_index.search("engine").size # -> 0

This method returns an undefined value.

Parameters:

  • record (Groonga::Record, Integer)

    The record that has a @value@ as its value. It can be Integer as record id.

  • value (String)

    The value of the @record@.

  • options (::Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :section (Integer) — default: 1

    The section number. It is one-origin.

    You must specify @=> true@ in Table#define_index_column to use this option.

Since:

  • 3.0.2



415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'ext/groonga/rb-grn-index-column.c', line 415

static VALUE
rb_grn_index_column_delete (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *column, *range;
    grn_rc rc;
    grn_id id;
    unsigned int section;
    grn_obj *old_value;
    VALUE rb_record, rb_value, rb_options, rb_section;

    rb_scan_args(argc, argv, "21", &rb_record, &rb_value, &rb_options);

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, &old_value, NULL,
                                    NULL, &range,
                                    NULL, NULL);

    id = RVAL2GRNID(rb_record, context, range, self);

    GRN_BULK_REWIND(old_value);
    RVAL2GRNBULK(rb_value, context, old_value);

    rb_grn_scan_options(rb_options,
                        "section", &rb_section,
                        NULL);

    if (NIL_P(rb_section)) {
        section = 1;
    } else {
        section = NUM2UINT(rb_section);
    }

    rc = grn_column_index_update(context, column, id, section, old_value, NULL);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return self;
}

#disk_usageObject



28
29
30
31
32
33
34
35
36
# File 'lib/groonga/index-column.rb', line 28

def disk_usage
  return 0 if path.nil?

  usage = super
  chunk_path = "#{path}.c"
  measurer = StatisticMeasurer.new
  usage += measurer.measure_disk_usage(chunk_path)
  usage
end

#dump(output_directory) ⇒ Object



23
24
25
26
# File 'lib/groonga/index-column.rb', line 23

def dump(output_directory)
  dumper = IndexColumnDumper.new(self, output_directory)
  dumper.dump
end

#estimate_size(token_id) ⇒ Integer #estimate_size(query, options = {}) ⇒ Integer #estimate_size(lexicon_cursor) ⇒ Integer

Estimates the number of documents found by the given token ID, query or lexicon cursor.

Examples:

Token ID style

# Define schema
Groonga::Schema.define do |schema|
  schema.create_table("Articles") do |table|
    table.text("content")
  end

  schema.create_table("Terms",
                      :type => :hash,
                      :key_type => "ShortText",
                      :default_tokenizer => "TokenBigram",
                      :normalizer => "NormalizerAuto") do |table|
    table.index("Articles.content",
                :name => "articles_content",
                :with_position => true,
                :with_section => true)
  end
end
articles = Groonga["Articles"]
terms = Groonga["Terms"]
index = Groonga["Terms.articles_content"]

# Add data
articles.add(:content => "Groonga is fast")
articles.add(:content => "Rroonga is fast")
articles.add(:content => "Mroonga is fast")

# Estimate the number of documents found by token ID
p @index.estimate_size(@terms["fast"].id)    # => 7
p @index.estimate_size(@terms["Groonga"].id) # => 1

Token record style

# Define schema
Groonga::Schema.define do |schema|
  schema.create_table("Articles") do |table|
    table.text("content")
  end

  schema.create_table("Terms",
                      :type => :hash,
                      :key_type => "ShortText",
                      :default_tokenizer => "TokenBigram",
                      :normalizer => "NormalizerAuto") do |table|
    table.index("Articles.content",
                :name => "articles_content",
                :with_position => true,
                :with_section => true)
  end
end
articles = Groonga["Articles"]
terms = Groonga["Terms"]
index = Groonga["Terms.articles_content"]

# Add data
articles.add(:content => "Groonga is fast")
articles.add(:content => "Rroonga is fast")
articles.add(:content => "Mroonga is fast")

# Estimate the number of documents found by token record
p @index.estimate_size(@terms["fast"])    # => 7
p @index.estimate_size(@terms["Groonga"]) # => 1

Query style

# Define schema
Groonga::Schema.define do |schema|
  schema.create_table("Articles") do |table|
    table.text("content")
  end

  schema.create_table("Terms",
                      :type => :hash,
                      :key_type => "ShortText",
                      :default_tokenizer => "TokenBigramSplitSymbolAlpha",
                      :normalizer => "NormalizerAuto") do |table|
    table.index("Articles.content",
                :name => "articles_content",
                :with_position => true,
                :with_section => true)
  end
end
articles = Groonga["Articles"]
index = Groonga["Terms.articles_content"]

# Add data
articles.add(:content => "Groonga is fast")
articles.add(:content => "Rroonga is fast")
articles.add(:content => "Mroonga is fast")

# Estimate the number of documents found by query
p @index.estimate_size("roonga") # => 6

Lexicon cursor style

# Define schema
Groonga::Schema.define do |schema|
  schema.create_table("Memos",
                      :type => :hash,
                      :key_type => "ShortText") do |table|
    table.short_text("tags", :type => :vector)
  end

  schema.create_table("Tags",
                      :type => :patricia_trie,
                      :key_type => "ShortText") do |table|
    table.index("Memos.tags",
                :name => "memos_tags")
  end
end
memos = Groonga["Memos"]
tags = Groonga["Tags"]
index = Groonga["Tags.memos_tags"]

# Add data
memos.add(:tags => ["Groonga"])
memos.add(:tags => ["Rroonga", "Ruby"])
memos.add(:tags => ["grndump", "Rroonga"])

# Estimate the number of documents found by lexicon cursor
# Iterates tags that start with "R".
tags.open_prefix_cursor("R") do |cursor|
  # The cursor iterates "Rroonga" and "Ruby".
  p index.estimate_size(cursor) # => 6
end

Overloads:

  • #estimate_size(token_id) ⇒ Integer

    Returns The estimated number of documents found by the given token ID.

    Parameters:

    • token_id (Integer, Record)

      The token ID to be estimated.

    Returns:

    • (Integer)

      The estimated number of documents found by the given token ID.

  • #estimate_size(query, options = {}) ⇒ Integer

    Returns The estimated number of documents found by the given query.

    Parameters:

    • query (String)

      The query to be estimated.

    • options (::Hash) (defaults to: {})

      The options.

    Options Hash (options):

    • :mode (Groonga::Operator, String, Symbol) — default: Groonga::Operator::EXACT

      The operation mode for search. It must be one of the followings:

      • Groonga::Operator::EXACT, "exact", :exact
      • Groonga::Operator::NEAR, "near", :near
      • Groonga::Operator::NEAR2, "near2", :near2
      • Groonga::Operator::SIMILAR, "similar", :similar
      • Groonga::Operator::REGEXP, "regexp", :regexp

    Returns:

    • (Integer)

      The estimated number of documents found by the given query.

    Since:

    • 5.0.1

  • #estimate_size(lexicon_cursor) ⇒ Integer

    Returns The estimated number of documents found by term IDS in the given lexicon cursor.

    Parameters:

    Returns:

    • (Integer)

      The estimated number of documents found by term IDS in the given lexicon cursor.

    Since:

    • 5.0.1

Since:

  • 4.0.7



1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
# File 'ext/groonga/rb-grn-index-column.c', line 1256

static VALUE
rb_grn_index_column_estimate_size (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *column;
    grn_obj *domain_object;
    unsigned int size;
    VALUE rb_target;
    VALUE rb_options;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, &domain_object,
                                    NULL, NULL, NULL,
                                    NULL, NULL,
                                    NULL, NULL);

    rb_scan_args(argc, argv, "11", &rb_target, &rb_options);

    if (TYPE(rb_target) == T_STRING) {
        const char *query;
        unsigned int query_length;
        grn_search_optarg options;
        VALUE rb_mode;

        query = StringValueCStr(rb_target);
        query_length = RSTRING_LEN(rb_target);

        rb_grn_scan_options(rb_options,
                            "mode", &rb_mode,
                            NULL);

        memset(&options, 0, sizeof(grn_search_optarg));
        if (NIL_P(rb_mode)) {
            options.mode = GRN_OP_EXACT;
        } else {
            options.mode = RVAL2GRNOPERATOR(rb_mode);
        }
        switch (options.mode) {
        case GRN_OP_EXACT:
        case GRN_OP_NEAR:
        case GRN_OP_NEAR2:
        case GRN_OP_SIMILAR:
        case GRN_OP_REGEXP:
            /* valid */
            break;
        default:
            rb_raise(rb_eArgError,
                     ":mode must be one of "
                     "nil, :exact, :near, :near2, :similar or :regexp: <%s>",
                     rb_grn_inspect(rb_mode));
            break;
        }

        size = grn_ii_estimate_size_for_query(context, (grn_ii *)column,
                                              query, query_length, &options);
    } else if (RVAL2CBOOL(rb_obj_is_kind_of(rb_target, rb_cGrnTableCursor))) {
        grn_table_cursor *lexicon_cursor;

        lexicon_cursor = RVAL2GRNTABLECURSOR(rb_target, &context);
        size = grn_ii_estimate_size_for_lexicon_cursor(context,
                                                       (grn_ii *)column,
                                                       lexicon_cursor);
    } else {
        grn_id token_id;
        token_id = RVAL2GRNID(rb_target, context, domain_object, self);
        size = grn_ii_estimate_size(context, (grn_ii *)column, token_id);
    }

    return UINT2NUM(size);
}

#inspectString

Inspects the index column.

Returns the inspected string.

Returns:

  • (String)

    the inspected string.



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/groonga/rb-grn-index-column.c', line 187

static VALUE
rb_grn_index_column_inspect (VALUE self)
{
    VALUE inspected;

    inspected = rb_str_new_cstr("");
    rb_grn_object_inspect_header(self, inspected);
    rb_grn_object_inspect_content(self, inspected);
    rb_grn_index_column_inspect_content(self, inspected);
    rb_grn_object_inspect_footer(self, inspected);

    return inspected;
}

#medium?Boolean

Checks whether the index column is medium or not.

Returns true if the index column is medium, false otherwise.

Returns:

  • (Boolean)

    true if the index column is medium, false otherwise.

Returns:

  • (Boolean)

Since:

  • 6.1.0



959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
# File 'ext/groonga/rb-grn-index-column.c', line 959

static VALUE
rb_grn_index_column_medium_p (VALUE self)
{
    grn_obj *column;
    grn_ctx *context;
    grn_column_flags flags;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL, NULL, NULL,
                                    NULL, NULL);

    flags = grn_column_get_flags(context, column);
    return CBOOL2RVAL(flags & GRN_OBJ_INDEX_MEDIUM);
}

#open_cursor(table_cursor, options = {}) ⇒ Object

Opens cursor to iterate posting in the index column.

Examples:

# TODO

Parameters:

  • The (TableCursor)

    table cursor for table of the index column.

  • options (::Hash) (defaults to: {})

Options Hash (options):

  • :with_section (Boolean) — default: nil

    Includes section info the posting. It is enabled by default if the index column is created with @:with_section@ flag.

  • :with_weight (Boolean) — default: nil

    Includes weight info the posting. It is enabled by default if the index column is created with @:with_weight@ flag.

  • :with_position (Boolean) — default: nil

    Includes position info the posting. It is enabled by default if the index column is created with @:with_position@ flag.



1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
# File 'ext/groonga/rb-grn-index-column.c', line 1000

static VALUE
rb_grn_index_column_open_cursor (int argc, VALUE *argv, VALUE self)
{
    grn_ctx          *context;
    grn_obj          *column;
    grn_column_flags  column_flags;
    grn_obj          *domain_object;
    grn_obj          *range_object;
    grn_table_cursor *table_cursor = NULL;
    grn_id            token_id = GRN_ID_NIL;
    grn_id            rid_min = GRN_ID_NIL;
    grn_id            rid_max = GRN_ID_MAX;
    int               flags   = 0;
    VALUE             rb_table_cursor_or_token;
    VALUE             options;
    VALUE             rb_with_section, rb_with_weight, rb_with_position;
    VALUE             rb_table;
    VALUE             rb_lexicon;
    VALUE             rb_cursor;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, &domain_object,
                                    NULL, NULL, NULL,
                                    NULL, &range_object,
                                    NULL, NULL);

    rb_scan_args(argc, argv, "11", &rb_table_cursor_or_token, &options);
    rb_grn_scan_options(options,
                        "with_section", &rb_with_section,
                        "with_weight", &rb_with_weight,
                        "with_position", &rb_with_position,
                        NULL);

    rb_table     = GRNOBJECT2RVAL(Qnil, context, range_object, GRN_FALSE);
    rb_lexicon   = rb_funcall(self, rb_intern("table"), 0);
    if (CBOOL2RVAL(rb_obj_is_kind_of(rb_table_cursor_or_token,
                                     rb_cGrnTableCursor))) {
        VALUE rb_table_cursor = rb_table_cursor_or_token;
        table_cursor = RVAL2GRNTABLECURSOR(rb_table_cursor, NULL);
    } else {
        VALUE rb_token = rb_table_cursor_or_token;
        token_id = RVAL2GRNID(rb_token, context, domain_object, self);
    }

    column_flags = grn_column_get_flags(context, column);

    if (NIL_P(rb_with_section)) {
        flags |= column_flags & GRN_OBJ_WITH_SECTION;
    } else if (RVAL2CBOOL(rb_with_section)) {
        flags |= GRN_OBJ_WITH_SECTION;
    }

    if (NIL_P(rb_with_weight)) {
        flags |= column_flags & GRN_OBJ_WITH_WEIGHT;
    } else if (RVAL2CBOOL(rb_with_weight)) {
        flags |= GRN_OBJ_WITH_WEIGHT;
    }

    if (NIL_P(rb_with_position)) {
        flags |= column_flags & GRN_OBJ_WITH_POSITION;
    } else if (RVAL2CBOOL(rb_with_position)) {
        flags |= GRN_OBJ_WITH_POSITION;
    }

    if (table_cursor) {
        grn_obj *index_cursor;
        index_cursor = grn_index_cursor_open(context, table_cursor,
                                             column, rid_min, rid_max, flags);
        rb_cursor = GRNINDEXCURSOR2RVAL(context,
                                        index_cursor,
                                        rb_table,
                                        rb_lexicon);
    } else {
        grn_ii *ii = (grn_ii *)column;
        grn_ii_cursor *ii_cursor;
        ii_cursor = grn_ii_cursor_open(context,
                                       ii,
                                       token_id,
                                       rid_min,
                                       rid_max,
                                       grn_ii_get_n_elements(context, ii),
                                       flags);
        rb_cursor = rb_grn_inverted_index_cursor_to_ruby_object(context,
                                                                ii_cursor,
                                                                token_id,
                                                                flags,
                                                                rb_table,
                                                                rb_lexicon);
    }

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_cursor, call_close, rb_cursor);
    else
        return rb_cursor;
}

#reindexvoid

Recreates the index column.

This method is useful when you have a broken index column.

You can use Database#reindex to recreate all index columns in a database.

You can use TableKeySupport#reindex to recreate all index columns in a table.

You can use FixSizeColumn#reindex or VariableSizeColumn#reindex to recreate all index columns. They use index columns of the data column as reindex target index columns.

Examples:

How to recreate the index column.

Groonga::Schema.define do |schema|
  schema.create_table("Memos") do |table|
    table.short_text("title")
    table.text("content")
  end

  schema.create_table("BigramTerms",
                      :type => :patricia_trie,
                      :key_type => :short_text,
                      :normalizer => "NormalizerAuto",
                      :default_tokenizer => "TokenBigram") do |table|
    table.index("Memos.title")
    table.index("Memos.content")
  end

  schema.create_table("MeCabTerms",
                      :type => :patricia_trie,
                      :key_type => :short_text,
                      :normalizer => "NormalizerAuto",
                      :default_tokenizer => "TokenMecab") do |table|
    table.index("Memos.title")
    table.index("Memos.content")
  end
end

Groonga["MeCabTerms.Memos_content"].reindex
# They aren't called:
#   Groonga["BigramTerms.Memos_title"].reindex
#   Groonga["BigramTerms.Memos_content"].reindex
#   Groonga["MeCabTerms.Memos_title"].reindex

This method returns an undefined value.

See Also:

Since:

  • 5.1.1



1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
# File 'ext/groonga/rb-grn-index-column.c', line 1385

static VALUE
rb_grn_index_column_reindex (VALUE self)
{
    grn_rc rc;
    grn_ctx *context;
    grn_obj *column;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL,
                                    NULL, NULL,
                                    NULL, NULL);

    rc = grn_obj_reindex(context, column);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return Qnil;
}

#search(query, options = {}) ⇒ Groonga::Hash

object から query に対応するオブジェクトを検索し、見つかっ たオブジェクトのIDがキーになっている Hash を返す。

Parameters:

  • options (::Hash) (defaults to: {})

    The name and value pairs. Omitted names are initialized as the default value

Options Hash (options):

  • :result (Groonga::Hash)

    結果を格納するGroonga::Hash。指定しない場合は新しく Groonga::Hashを生成し、それに結果を格納して返す。

  • :operator (Object)

    以下のどれかの値を指定する。 +nil+ , @”or”@ , @”||”@ , @”and”@ , @”+”@ , @”&&”@ , @”but”@ , @”not”@ , @”-“@ , @”adjust”@ , @”>”@ 。 それぞれ以下のようになる。(FIXME: 「以下」)

  • :exact (Object)

    +true+ を指定すると完全一致で検索する

  • :longest_common_prefix (Object)

    +true+ を指定すると query と同じ接頭辞をもつエントリのう ち、もっとも長いエントリを検索する

  • :suffix (Object)

    +true+ を指定すると query が後方一致するエントリを検索す る

  • :prefix (Object)

    +true+ を指定すると query が前方一致するレコードを検索す る

  • :near (Object)

    +true+ を指定すると query に指定した複数の語が近傍に含ま れるレコードを検索する

Returns:



788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
# File 'ext/groonga/rb-grn-index-column.c', line 788

static VALUE
rb_grn_index_column_search (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *column;
    grn_obj *range;
    grn_obj *query = NULL, *id_query = NULL, *string_query = NULL;
    grn_obj *result;
    grn_operator operator;
    grn_search_optarg options;
    grn_rc rc;
    VALUE rb_query, rb_options, rb_result, rb_operator, rb_mode, rb_weight;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL, NULL, &range,
                                    &id_query, &string_query);

    rb_scan_args(argc, argv, "11", &rb_query, &rb_options);

    if (CBOOL2RVAL(rb_obj_is_kind_of(rb_query, rb_cInteger))) {
        grn_id id;
        id = NUM2UINT(rb_query);
        GRN_TEXT_SET(context, id_query, &id, sizeof(grn_id));
        query = id_query;
    } else {
        const char *_query;
        _query = StringValuePtr(rb_query);
        GRN_TEXT_SET(context, string_query, _query, RSTRING_LEN(rb_query));
        query = string_query;
    }

    rb_grn_scan_options(rb_options,
                        "result", &rb_result,
                        "operator", &rb_operator,
                        "mode", &rb_mode,
                        "weight", &rb_weight,
                        NULL);

    if (NIL_P(rb_result)) {
        result = grn_table_create(context, NULL, 0, NULL,
                                  GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
                                  range, 0);
        rb_grn_context_check(context, self);
        rb_result = GRNOBJECT2RVAL(Qnil, context, result, GRN_TRUE);
    } else {
        result = RVAL2GRNOBJECT(rb_result, &context);
    }

    operator = RVAL2GRNOPERATOR(rb_operator);

    memset(&options, 0, sizeof(grn_search_optarg));
    if (NIL_P(rb_mode)) {
        options.mode = GRN_OP_EXACT;
    } else {
        options.mode = RVAL2GRNOPERATOR(rb_mode);
    }
    options.similarity_threshold = 0;
    options.max_interval = 0;
    options.weight_vector = NULL;
    if (NIL_P(rb_weight)) {
        options.vector_size = 1;
    } else {
        options.vector_size = NUM2UINT(rb_weight);
    }
    options.proc = NULL;
    options.max_size = 0;

    rc = grn_obj_search(context, column, query, result, operator, &options);
    rb_grn_rc_check(rc, self);

    return rb_result;
}

#small?Boolean

Checks whether the index column is small or not.

Returns true if the index column is small, false otherwise.

Returns:

  • (Boolean)

    true if the index column is small, false otherwise.

Returns:

  • (Boolean)

Since:

  • 6.1.0



934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
# File 'ext/groonga/rb-grn-index-column.c', line 934

static VALUE
rb_grn_index_column_small_p (VALUE self)
{
    grn_obj *column;
    grn_ctx *context;
    grn_column_flags flags;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL, NULL, NULL,
                                    NULL, NULL);

    flags = grn_column_get_flags(context, column);
    return CBOOL2RVAL(flags & GRN_OBJ_INDEX_SMALL);
}

#source=(column) ⇒ Object

インデックス対象となるカラムを設定する。

Parameters:



748
749
750
751
752
753
754
755
# File 'ext/groonga/rb-grn-index-column.c', line 748

static VALUE
rb_grn_index_column_set_source (VALUE self, VALUE rb_source)
{
    if (!RVAL2CBOOL(rb_obj_is_kind_of(rb_source, rb_cArray)))
        rb_source = rb_ary_new_from_args(1, rb_source);

    return rb_grn_index_column_set_sources(self, rb_source);
}

#sources::Array<Groonga::Column>

インデックス対象となっている Column の配列を返す。

Returns:



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'ext/groonga/rb-grn-index-column.c', line 582

static VALUE
rb_grn_index_column_get_sources (VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *column;
    grn_obj sources;
    grn_id *source_ids;
    VALUE rb_sources;
    int i, n;
    VALUE exception;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL, NULL, NULL,
                                    NULL, NULL);

    GRN_OBJ_INIT(&sources, GRN_BULK, 0, GRN_ID_NIL);
    grn_obj_get_info(context, column, GRN_INFO_SOURCE, &sources);
    rb_grn_context_check(context, self);

    n = GRN_BULK_VSIZE(&sources) / sizeof(grn_id);
    source_ids = (grn_id *)GRN_BULK_HEAD(&sources);
    rb_sources = rb_ary_new2(n);
    for (i = 0; i < n; i++) {
        grn_obj *source;
        VALUE rb_source;

        source = grn_ctx_at(context, *source_ids);
        exception = rb_grn_context_to_exception(context, self);
        if (!NIL_P(exception)) {
            grn_obj_unlink(context, &sources);
            rb_exc_raise(exception);
        }

        rb_source = GRNOBJECT2RVAL(Qnil, context, source, GRN_FALSE);
        rb_ary_push(rb_sources, rb_source);
        source_ids++;
    }
    grn_obj_unlink(context, &sources);

    return rb_sources;
}

#sources=(columns) ⇒ Object

インデックス対象となる複数のカラムを配列で設定する。

Parameters:



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
# File 'ext/groonga/rb-grn-index-column.c', line 700

static VALUE
rb_grn_index_column_set_sources (VALUE self, VALUE rb_sources)
{
    VALUE exception;
    grn_ctx *context = NULL;
    grn_obj *column;
    int i, n;
    VALUE *rb_source_values;
    grn_id range_id;
    grn_id *sources;
    grn_rc rc;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL,
                                    &range_id, NULL,
                                    NULL, NULL);

    n = RARRAY_LEN(rb_sources);
    rb_source_values = RARRAY_PTR(rb_sources);
    sources = ALLOCA_N(grn_id, n);
    for (i = 0; i < n; i++) {
        sources[i] = resolve_source_id(context, column, range_id,
                                       rb_source_values[i]);
    }

    {
        grn_obj bulk_sources;
        GRN_OBJ_INIT(&bulk_sources, GRN_BULK, 0, GRN_ID_NIL);
        GRN_TEXT_SET(context, &bulk_sources, sources, n * sizeof(grn_id));
        rc = grn_obj_set_info(context, column, GRN_INFO_SOURCE, &bulk_sources);
        exception = rb_grn_context_to_exception(context, self);
        grn_obj_unlink(context, &bulk_sources);
    }

    if (!NIL_P(exception))
        rb_exc_raise(exception);
    rb_grn_rc_check(rc, self);

    return Qnil;
}

#update(record, old_value, new_value, options = {}) ⇒ void

Updates a record that has @new_value@ as new content and @old_value@ as old content in inverted index. Normally, this method is not used explicitly. Inverted index for fulltext search is updated automatically by using @:source@ option of Table#define_index_column.

Examples:

Updates sentences of an article in index

articles = Groonga::Array.create(:name => "Articles")
articles.define_column("title", "ShortText")
articles.define_column("content", "Text")

terms = Groonga::Hash.create(:name => "Terms",
                             :key_type => "ShortText",
                             :default_tokenizer => "TokenBigram")
content_index = terms.define_index_column("content", articles,
                                          :with_position => true,
                                          :with_section => true)

old_sentence = <<-SENTENCE
Groonga is a fast and accurate full text search engine based on
inverted index. One of the characteristics of groonga is that a
newly registered document instantly appears in search
results. Also, groonga allows updates without read locks. These
characteristics result in superior performance on real-time
applications.
SENTENCE

new_sentence = <<-SENTENCE
Groonga is also a column-oriented database management system
(DBMS). Compared with well-known row-oriented systems, such as
MySQL and PostgreSQL, column-oriented systems are more suited for
aggregate queries. Due to this advantage, groonga can cover
weakness of row-oriented systems.
SENTENCE

groonga = articles.add(:title => "groonga", :content => old_sentence)

content_index.add(groonga, old_sentence, :section => 1)
p content_index.search("engine").size # -> 1
p content_index.search("MySQL").size  # -> 0

groonga[:content] = new_sentence
content_index.update(groonga, old_sentence, new_sentence, :section => 1)
p content_index.search("engine").size # -> 0
p content_index.search("MySQL").size  # -> 1

This method returns an undefined value.

Parameters:

  • record (Groonga::Record, Integer)

    The record that has a @new_value@ as its new value and @old_value@ as its old value. It can be Integer as record id.

  • old_value (String)

    The old value of the @record@.

  • new_value (String)

    The new value of the @record@.

  • options (::Hash) (defaults to: {})

    The options.

Options Hash (options):

  • :section (Integer) — default: 1

    The section number. It is one-origin.

    You must specify @=> true@ in Table#define_index_column to use this option.

Since:

  • 3.0.2



522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
# File 'ext/groonga/rb-grn-index-column.c', line 522

static VALUE
rb_grn_index_column_update (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context = NULL;
    grn_obj *column, *range;
    grn_rc rc;
    grn_id id;
    unsigned int section;
    grn_obj *old_value, *new_value;
    VALUE rb_record, rb_old_value, rb_new_value, rb_options, rb_section;

    rb_scan_args(argc, argv, "31",
                 &rb_record, &rb_old_value, &rb_new_value, &rb_options);

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    &new_value, &old_value, NULL,
                                    NULL, &range,
                                    NULL, NULL);

    id = RVAL2GRNID(rb_record, context, range, self);

    if (NIL_P(rb_old_value)) {
        old_value = NULL;
    } else {
        GRN_BULK_REWIND(old_value);
        RVAL2GRNBULK(rb_old_value, context, old_value);
    }

    if (NIL_P(rb_new_value)) {
        new_value = NULL;
    } else {
        GRN_BULK_REWIND(new_value);
        RVAL2GRNBULK(rb_new_value, context, new_value);
    }

    rb_grn_scan_options(rb_options,
                        "section", &rb_section,
                        NULL);

    if (NIL_P(rb_section)) {
        section = 1;
    } else {
        section = NUM2UINT(rb_section);
    }

    rc = grn_column_index_update(context, column, id, section,
                                 old_value, new_value);
    rb_grn_context_check(context, self);
    rb_grn_rc_check(rc, self);

    return self;
}

#with_position?Boolean

column が位置情報も格納する場合は +true+ を返します。

Returns:

  • (Boolean)


909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'ext/groonga/rb-grn-index-column.c', line 909

static VALUE
rb_grn_index_column_with_position_p (VALUE self)
{
    grn_obj *column;
    grn_ctx *context;
    grn_column_flags flags;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL, NULL, NULL,
                                    NULL, NULL);

    flags = grn_column_get_flags(context, column);
    return CBOOL2RVAL(flags & GRN_OBJ_WITH_POSITION);
}

#with_section?Boolean

column が段落情報も格納する場合は +true+ を返します。

Returns:

  • (Boolean)


867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
# File 'ext/groonga/rb-grn-index-column.c', line 867

static VALUE
rb_grn_index_column_with_section_p (VALUE self)
{
    grn_obj *column;
    grn_ctx *context;
    grn_column_flags flags;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL, NULL, NULL,
                                    NULL, NULL);

    flags = grn_column_get_flags(context, column);
    return CBOOL2RVAL(flags & GRN_OBJ_WITH_SECTION);
}

#with_weight?Boolean

column がウェイト情報も格納する場合は +true+ を返します。

Returns:

  • (Boolean)


888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
# File 'ext/groonga/rb-grn-index-column.c', line 888

static VALUE
rb_grn_index_column_with_weight_p (VALUE self)
{
    grn_obj *column;
    grn_ctx *context;
    grn_column_flags flags;

    rb_grn_index_column_deconstruct(SELF(self), &column, &context,
                                    NULL, NULL,
                                    NULL, NULL, NULL, NULL, NULL,
                                    NULL, NULL);

    flags = grn_column_get_flags(context, column);
    return CBOOL2RVAL(flags & GRN_OBJ_WITH_WEIGHT);
}