Navigation Menu

Skip to content

Commit

Permalink
Add mroonga_boolean_mode_syntax_flags system and session variable
Browse files Browse the repository at this point in the history
You can customize syntax `MATCH () AGAINST ("..." IN BOOLEAN MODE)`.

Available flags:

  * DEFAULT: Equals to SYNTAX_QUERY,ALLOW_LEADING_NOT.
  * SYNTAX_QUERY: Uses query grnexpr syntax. If neither SYNTAX_QUERY nor
    SYNTAX_SCRIPT aren't specified, SYNTAX_QUERY is used.
  * SYNTAX_SCRIPT: Uses script grnexpr syntax. If both SYNTAX_QUERY and
    SYNTAX_SCRIPT are specified, SYNTAX_SCRIPT is used.
  * ALLOW_COLUMN: Allows "COLUMN:..." syntax in query grnexpr syntax.
  * ALLOW_UPDATE: Allows updating value byb "COLUMN:=NEW_VALUE" syntax
    in query grnexpr syntax.
  * ALLOW_LEADING_NOT: Allows "-NOT_INCLUDED_KEYWORD ..." syntax
    in query grnexpr syntax.

TODO:

  * Document me.
  • Loading branch information
kou committed Dec 23, 2014
1 parent ef4e83b commit dd097fe
Show file tree
Hide file tree
Showing 12 changed files with 357 additions and 3 deletions.
68 changes: 65 additions & 3 deletions ha_mroonga.cpp
Expand Up @@ -524,6 +524,29 @@ static char *mrn_version = const_cast<char *>(MRN_VERSION);
static char *mrn_vector_column_delimiter = NULL;
static my_bool mrn_libgroonga_support_zlib = FALSE;
static my_bool mrn_libgroonga_support_lz4 = FALSE;
typedef enum {
MRN_BOOLEAN_MODE_SYNTAX_FLAG_DEFAULT = (1 << 0),
MRN_BOOLEAN_MODE_SYNTAX_FLAG_SYNTAX_QUERY = (1 << 1),
MRN_BOOLEAN_MODE_SYNTAX_FLAG_SYNTAX_SCRIPT = (1 << 2),
MRN_BOOLEAN_MODE_SYNTAX_FLAG_ALLOW_COLUMN = (1 << 3),
MRN_BOOLEAN_MODE_SYNTAX_FLAG_ALLOW_UPDATE = (1 << 4),
MRN_BOOLEAN_MODE_SYNTAX_FLAG_ALLOW_LEADING_NOT = (1 << 5)
} mrn_boolean_mode_syntax_flag;
static const char *mrn_boolean_mode_sytnax_flag_names[] = {
"DEFAULT",
"SYNTAX_QUERY",
"SYNTAX_SCRIPT",
"ALLOW_COLUMN",
"ALLOW_UPDATE",
"ALLOW_LEADING_NOT",
NullS
};
static TYPELIB mrn_boolean_mode_syntax_flags_typelib = {
array_elements(mrn_boolean_mode_sytnax_flag_names) - 1,
"",
mrn_boolean_mode_sytnax_flag_names,
NULL
};

typedef enum {
MRN_ACTION_ON_ERROR_ERROR,
Expand Down Expand Up @@ -931,6 +954,18 @@ static MYSQL_SYSVAR_BOOL(libgroonga_support_lz4, mrn_libgroonga_support_lz4,
NULL,
grn_check_lz4_support());

static MYSQL_THDVAR_SET(boolean_mode_syntax_flags,
PLUGIN_VAR_RQCMDARG,
"The flags to custom syntax in BOOLEAN MODE. "
"Available flags: "
"DEFAULT(=SYNTAX_QUERY,ALLOW_LEADING_NOT), "
"SYNTAX_QUERY, SYNTAX_SCRIPT, "
"ALLOW_COLUMN, ALLOW_UPDATE and ALLOW_LEADING_NOT",
NULL,
NULL,
MRN_BOOLEAN_MODE_SYNTAX_FLAG_DEFAULT,
&mrn_boolean_mode_syntax_flags_typelib);

static struct st_mysql_sys_var *mrn_system_variables[] =
{
MYSQL_SYSVAR(log_level),
Expand All @@ -948,6 +983,7 @@ static struct st_mysql_sys_var *mrn_system_variables[] =
MYSQL_SYSVAR(vector_column_delimiter),
MYSQL_SYSVAR(libgroonga_support_zlib),
MYSQL_SYSVAR(libgroonga_support_lz4),
MYSQL_SYSVAR(boolean_mode_syntax_flags),
NULL
};

Expand Down Expand Up @@ -7732,6 +7768,34 @@ bool ha_mroonga::generic_ft_init_ext_parse_pragma_w(struct st_mrn_ft_info *info,
DBUG_RETURN(n_weights > 0);
}

grn_expr_flags ha_mroonga::expr_flags_in_boolean_mode()
{
MRN_DBUG_ENTER_METHOD();

ulonglong syntax_flags = THDVAR(ha_thd(), boolean_mode_syntax_flags);
grn_expr_flags expression_flags = 0;
if (syntax_flags == MRN_BOOLEAN_MODE_SYNTAX_FLAG_DEFAULT) {
expression_flags = GRN_EXPR_SYNTAX_QUERY | GRN_EXPR_ALLOW_LEADING_NOT;
} else {
if (syntax_flags & MRN_BOOLEAN_MODE_SYNTAX_FLAG_SYNTAX_SCRIPT) {
expression_flags |= GRN_EXPR_SYNTAX_SCRIPT;
} else {
expression_flags |= GRN_EXPR_SYNTAX_QUERY;
}
if (syntax_flags & MRN_BOOLEAN_MODE_SYNTAX_FLAG_ALLOW_COLUMN) {
expression_flags |= GRN_EXPR_ALLOW_COLUMN;
}
if (syntax_flags & MRN_BOOLEAN_MODE_SYNTAX_FLAG_ALLOW_UPDATE) {
expression_flags |= GRN_EXPR_ALLOW_UPDATE;
}
if (syntax_flags & MRN_BOOLEAN_MODE_SYNTAX_FLAG_ALLOW_LEADING_NOT) {
expression_flags |= GRN_EXPR_ALLOW_LEADING_NOT;
}
}

DBUG_RETURN(expression_flags);
}

grn_rc ha_mroonga::generic_ft_init_ext_prepare_expression_in_boolean_mode(
struct st_mrn_ft_info *info,
String *key,
Expand Down Expand Up @@ -7812,12 +7876,10 @@ grn_rc ha_mroonga::generic_ft_init_ext_prepare_expression_in_boolean_mode(
if (!weight_specified) {
grn_expr_append_obj(info->ctx, match_columns, index_column, GRN_OP_PUSH, 1);
}
grn_expr_flags expression_flags =
GRN_EXPR_SYNTAX_QUERY | GRN_EXPR_ALLOW_LEADING_NOT;
rc = grn_expr_parse(info->ctx, expression,
keyword, keyword_length,
match_columns, GRN_OP_MATCH, default_operator,
expression_flags);
expr_flags_in_boolean_mode());
if (rc) {
char error_message[MRN_MESSAGE_BUFFER_SIZE];
snprintf(error_message, MRN_MESSAGE_BUFFER_SIZE,
Expand Down
1 change: 1 addition & 0 deletions ha_mroonga.hpp
Expand Up @@ -904,6 +904,7 @@ class ha_mroonga: public handler
grn_obj *match_columns,
uint *consumed_keyword_length,
grn_obj *tmp_objects);
grn_expr_flags expr_flags_in_boolean_mode();
grn_rc generic_ft_init_ext_prepare_expression_in_boolean_mode(
struct st_mrn_ft_info *info,
String *key,
Expand Down
@@ -0,0 +1,18 @@
SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;
SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY,ALLOW_COLUMN";
SET NAMES UTF8;
CREATE TABLE diaries (
title TEXT,
content TEXT,
FULLTEXT KEY (title),
FULLTEXT KEY (content)
) DEFAULT CHARSET=utf8;
INSERT INTO diaries VALUES("Groonga", "Hello Groonga");
SELECT * FROM diaries
WHERE MATCH(title) AGAINST("content:@Hello" IN BOOLEAN MODE);
title content
Groonga Hello Groonga
DROP TABLE diaries;
SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;
@@ -0,0 +1,16 @@
SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;
SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY,ALLOW_LEADING_NOT";
SET NAMES UTF8;
CREATE TABLE diaries (
title TEXT,
FULLTEXT KEY (title)
) DEFAULT CHARSET=utf8;
INSERT INTO diaries VALUES("Groonga");
INSERT INTO diaries VALUES("Mroonga");
SELECT * FROM diaries WHERE MATCH(title) AGAINST("-Groonga" IN BOOLEAN MODE);
title
Mroonga
DROP TABLE diaries;
SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;
@@ -0,0 +1,18 @@
SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;
SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY,ALLOW_COLUMN,ALLOW_UPDATE";
SET NAMES UTF8;
CREATE TABLE diaries (
title TEXT,
content TEXT,
FULLTEXT KEY (title),
FULLTEXT KEY (content)
) DEFAULT CHARSET=utf8;
INSERT INTO diaries VALUES("Groonga", "Hello Groonga");
SELECT * FROM diaries
WHERE MATCH(title) AGAINST('content:="Hello Mroonga"' IN BOOLEAN MODE);
title content
Groonga Hello Mroonga
DROP TABLE diaries;
SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;
@@ -0,0 +1,15 @@
SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;
SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY";
SET NAMES UTF8;
CREATE TABLE diaries (
title TEXT,
FULLTEXT KEY (title)
) DEFAULT CHARSET=utf8;
INSERT INTO diaries VALUES("Re:Mroonga");
SELECT * FROM diaries WHERE MATCH(title) AGAINST("Re:Mroonga" IN BOOLEAN MODE);
title
Re:Mroonga
DROP TABLE diaries;
SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;
@@ -0,0 +1,16 @@
SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;
SET mroonga_boolean_mode_syntax_flags = "SYNTAX_SCRIPT";
SET NAMES UTF8;
CREATE TABLE diaries (
title TEXT,
FULLTEXT KEY (title)
) DEFAULT CHARSET=utf8;
INSERT INTO diaries VALUES("Re:Mroonga");
SELECT * FROM diaries
WHERE MATCH(title) AGAINST("title @ 'Re:Mroonga'" IN BOOLEAN MODE);
title
Re:Mroonga
DROP TABLE diaries;
SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;
@@ -0,0 +1,43 @@
# Copyright(C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

--source ../../../../include/mroonga/have_mroonga.inc

SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;

SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY,ALLOW_COLUMN";

SET NAMES UTF8;

CREATE TABLE diaries (
title TEXT,
content TEXT,
FULLTEXT KEY (title),
FULLTEXT KEY (content)
) DEFAULT CHARSET=utf8;

INSERT INTO diaries VALUES("Groonga", "Hello Groonga");

SELECT * FROM diaries
WHERE MATCH(title) AGAINST("content:@Hello" IN BOOLEAN MODE);

DROP TABLE diaries;

SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;

--source ../../../../include/mroonga/have_mroonga_deinit.inc
@@ -0,0 +1,41 @@
# Copyright(C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

--source ../../../../include/mroonga/have_mroonga.inc

SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;

SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY,ALLOW_LEADING_NOT";

SET NAMES UTF8;

CREATE TABLE diaries (
title TEXT,
FULLTEXT KEY (title)
) DEFAULT CHARSET=utf8;

INSERT INTO diaries VALUES("Groonga");
INSERT INTO diaries VALUES("Mroonga");

SELECT * FROM diaries WHERE MATCH(title) AGAINST("-Groonga" IN BOOLEAN MODE);

DROP TABLE diaries;

SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;

--source ../../../../include/mroonga/have_mroonga_deinit.inc
@@ -0,0 +1,43 @@
# Copyright(C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

--source ../../../../include/mroonga/have_mroonga.inc

SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;

SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY,ALLOW_COLUMN,ALLOW_UPDATE";

SET NAMES UTF8;

CREATE TABLE diaries (
title TEXT,
content TEXT,
FULLTEXT KEY (title),
FULLTEXT KEY (content)
) DEFAULT CHARSET=utf8;

INSERT INTO diaries VALUES("Groonga", "Hello Groonga");

SELECT * FROM diaries
WHERE MATCH(title) AGAINST('content:="Hello Mroonga"' IN BOOLEAN MODE);

DROP TABLE diaries;

SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;

--source ../../../../include/mroonga/have_mroonga_deinit.inc
@@ -0,0 +1,40 @@
# Copyright(C) 2014 Kouhei Sutou <kou@clear-code.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

--source ../../../../include/mroonga/have_mroonga.inc

SET @mroonga_boolean_mode_syntax_flags_backup =
@@mroonga_boolean_mode_syntax_flags;

SET mroonga_boolean_mode_syntax_flags = "SYNTAX_QUERY";

SET NAMES UTF8;

CREATE TABLE diaries (
title TEXT,
FULLTEXT KEY (title)
) DEFAULT CHARSET=utf8;

INSERT INTO diaries VALUES("Re:Mroonga");

SELECT * FROM diaries WHERE MATCH(title) AGAINST("Re:Mroonga" IN BOOLEAN MODE);

DROP TABLE diaries;

SET mroonga_boolean_mode_syntax_flags =
@mroonga_boolean_mode_syntax_flags_backup;

--source ../../../../include/mroonga/have_mroonga_deinit.inc

0 comments on commit dd097fe

Please sign in to comment.