1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 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 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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 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 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 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 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 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 575 576 577 578 579 580 581 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 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 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 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 741 742 743 744 745 746 747 748 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 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 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>4.7. match_columns parameter — Groonga v10.1.1-31-g1e46ba6 documentation</title>
<link rel="stylesheet" href="../_static/groonga.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<link rel="shortcut icon" href="../_static/favicon.ico"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="4.8. Prefix search with patricia trie" href="patricia_trie.html" />
<link rel="prev" title="4.6. Tag search and reverse resolution of reference relationships" href="index.html" />
</head><body>
<div class="header">
<h1 class="title">
<a id="top-link" href="../index.html">
<span class="project">groonga</span>
<span class="separator">-</span>
<span class="description">An open-source fulltext search engine and column store.</span>
</a>
</h1>
<div class="other-language-links">
<ul>
<li><a href="../../../ja/html/tutorial/match_columns.html">日本語</a></li>
</ul>
</div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="patricia_trie.html" title="4.8. Prefix search with patricia trie"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="index.html" title="4.6. Tag search and reverse resolution of reference relationships"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Groonga v10.1.1-31-g1e46ba6 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../tutorial.html" accesskey="U"><span class="section-number">4. </span>Tutorial</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">4.7. </span>match_columns parameter</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="match-columns-parameter">
<h1><span class="section-number">4.7. </span>match_columns parameter<a class="headerlink" href="#match-columns-parameter" title="Permalink to this headline">¶</a></h1>
<div class="section" id="full-text-search-against-multiple-columns">
<h2><span class="section-number">4.7.1. </span>Full-text search against multiple columns<a class="headerlink" href="#full-text-search-against-multiple-columns" title="Permalink to this headline">¶</a></h2>
<p>Groonga supports full-text search against multiple columns. Let’s consider blog site. Usually, blog site has a table which contains title column and content column. How do you search the blog entry which contains specified keywords in title or content?</p>
<p>In such a case, there are two ways to create indexes. One way is creating column index against each column. The other way is creating one column index against multiple columns. Either way, Groonga supports similar full-text search syntax.</p>
<div class="section" id="creating-column-index-against-each-column">
<span id="id1"></span><h3><span class="section-number">4.7.1.1. </span>Creating column index against each column<a class="headerlink" href="#creating-column-index-against-each-column" title="Permalink to this headline">¶</a></h3>
<p>Here is the example which create column index against each column.</p>
<p>First, create <code class="docutils literal notranslate"><span class="pre">Blog1</span></code> table, add <code class="docutils literal notranslate"><span class="pre">title</span></code> column which stores title string, <code class="docutils literal notranslate"><span class="pre">message</span></code> column which stores content of blog entry.</p>
<p>Then create <code class="docutils literal notranslate"><span class="pre">IndexBlog1</span></code> table for column indexes, add <code class="docutils literal notranslate"><span class="pre">index_title</span></code> column for <code class="docutils literal notranslate"><span class="pre">title</span></code> column, <code class="docutils literal notranslate"><span class="pre">index_message</span></code> column for <code class="docutils literal notranslate"><span class="pre">message</span></code> column.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create --name Blog1 --flags TABLE_HASH_KEY --key_type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create --table Blog1 --name title --flags COLUMN_SCALAR --type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create --table Blog1 --name message --flags COLUMN_SCALAR --type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create --name IndexBlog1 --flags TABLE_PAT_KEY --key_type ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create --table IndexBlog1 --name index_title --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source title
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create --table IndexBlog1 --name index_message --flags COLUMN_INDEX|WITH_POSITION --type Blog1 --source message
# [[0, 1337566253.89858, 0.000355720520019531], true]
load --table Blog1
[
{"_key":"grn1","title":"Groonga test","message":"Groonga message"},
{"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 Groonga moritars"},
{"_key":"grn3","title":"Groonga message","message":"none"}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">match_columns</span></code> option of <code class="docutils literal notranslate"><span class="pre">select</span></code> command accepts multiple columns as search target.
Specify query string to <code class="docutils literal notranslate"><span class="pre">query</span></code> option. Then you can do full-text search title and content of blog entries.</p>
<p>Let’s try to search blog entries.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Blog1 --match_columns title||message --query groonga
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 3
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "message",
# "ShortText"
# ],
# [
# "title",
# "ShortText"
# ]
# ],
# [
# 1,
# "grn1",
# "Groonga message",
# "Groonga test"
# ],
# [
# 3,
# "grn3",
# "none",
# "Groonga message"
# ],
# [
# 2,
# "grn2",
# "rakutan eggs 4 - 4 Groonga moritars",
# "baseball result"
# ]
# ]
# ]
# ]
select --table Blog1 --match_columns title||message --query message
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "message",
# "ShortText"
# ],
# [
# "title",
# "ShortText"
# ]
# ],
# [
# 3,
# "grn3",
# "none",
# "Groonga message"
# ],
# [
# 1,
# "grn1",
# "Groonga message",
# "Groonga test"
# ]
# ]
# ]
# ]
select --table Blog1 --match_columns title --query message
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "message",
# "ShortText"
# ],
# [
# "title",
# "ShortText"
# ]
# ],
# [
# 3,
# "grn3",
# "none",
# "Groonga message"
# ]
# ]
# ]
# ]
</pre></div>
</div>
</div>
<div class="section" id="creating-one-column-index-against-multiple-columns">
<h3><span class="section-number">4.7.1.2. </span>Creating one column index against multiple columns<a class="headerlink" href="#creating-one-column-index-against-multiple-columns" title="Permalink to this headline">¶</a></h3>
<p>Groonga also supports one column index against multiple columns.</p>
<p>The difference for previous example is only one column index exists. Thus, There is one common column index against title and message column.</p>
<p>Even though same column index is used, Groonga supports to search against title column only, message column only and title or message column.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create --name Blog2 --flags TABLE_HASH_KEY --key_type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create --table Blog2 --name title --flags COLUMN_SCALAR --type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create --table Blog2 --name message --flags COLUMN_SCALAR --type ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create --name IndexBlog2 --flags TABLE_PAT_KEY --key_type ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create --table IndexBlog2 --name index_blog --flags COLUMN_INDEX|WITH_POSITION|WITH_SECTION --type Blog2 --source title,message
# [[0, 1337566253.89858, 0.000355720520019531], true]
load --table Blog2
[
{"_key":"grn1","title":"Groonga test","message":"Groonga message"},
{"_key":"grn2","title":"baseball result","message":"rakutan eggs 4 - 4 Groonga moritars"},
{"_key":"grn3","title":"Groonga message","message":"none"}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
</pre></div>
</div>
<p>Let’s search same query in previous section. You can get same search results.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Blog2 --match_columns title||message --query groonga
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 3
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "message",
# "ShortText"
# ],
# [
# "title",
# "ShortText"
# ]
# ],
# [
# 1,
# "grn1",
# "Groonga message",
# "Groonga test"
# ],
# [
# 2,
# "grn2",
# "rakutan eggs 4 - 4 Groonga moritars",
# "baseball result"
# ],
# [
# 3,
# "grn3",
# "none",
# "Groonga message"
# ]
# ]
# ]
# ]
select --table Blog2 --match_columns title||message --query message
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "message",
# "ShortText"
# ],
# [
# "title",
# "ShortText"
# ]
# ],
# [
# 1,
# "grn1",
# "Groonga message",
# "Groonga test"
# ],
# [
# 3,
# "grn3",
# "none",
# "Groonga message"
# ]
# ]
# ]
# ]
select --table Blog2 --match_columns title --query message
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "message",
# "ShortText"
# ],
# [
# "title",
# "ShortText"
# ]
# ],
# [
# 3,
# "grn3",
# "none",
# "Groonga message"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>There may be a question that “which is the better solution for indexing.”
It depends on the case.</p>
<ul class="simple">
<li><p>Indexes for each column - The update performance tends to be better than multiple colum index because there is enough buffer for updating. On the other hand, the efficiency of disk usage is not so good.</p></li>
<li><p>Indexes for multiple column - It saves disk usage because it shares common buffer. On the other hand, the update performance is not so good.</p></li>
</ul>
</div>
</div>
</div>
<div class="section" id="full-text-search-with-specific-index-name">
<span id="id2"></span><h2><span class="section-number">4.7.2. </span>Full text search with specific index name<a class="headerlink" href="#full-text-search-with-specific-index-name" title="Permalink to this headline">¶</a></h2>
<p>Groonga also supports full text search with specific index name.</p>
<p>In this section, you learn how to use specific index column efficiently.</p>
<p>Here is the concrete example about specific index name.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create Entries TABLE_HASH_KEY ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Entries title COLUMN_SCALAR ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Entries body COLUMN_SCALAR ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
load --table Entries
[
{"_key": "http://example.com/entry1", "title":"Hello Groonga.", "body":"This is my first entry."},
{"_key": "http://example.com/entry2", "title":"Hello world.", "body":"I love Groonga!"},
{"_key": "http://example.com/entry3", "title":"Hello Mroonga, bye Groonga.", "body":"I use Mroonga."},
{"_key": "http://example.com/entry4", "title":"Say, Hello Groonga!", "body":"I'm back."}
]
# [[0, 1337566253.89858, 0.000355720520019531], 4]
table_create Terms TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Terms entries_title COLUMN_INDEX|WITH_POSITION Entries title
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Terms entries_body COLUMN_INDEX|WITH_POSITION Entries body
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Terms entries_whole COLUMN_INDEX|WITH_POSITION|WITH_SECTION Entries title,body
# [[0, 1337566253.89858, 0.000355720520019531], true]
</pre></div>
</div>
<p>The table which stores entries has columns for title and body.
And the terms table has index columns for title and body to entries table.</p>
<p>There are three index columns in terms table.</p>
<ul class="simple">
<li><p>entries_title: index column for title</p></li>
<li><p>entries_body: index column for body</p></li>
<li><p>entries_whole: index column for title and body</p></li>
</ul>
<p>If you specify index column which is related to specific data column, related data column is searched with that index implicitly.</p>
<p>For example, if you want to search title or body only, specify <code class="docutils literal notranslate"><span class="pre">Terms.entries_title</span></code> or <code class="docutils literal notranslate"><span class="pre">Terms.entries_body</span></code> index column.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Entries --output_columns title --match_columns Terms.entries_title --query "Groonga"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 3
# ],
# [
# [
# "title",
# "ShortText"
# ]
# ],
# [
# "Hello Groonga."
# ],
# [
# "Hello Mroonga, bye Groonga."
# ],
# [
# "Say, Hello Groonga!"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>This example uses <code class="docutils literal notranslate"><span class="pre">Terms.entries_title</span></code> as index, then search “Groonga” against title data column.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Entries --output_columns body --match_columns Terms.entries_body --query "Groonga"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "body",
# "ShortText"
# ]
# ],
# [
# "I love Groonga!"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>This example uses <code class="docutils literal notranslate"><span class="pre">Terms.entries_body</span></code> as index, then search “Groonga” against body data column.</p>
<p>If you specify multiple index column which is related to specific data columns, you can also specify data column name as suffix. It means that “Use specific index and search specific data column explicitly”.</p>
<p>For example, if you want to search title or body only with <code class="docutils literal notranslate"><span class="pre">entries_whole</span></code> index, specify <code class="docutils literal notranslate"><span class="pre">Terms.entries_whole.title</span></code> or <code class="docutils literal notranslate"><span class="pre">Terms.entries_whole.body</span></code>. It uses <code class="docutils literal notranslate"><span class="pre">Terms.entries_whole</span></code> index and search <code class="docutils literal notranslate"><span class="pre">title</span></code> column or <code class="docutils literal notranslate"><span class="pre">body</span></code> column explicitly.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Entries --output_columns title --match_columns Terms.entries_whole.title --query "Groonga"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 3
# ],
# [
# [
# "title",
# "ShortText"
# ]
# ],
# [
# "Hello Groonga."
# ],
# [
# "Hello Mroonga, bye Groonga."
# ],
# [
# "Say, Hello Groonga!"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>This example uses <code class="docutils literal notranslate"><span class="pre">Terms.entries_whole</span></code> as index, then search “Groonga” against title data column.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Entries --output_columns body --match_columns Terms.entries_whole.body --query "Groonga"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "body",
# "ShortText"
# ]
# ],
# [
# "I love Groonga!"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>This example uses <code class="docutils literal notranslate"><span class="pre">Terms.entries_whole</span></code> as index, then search “Groonga” against body data column.</p>
</div>
<div class="section" id="nested-index-search-among-related-table-by-column-index">
<span id="nested-index-search"></span><h2><span class="section-number">4.7.3. </span>Nested index search among related table by column index<a class="headerlink" href="#nested-index-search-among-related-table-by-column-index" title="Permalink to this headline">¶</a></h2>
<p>If there are relationships among multiple table with column index,
you can search multiple table by specifying reference column name.</p>
<p>Here is the concrete example.</p>
<p>There are tables which store blog articles, comments for articles.
The table which stores articles has columns for article and comment.
And the comment column refers Comments table.
The table which stores comments has columns for comment and column index to article table.</p>
<p>if you want to search the articles which contain specified keyword in comment,
you need to execute fulltext search for table of comment, then search the records which contains fulltext search results.</p>
<p>But, you can search the records by specifying the reference column index at once.</p>
<p>Here is the sample schema.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create Comments TABLE_HASH_KEY UInt32
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Comments content COLUMN_SCALAR ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Articles TABLE_NO_KEY
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Articles content COLUMN_SCALAR Text
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Articles comment COLUMN_SCALAR Comments
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Lexicon TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Lexicon articles_content COLUMN_INDEX|WITH_POSITION Articles content
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Lexicon comments_content COLUMN_INDEX|WITH_POSITION Comments content
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Comments article COLUMN_INDEX Articles comment
# [[0, 1337566253.89858, 0.000355720520019531], true]
</pre></div>
</div>
<p>Here is the sample data.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>load --table Comments
[
{"_key": 1, "content": "I'm using Groonga too!"},
{"_key": 2, "content": "I'm using Groonga and Mroonga!"},
{"_key": 3, "content": "I'm using Mroonga too!"}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
load --table Articles
[
{"content": "Groonga is fast!", "comment": 1},
{"content": "Groonga is useful!"},
{"content": "Mroonga is fast!", "comment": 3}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
</pre></div>
</div>
<p>You can write the query that search the records which contains specified keyword as a comment, then fetch the articles which refers to it.</p>
<p>Query for searching the records described above:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Articles --match_columns comment.content --query groonga --output_columns "_id, _score, *"
</pre></div>
</div>
<p>You need to concatenate comment column of Articles table and content column of Comments table with period( <code class="docutils literal notranslate"><span class="pre">.</span></code> ) as <code class="docutils literal notranslate"><span class="pre">--match_columns</span></code> arguments.</p>
<p>At first, this query execute fulltext search from content of Comments table, then fetch the records of Articles table which refers to already searched records of Comments table.
(Because of this, if you comment out the query which creates index column <code class="docutils literal notranslate"><span class="pre">article</span></code> of Comments table, you can’t get intended search results.)</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Articles --match_columns comment.content --query groonga --output_columns "_id, _score, *"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_score",
# "Int32"
# ],
# [
# "comment",
# "Comments"
# ],
# [
# "content",
# "Text"
# ]
# ],
# [
# 1,
# 1,
# 1,
# "Groonga is fast!"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>Now, you can search articles which contains specific keywords as a comment.</p>
<p>The feature of nested index search is not limited to the relationship between two table only.</p>
<p>Here is the sample schema similar to previous one. The difference is added table which express ‘Reply’ and relationship is extended to three tables.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>table_create Replies2 TABLE_HASH_KEY UInt32
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Replies2 content COLUMN_SCALAR ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Comments2 TABLE_HASH_KEY UInt32
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Comments2 content COLUMN_SCALAR ShortText
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Comments2 comment COLUMN_SCALAR Replies2
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Articles2 TABLE_NO_KEY
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Articles2 content COLUMN_SCALAR Text
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Articles2 comment COLUMN_SCALAR Comments2
# [[0, 1337566253.89858, 0.000355720520019531], true]
table_create Lexicon2 TABLE_PAT_KEY ShortText --default_tokenizer TokenBigram --normalizer NormalizerAuto
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Lexicon2 articles_content COLUMN_INDEX|WITH_POSITION Articles2 content
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Lexicon2 comments_content COLUMN_INDEX|WITH_POSITION Comments2 content
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Lexicon2 replies_content COLUMN_INDEX|WITH_POSITION Replies2 content
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Comments2 article COLUMN_INDEX Articles2 comment
# [[0, 1337566253.89858, 0.000355720520019531], true]
column_create Replies2 reply_to COLUMN_INDEX Comments2 comment
# [[0, 1337566253.89858, 0.000355720520019531], true]
</pre></div>
</div>
<p>Here is the sample data.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>load --table Replies2
[
{"_key": 1, "content": "I'm using Rroonga too!"},
{"_key": 2, "content": "I'm using Groonga and Mroonga and Rroonga!"},
{"_key": 3, "content": "I'm using Nroonga too!"}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
load --table Comments2
[
{"_key": 1, "content": "I'm using Groonga too!", "comment": 1},
{"_key": 2, "content": "I'm using Groonga and Mroonga!", "comment": 2},
{"_key": 3, "content": "I'm using Mroonga too!"}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
load --table Articles2
[
{"content": "Groonga is fast!", "comment": 1},
{"content": "Groonga is useful!", "comment": 2},
{"content": "Mroonga is fast!", "comment": 3}
]
# [[0, 1337566253.89858, 0.000355720520019531], 3]
</pre></div>
</div>
<p>Query for searching the records described above:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Articles2 --match_columns comment.content --query mroonga --output_columns "_id, _score, *"
select Articles2 --match_columns comment.comment.content --query mroonga --output_columns "_id, _score, *"
</pre></div>
</div>
<p>The first query searches <code class="docutils literal notranslate"><span class="pre">mroonga</span></code> from <code class="docutils literal notranslate"><span class="pre">Comments2</span></code> table, the second one searches <code class="docutils literal notranslate"><span class="pre">mroonga</span></code> from <code class="docutils literal notranslate"><span class="pre">Replies2</span></code> and <code class="docutils literal notranslate"><span class="pre">Comments2</span></code> table by using reference column index.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select Articles2 --match_columns comment.content --query mroonga --output_columns "_id, _score, *"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_score",
# "Int32"
# ],
# [
# "comment",
# "Comments2"
# ],
# [
# "content",
# "Text"
# ]
# ],
# [
# 2,
# 1,
# 2,
# "Groonga is useful!"
# ],
# [
# 3,
# 1,
# 3,
# "Mroonga is fast!"
# ]
# ]
# ]
# ]
select Articles2 --match_columns comment.comment.content --query mroonga --output_columns "_id, _score, *"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_score",
# "Int32"
# ],
# [
# "comment",
# "Comments2"
# ],
# [
# "content",
# "Text"
# ]
# ],
# [
# 2,
# 1,
# 2,
# "Groonga is useful!"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>As a result, the first query matches two article because of <code class="docutils literal notranslate"><span class="pre">Comments2</span></code> table has two records which contains <code class="docutils literal notranslate"><span class="pre">mroonga</span></code> as keyword.</p>
<p>On the other hand, the second one matches one article only because of <code class="docutils literal notranslate"><span class="pre">Replies2</span></code> table has only one record which contains <code class="docutils literal notranslate"><span class="pre">mroonga</span></code> as keyword, and there is one record which contains same keyword and refers to the record in <code class="docutils literal notranslate"><span class="pre">Comments2</span></code> table.</p>
</div>
<div class="section" id="indexes-with-weight">
<h2><span class="section-number">4.7.4. </span>Indexes with Weight<a class="headerlink" href="#indexes-with-weight" title="Permalink to this headline">¶</a></h2>
<p>If index columns are created for data columns, you can search by indexes with weight.
For example, let’s try to search blog entries by indexes with weight which contains <code class="docutils literal notranslate"><span class="pre">Groonga</span></code> as important keyword in <code class="docutils literal notranslate"><span class="pre">Blog1</span></code> table.
Generally speaking, an important keyword tend to be included in blog title, so if <code class="docutils literal notranslate"><span class="pre">title</span></code> column contains <code class="docutils literal notranslate"><span class="pre">Groonga</span></code>, its score ( <code class="docutils literal notranslate"><span class="pre">_score</span></code> ) must be raised in contrast to <code class="docutils literal notranslate"><span class="pre">message</span></code> column. The indexes with weight is used for such a purpose.</p>
<p>Here is the example which search blog entries with <code class="docutils literal notranslate"><span class="pre">Groonga</span></code> as important keyword in <code class="docutils literal notranslate"><span class="pre">title</span></code> or <code class="docutils literal notranslate"><span class="pre">message</span></code> columns.</p>
<p>The sample schema and data is same as <a class="reference internal" href="#creating-column-index-against-each-column"><span class="std std-ref">Creating column index against each column</span></a>.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Blog1 --match_columns 'IndexBlog1.index_title * 10 || IndexBlog1.index_message' --query 'Groonga' --output_columns "_id, _score, *"
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 3
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_score",
# "Int32"
# ],
# [
# "message",
# "ShortText"
# ],
# [
# "title",
# "ShortText"
# ]
# ],
# [
# 1,
# 11,
# "Groonga message",
# "Groonga test"
# ],
# [
# 3,
# 10,
# "none",
# "Groonga message"
# ],
# [
# 2,
# 1,
# "rakutan eggs 4 - 4 Groonga moritars",
# "baseball result"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>In above query, <code class="docutils literal notranslate"><span class="pre">'IndexBlog1.index_title</span> <span class="pre">*</span> <span class="pre">10</span> <span class="pre">||</span> <span class="pre">IndexBlog1.index_message'</span></code> is specified for <code class="docutils literal notranslate"><span class="pre">--match_columns</span></code>.
It means that if <code class="docutils literal notranslate"><span class="pre">title</span></code> column (search <code class="docutils literal notranslate"><span class="pre">title</span></code> column using <code class="docutils literal notranslate"><span class="pre">IndexBlog1.index_title</span></code> index) matches to <code class="docutils literal notranslate"><span class="pre">Groonga</span></code>, its weight is multiplied to 10 and if <code class="docutils literal notranslate"><span class="pre">message</span></code> column (search <code class="docutils literal notranslate"><span class="pre">message</span></code> column using <code class="docutils literal notranslate"><span class="pre">IndexBlog1.index_message</span></code> index) matches to <code class="docutils literal notranslate"><span class="pre">Groonga</span></code>,
its weight is 1 (default). If <code class="docutils literal notranslate"><span class="pre">Groonga</span></code> matches to <code class="docutils literal notranslate"><span class="pre">title</span></code> and <code class="docutils literal notranslate"><span class="pre">message</span></code>, its weight is 11 (10 + 1) in this case.</p>
<p>As a result, <code class="docutils literal notranslate"><span class="pre">Groonga</span> <span class="pre">test</span></code> blog entry is listed in first.</p>
</div>
</div>
<div class="clearer"></div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">4.7. match_columns parameter</a><ul>
<li><a class="reference internal" href="#full-text-search-against-multiple-columns">4.7.1. Full-text search against multiple columns</a><ul>
<li><a class="reference internal" href="#creating-column-index-against-each-column">4.7.1.1. Creating column index against each column</a></li>
<li><a class="reference internal" href="#creating-one-column-index-against-multiple-columns">4.7.1.2. Creating one column index against multiple columns</a></li>
</ul>
</li>
<li><a class="reference internal" href="#full-text-search-with-specific-index-name">4.7.2. Full text search with specific index name</a></li>
<li><a class="reference internal" href="#nested-index-search-among-related-table-by-column-index">4.7.3. Nested index search among related table by column index</a></li>
<li><a class="reference internal" href="#indexes-with-weight">4.7.4. Indexes with Weight</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="index.html"
title="previous chapter"><span class="section-number">4.6. </span>Tag search and reverse resolution of reference relationships</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="patricia_trie.html"
title="next chapter"><span class="section-number">4.8. </span>Prefix search with patricia trie</a></p>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="patricia_trie.html" title="4.8. Prefix search with patricia trie"
>next</a> |</li>
<li class="right" >
<a href="index.html" title="4.6. Tag search and reverse resolution of reference relationships"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="../index.html">Groonga v10.1.1-31-g1e46ba6 documentation</a> »</li>
<li class="nav-item nav-item-1"><a href="../tutorial.html" ><span class="section-number">4. </span>Tutorial</a> »</li>
<li class="nav-item nav-item-this"><a href=""><span class="section-number">4.7. </span>match_columns parameter</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2009-2021, Brazil, Inc.
</div>
</body>
</html>
|