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, #find_indexes, #index?, #indexes, #local_name, #lock, #locked?, #reference?, #rename, #scalar?, #select, #table, #truncate, #unlock, #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

#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



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'ext/groonga/rb-grn-index-column.c', line 262

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



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
# File 'ext/groonga/rb-grn-index-column.c', line 376

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



1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
# File 'ext/groonga/rb-grn-index-column.c', line 1182

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.



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/groonga/rb-grn-index-column.c', line 180

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



920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
# File 'ext/groonga/rb-grn-index-column.c', line 920

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.



955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
# File 'ext/groonga/rb-grn-index-column.c', line 955

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          *range_object;
    grn_table_cursor *table_cursor;
    grn_id            rid_min = GRN_ID_NIL;
    grn_id            rid_max = GRN_ID_MAX;
    int               flags   = 0;
    grn_obj          *index_cursor;
    VALUE             rb_table_cursor;
    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, NULL,
                                    NULL, NULL, NULL,
                                    NULL, &range_object,
                                    NULL, NULL);

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

    table_cursor = RVAL2GRNTABLECURSOR(rb_table_cursor, NULL);
    rb_table     = GRNOBJECT2RVAL(Qnil, context, range_object, GRN_FALSE);
    rb_lexicon   = rb_iv_get(rb_table_cursor, "@table");

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

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

    if (rb_block_given_p())
        return rb_ensure(rb_yield, rb_cursor, rb_grn_object_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



1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
# File 'ext/groonga/rb-grn-index-column.c', line 1311

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:



749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
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
# File 'ext/groonga/rb-grn-index-column.c', line 749

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



895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
# File 'ext/groonga/rb-grn-index-column.c', line 895

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:



709
710
711
712
713
714
715
716
# File 'ext/groonga/rb-grn-index-column.c', line 709

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:



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
575
576
577
578
579
580
581
582
583
584
# File 'ext/groonga/rb-grn-index-column.c', line 543

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:

  • columns (::Array<Groonga::Column>)

    インデックス対象となるカラムの配列



661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
# File 'ext/groonga/rb-grn-index-column.c', line 661

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



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'ext/groonga/rb-grn-index-column.c', line 483

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)


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

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)


828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# File 'ext/groonga/rb-grn-index-column.c', line 828

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)


849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
# File 'ext/groonga/rb-grn-index-column.c', line 849

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