Debian Codegraph @

doc/source/tutorial/search.rst

raw
1 .. -*- rst -*-
2
3 .. groonga-include : data.rst
4
5 .. groonga-command
6 .. database: tutorial
7
8 Various search conditions
9 =========================
10
11 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).
12
13 Narrow down & Full-text search by using syntax like JavaScript
14 --------------------------------------------------------------
15
16 The ``filter`` parameter of ``select`` command accepts the search condition.
17 There is one difference between ``filter`` parameter and ``query`` parameter, you need to specify the condition by syntax like JavaScript for ``filter`` parameter.
18
19 .. groonga-command
20 .. include:: ../example/tutorial/search-1.log
21 .. select --table Site --filter "_id <= 1" --output_columns _id,_key
22
23 See the detail of above query. Here is the condition which is specified as ``filter`` parameter::
24
25 _id <= 1
26
27 In this case, this query returns the records which meets the condition that the value of ``_id`` is equal to or less than 1.
28
29 Moreover, you can use ``&&`` for AND search, ``||`` for OR search.
30
31 .. groonga-command
32 .. include:: ../example/tutorial/search-2.log
33 .. select --table Site --filter "_id >= 4 && _id <= 6" --output_columns _id,_key
34 .. select --table Site --filter "_id <= 2 || _id >= 7" --output_columns _id,_key
35
36 If you specify ``query`` parameter and ``filter`` parameter at the same time, you can get the records which meets both of the condition as a result.
37
38 Sort by using ``scorer``
39 ------------------------
40
41 ``select`` command accepts ``scorer`` parameter which is used to process each record of full-text search results.
42
43 This parameter accepts the conditions which is specified by syntax like JavaScript as same as ``filter`` parameter.
44
45 .. groonga-command
46 .. include:: ../example/tutorial/search-3.log
47 .. select --table Site --filter "true" --scorer "_score = rand()" --output_columns _id,_key,_score --sort_keys _score
48 .. select --table Site --filter "true" --scorer "_score = rand()" --output_columns _id,_key,_score --sort_keys _score
49
50 '_score' is one of a pseudo column. The score of full-text search is assigned to it.
51 See :doc:`/reference/columns/pseudo` about '_score' column.
52
53 In the above query, the condition of ``scorer`` parameter is::
54
55 _score = rand()
56
57 In this case, the score of full-text search is overwritten by the value of rand() function.
58
59 The condition of ``sort_keys`` parameter is::
60
61 _score
62
63 This means that sorting the search result by ascending order.
64
65 As a result, the order of search result is randomized.
66
67 Narrow down & sort by using location information
68 ------------------------------------------------
69
70 Groonga supports to store location information (Longitude & Latitude) and not only narrow down but also sort by using it.
71
72 Groonga supports two kind of column types to store location information. One is ``TokyoGeoPoint``, the other is ``WGS84GeoPoint``. ``TokyoGeoPoint`` is used for Japan geodetic system. ``WGS84GeoPoint`` is used for world geodetic system.
73
74 Specify longitude and latitude as follows:
75
76 * "[latitude in milliseconds]x[longitude in milliseconds]"(e.g.: "128452975x503157902")
77 * "[latitude in milliseconds],[longitude in milliseconds]"(e.g.: "128452975,503157902")
78 * "[latitude in degrees]x[longitude in degrees]"(e.g.: "35.6813819x139.7660839")
79 * "[latitude in degrees],[longitude in degrees]"(e.g.: "35.6813819,139.7660839")
80
81 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.
82
83 Let's register location information in milliseconds.
84
85 .. groonga-command
86 .. include:: ../example/tutorial/search-4.log
87 .. column_create --table Site --name location --type WGS84GeoPoint
88 .. load --table Site
89 .. [
90 .. {"_key":"http://example.org/","location":"128452975x503157902"}
91 .. {"_key":"http://example.net/","location":"128487316x502920929"},
92 .. ]
93 .. select --table Site --query "_id:1 OR _id:2" --output_columns _key,location
94
95 Then assign the value of geo distance which is calculated by :doc:`/reference/functions/geo_distance` function to ``scorer`` parameter.
96
97 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.
98
99 .. groonga-command
100 .. include:: ../example/tutorial/search-5.log
101 .. select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")'
102
103 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.
104
105 The return value of geo_distance function is also used for sorting by specifying pseudo ``_score`` column to ``sort_keys`` parameter.
106
107 .. groonga-command
108 .. include:: ../example/tutorial/search-6.log
109 .. select --table Site --query "_id:1 OR _id:2" --output_columns _key,location,_score --scorer '_score = geo_distance(location, "128515259x503187188")' --sort_keys -_score
110
111 Groonga also supports to narrow down by "a certain point within specified meters".
112
113 In such a case, use :doc:`/reference/functions/geo_in_circle` function in ``filter`` parameter.
114
115 For example, search the records which exists within 5000 meters from Akihabara station.
116
117 .. groonga-command
118 .. include:: ../example/tutorial/search-7.log
119 .. select --table Site --output_columns _key,location --filter 'geo_in_circle(location, "128515259x503187188", 5000)'
120
121 There is :doc:`/reference/functions/geo_in_rectangle` function which is used to search a certain point within specified region.