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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>4.4. Various search conditions — 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.5. Drilldown" href="drilldown.html" />
<link rel="prev" title="4.3. Various data types" href="data.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/search.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="drilldown.html" title="4.5. Drilldown"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="data.html" title="4.3. Various data types"
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.4. </span>Various search conditions</a></li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="various-search-conditions">
<h1><span class="section-number">4.4. </span>Various search conditions<a class="headerlink" href="#various-search-conditions" title="Permalink to this headline">¶</a></h1>
<p>Groonga supports to narrow down by using syntax like JavaScript, sort by the calculated value. Additionally, Groonga also supports to narrow down & sort search results by using location information (latitude & longitude).</p>
<div class="section" id="narrow-down-full-text-search-by-using-syntax-like-javascript">
<h2><span class="section-number">4.4.1. </span>Narrow down & Full-text search by using syntax like JavaScript<a class="headerlink" href="#narrow-down-full-text-search-by-using-syntax-like-javascript" title="Permalink to this headline">¶</a></h2>
<p>The <code class="docutils literal notranslate"><span class="pre">filter</span></code> parameter of <code class="docutils literal notranslate"><span class="pre">select</span></code> command accepts the search condition.
There is one difference between <code class="docutils literal notranslate"><span class="pre">filter</span></code> parameter and <code class="docutils literal notranslate"><span class="pre">query</span></code> parameter, you need to specify the condition by syntax like JavaScript for <code class="docutils literal notranslate"><span class="pre">filter</span></code> parameter.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --filter "_id <= 1" --output_columns _id,_key
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ]
# ],
# [
# 1,
# "http://example.org/"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>See the detail of above query. Here is the condition which is specified as <code class="docutils literal notranslate"><span class="pre">filter</span></code> parameter:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>_id <= 1
</pre></div>
</div>
<p>In this case, this query returns the records which meets the condition that the value of <code class="docutils literal notranslate"><span class="pre">_id</span></code> is equal to or less than 1.</p>
<p>Moreover, you can use <code class="docutils literal notranslate"><span class="pre">&&</span></code> for AND search, <code class="docutils literal notranslate"><span class="pre">||</span></code> for OR search.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --filter "_id >= 4 && _id <= 6" --output_columns _id,_key
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 3
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ]
# ],
# [
# 4,
# "http://example.net/afr"
# ],
# [
# 5,
# "http://example.org/aba"
# ],
# [
# 6,
# "http://example.com/rab"
# ]
# ]
# ]
# ]
select --table Site --filter "_id <= 2 || _id >= 7" --output_columns _id,_key
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 5
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ]
# ],
# [
# 1,
# "http://example.org/"
# ],
# [
# 2,
# "http://example.net/"
# ],
# [
# 7,
# "http://example.net/atv"
# ],
# [
# 8,
# "http://example.org/gat"
# ],
# [
# 9,
# "http://example.com/vdw"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>If you specify <code class="docutils literal notranslate"><span class="pre">query</span></code> parameter and <code class="docutils literal notranslate"><span class="pre">filter</span></code> parameter at the same time, you can get the records which meets both of the condition as a result.</p>
</div>
<div class="section" id="sort-by-using-scorer">
<h2><span class="section-number">4.4.2. </span>Sort by using <code class="docutils literal notranslate"><span class="pre">scorer</span></code><a class="headerlink" href="#sort-by-using-scorer" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal notranslate"><span class="pre">select</span></code> command accepts <code class="docutils literal notranslate"><span class="pre">scorer</span></code> parameter which is used to process each record of full-text search results.</p>
<p>This parameter accepts the conditions which is specified by syntax like JavaScript as same as <code class="docutils literal notranslate"><span class="pre">filter</span></code> parameter.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --filter "true" --scorer "_score = rand()" --output_columns _id,_key,_score --sort_keys _score
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 9
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "_score",
# "Int32"
# ]
# ],
# [
# 6,
# "http://example.com/rab",
# 424238335
# ],
# [
# 9,
# "http://example.com/vdw",
# 596516649
# ],
# [
# 7,
# "http://example.net/atv",
# 719885386
# ],
# [
# 2,
# "http://example.net/",
# 846930886
# ],
# [
# 8,
# "http://example.org/gat",
# 1649760492
# ],
# [
# 3,
# "http://example.com/",
# 1681692777
# ],
# [
# 4,
# "http://example.net/afr",
# 1714636915
# ],
# [
# 1,
# "http://example.org/",
# 1804289383
# ],
# [
# 5,
# "http://example.org/aba",
# 1957747793
# ]
# ]
# ]
# ]
select --table Site --filter "true" --scorer "_score = rand()" --output_columns _id,_key,_score --sort_keys _score
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 9
# ],
# [
# [
# "_id",
# "UInt32"
# ],
# [
# "_key",
# "ShortText"
# ],
# [
# "_score",
# "Int32"
# ]
# ],
# [
# 4,
# "http://example.net/afr",
# 783368690
# ],
# [
# 2,
# "http://example.net/",
# 1025202362
# ],
# [
# 5,
# "http://example.org/aba",
# 1102520059
# ],
# [
# 1,
# "http://example.org/",
# 1189641421
# ],
# [
# 3,
# "http://example.com/",
# 1350490027
# ],
# [
# 8,
# "http://example.org/gat",
# 1365180540
# ],
# [
# 9,
# "http://example.com/vdw",
# 1540383426
# ],
# [
# 7,
# "http://example.net/atv",
# 1967513926
# ],
# [
# 6,
# "http://example.com/rab",
# 2044897763
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>‘_score’ is one of a pseudo column. The score of full-text search is assigned to it.
See <a class="reference internal" href="../reference/columns/pseudo.html"><span class="doc">Pseudo column</span></a> about ‘_score’ column.</p>
<p>In the above query, the condition of <code class="docutils literal notranslate"><span class="pre">scorer</span></code> parameter is:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>_score = rand()
</pre></div>
</div>
<p>In this case, the score of full-text search is overwritten by the value of rand() function.</p>
<p>The condition of <code class="docutils literal notranslate"><span class="pre">sort_keys</span></code> parameter is:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>_score
</pre></div>
</div>
<p>This means that sorting the search result by ascending order.</p>
<p>As a result, the order of search result is randomized.</p>
</div>
<div class="section" id="narrow-down-sort-by-using-location-information">
<h2><span class="section-number">4.4.3. </span>Narrow down & sort by using location information<a class="headerlink" href="#narrow-down-sort-by-using-location-information" title="Permalink to this headline">¶</a></h2>
<p>Groonga supports to store location information (Longitude & Latitude) and not only narrow down but also sort by using it.</p>
<p>Groonga supports two kind of column types to store location information. One is <code class="docutils literal notranslate"><span class="pre">TokyoGeoPoint</span></code>, the other is <code class="docutils literal notranslate"><span class="pre">WGS84GeoPoint</span></code>. <code class="docutils literal notranslate"><span class="pre">TokyoGeoPoint</span></code> is used for Japan geodetic system. <code class="docutils literal notranslate"><span class="pre">WGS84GeoPoint</span></code> is used for world geodetic system.</p>
<p>Specify longitude and latitude as follows:</p>
<ul class="simple">
<li><p>“[latitude in milliseconds]x[longitude in milliseconds]”(e.g.: “128452975x503157902”)</p></li>
<li><p>“[latitude in milliseconds],[longitude in milliseconds]”(e.g.: “128452975,503157902”)</p></li>
<li><p>“[latitude in degrees]x[longitude in degrees]”(e.g.: “35.6813819x139.7660839”)</p></li>
<li><p>“[latitude in degrees],[longitude in degrees]”(e.g.: “35.6813819,139.7660839”)</p></li>
</ul>
<p>Let’s store two location information about station in Japan by WGS. One is Tokyo station, the other is Shinjyuku station. Both of them are station in Japan. The latitude of Tokyo station is 35 degrees 40 minutes 52.975 seconds, the longitude of Tokyo station is 139 degrees 45 minutes 57.902 seconds. The latitude of Shinjyuku station is 35 degrees 41 minutes 27.316 seconds, the longitude of Shinjyuku station is 139 degrees 42 minutes 0.929 seconds. Thus, location information in milliseconds are “128452975x503157902” and “128487316x502920929” respectively. location information in degrees are “35.6813819x139.7660839” and “35.6909211x139.7002581” respectively.</p>
<p>Let’s register location information in milliseconds.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>column_create --table Site --name location --type WGS84GeoPoint
# [[0, 1337566253.89858, 0.000355720520019531], true]
load --table Site
[
{"_key":"http://example.org/","location":"128452975x503157902"}
{"_key":"http://example.net/","location":"128487316x502920929"},
]
# [[0, 1337566253.89858, 0.000355720520019531], 2]
select --table Site --query "_id:1 OR _id:2" --output_columns _key,location
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "_key",
# "ShortText"
# ],
# [
# "location",
# "WGS84GeoPoint"
# ]
# ],
# [
# "http://example.org/",
# "128452975x503157902"
# ],
# [
# "http://example.net/",
# "128487316x502920929"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>Then assign the value of geo distance which is calculated by <a class="reference internal" href="../reference/functions/geo_distance.html"><span class="doc">geo_distance</span></a> function to <code class="docutils literal notranslate"><span class="pre">scorer</span></code> parameter.</p>
<p>Let’s show geo distance from Akihabara station in Japan. In world geodetic system, the latitude of Akihabara station is 35 degrees 41 minutes 55.259 seconds, the longitude of Akihabara station is 139 degrees 46 minutes 27.188 seconds. Specify “128515259x503187188” for geo_distance function.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")'
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "_key",
# "ShortText"
# ],
# [
# "location",
# "WGS84GeoPoint"
# ],
# [
# "_score",
# "Int32"
# ]
# ],
# [
# "http://example.org/",
# "128452975x503157902",
# 2054
# ],
# [
# "http://example.net/",
# "128487316x502920929",
# 6720
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>As you can see, the geo distance between Tokyo station and Akihabara station is 2054 meters, the geo distance between Akihabara station and Shinjyuku station is 6720 meters.</p>
<p>The return value of geo_distance function is also used for sorting by specifying pseudo <code class="docutils literal notranslate"><span class="pre">_score</span></code> column to <code class="docutils literal notranslate"><span class="pre">sort_keys</span></code> parameter.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")' --sort_keys -_score
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 2
# ],
# [
# [
# "_key",
# "ShortText"
# ],
# [
# "location",
# "WGS84GeoPoint"
# ],
# [
# "_score",
# "Int32"
# ]
# ],
# [
# "http://example.net/",
# "128487316x502920929",
# 6720
# ],
# [
# "http://example.org/",
# "128452975x503157902",
# 2054
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>Groonga also supports to narrow down by “a certain point within specified meters”.</p>
<p>In such a case, use <a class="reference internal" href="../reference/functions/geo_in_circle.html"><span class="doc">geo_in_circle</span></a> function in <code class="docutils literal notranslate"><span class="pre">filter</span></code> parameter.</p>
<p>For example, search the records which exists within 5000 meters from Akihabara station.</p>
<p>Execution example:</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>select --table Site --output_columns _key,location --filter 'geo_in_circle(location, "128515259x503187188", 5000)'
# [
# [
# 0,
# 1337566253.89858,
# 0.000355720520019531
# ],
# [
# [
# [
# 1
# ],
# [
# [
# "_key",
# "ShortText"
# ],
# [
# "location",
# "WGS84GeoPoint"
# ]
# ],
# [
# "http://example.org/",
# "128452975x503157902"
# ]
# ]
# ]
# ]
</pre></div>
</div>
<p>There is <a class="reference internal" href="../reference/functions/geo_in_rectangle.html"><span class="doc">geo_in_rectangle</span></a> function which is used to search a certain point within specified region.</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.4. Various search conditions</a><ul>
<li><a class="reference internal" href="#narrow-down-full-text-search-by-using-syntax-like-javascript">4.4.1. Narrow down & Full-text search by using syntax like JavaScript</a></li>
<li><a class="reference internal" href="#sort-by-using-scorer">4.4.2. Sort by using <code class="docutils literal notranslate"><span class="pre">scorer</span></code></a></li>
<li><a class="reference internal" href="#narrow-down-sort-by-using-location-information">4.4.3. Narrow down & sort by using location information</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="data.html"
title="previous chapter"><span class="section-number">4.3. </span>Various data types</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="drilldown.html"
title="next chapter"><span class="section-number">4.5. </span>Drilldown</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="drilldown.html" title="4.5. Drilldown"
>next</a> |</li>
<li class="right" >
<a href="data.html" title="4.3. Various data types"
>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.4. </span>Various search conditions</a></li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2009-2021, Brazil, Inc.
</div>
</body>
</html>
|