-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathREADME.sql
More file actions
140 lines (121 loc) · 5.35 KB
/
Copy pathREADME.sql
File metadata and controls
140 lines (121 loc) · 5.35 KB
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
/*
* Inverted Index Toolkit <http://code.google.com/p/inverted-index/>
* Apache License 2.0, blah blah blah.
*
* This code is an _example_ implementation of the Inverted Index
* Technique, coded using MySQL stored procedures and triggers.
*
* To run, create a mysql database, and then pipe this file (README.sql)
* to the mysql command-line client:
mysqladmin -u User -pPassword create invertedindex
mysql -u User -pPassword invertedindex < README.sql
* Alternatively, source the following SQL files, in order. If you're at
* the MySQL command prompt, do:
*/
\. sql/tables.sql
\. sql/sp_wordID.sql
\. sql/sp_wordID_withNew.sql
\. sql/sp_classID.sql
\. sql/sp_nextWord.sql
\. sql/sp_parseWords.sql
\. sql/sp_sanitizeWord.sql
\. sql/sp_indexString.sql
\. sql/sp_buildSearchParts.sql
\. sql/sp_search.sql
\. sql/sp_searchBasic.sql
\. sql/sp_searchPhrase.sql
-- -----------------------------------------------------------------------
-- and the sample data, which shows how to set up the correct triggers
-- -----------------------------------------------------------------------
\. tests/setup.sql
\. tests/triggers.sql
\. tests/data.sql
-- -----------------------------------------------------------------------
-- Then try out the searching procedures:
-- -----------------------------------------------------------------------
CALL searchBasic(classID('article'), 'night time');
CALL searchPhrase(classID('article.body'), 'best of times');
-- -----------------------------------------------------------------------
-- If you want to control the content output, then use the main 'search'
-- procedure directly:
-- -----------------------------------------------------------------------
CALL search(classID('article.body'), 'best of times', 'phrase',
'article', 'article_id', 'title,body');
CALL search(classID('article.body'), 'best of times', 'phrase',
NULL, NULL, NULL);
-- -----------------------------------------------------------------------
-- or run some of the "manual" SQL queries instead:
-- -----------------------------------------------------------------------
\. tests/query1.sql
\. tests/query2.sql
\. tests/query3.sql
\. tests/query4.sql
-- -----------------------------------------------------------------------
-- The SQL the stored procedures used is preserved in @_sql, so you can
-- see how the query works:
-- -----------------------------------------------------------------------
CALL searchPhrase(classID('article'), 'best of times');
SELECT @_sql;
-- -----------------------------------------------------------------------
-- The components of the query are also preserved, so you can construct
-- more complex queries. You can prevent this by setting
-- @_suppressGlobals = TRUE.
-- -----------------------------------------------------------------------
SELECT CONCAT('SELECT ',@_outputFormat,
' FROM ',@_froms,
' WHERE ',@_wheres,
' GROUP BY ',@_groupBy,
' ORDER BY d.title') AS sql_from_parts;
-- -----------------------------------------------------------------------
-- If you set @_noExecute, then the query isn't actually executed, so you
-- can construct your own custom query.
-- -----------------------------------------------------------------------
SET @_noExecute = TRUE;
CALL searchPhrase(classID('article'), 'best of times');
SET @_noExecute = FALSE;
-- -----------------------------------------------------------------------
-- These functions can come in handy when using PREPARE and EXECUTE.
-- tests/query0.sql demonstrates this technique.
-- -----------------------------------------------------------------------
\. tests/query0.sql
-- -----------------------------------------------------------------------
-- If a word in a search string isn't found, then the result format is
-- different.
-- -----------------------------------------------------------------------
CALL searchPhrase(classID('article'), 'sadkhfasiuodyfaiusdhfaids');
/*
* You can also try some of the bigger example files in the 'corpus' branch
* (in SVN). This is a much larger dataset sourced from Project Gutenberg
* that takes longer to install, but gives a good test of the search
* functions.
*
*
* Notes:
*
* Column indexing these tables is critical. It's worth taking a good
* look at this with real data. The sample data here will not demonstrate
* correct indexing.
*
* Try out a few of the "CREATE INDEX search_index" lines in
* sql/tables.sql, and see if they improve performance: Do a sample query
* (using CALL searchBasic(...)), then SELECT @_sql, then DESCRIBE the
* resulting query to see if the new index was used (by looking in the
* 'key' column)
*
* Extra indexes can significantly slow down indexing time, but if they
* are used, they may massively improve query performance. The best thing
* to do is try them out!
*
*
* This implementation currently processes words in a very simple way: it
* just trims whitespace from the start and end of the word, and it
* converts it to lowercase.
*
* Words are considered to follow the pattern: [[:alnum:]]+, ie. a string
* of alphanumeric characters.
*
* Different applications of this code will need different rules.
* Addresses, in particular, have some specific needs, such as converting
* ordinals (1st <=> First); adding alternatives (Basement Flat <=> Garden
* Flat); abbreviations (ave. <=> Avenue).
*/