1Lucy::Search::QueryParsUesre(r3)Contributed Perl DocumenLtuactyi:o:nSearch::QueryParser(3)
2
3
4
6 Lucy::Search::QueryParser - Transform a string into a Query object.
7
9 my $query_parser = Lucy::Search::QueryParser->new(
10 schema => $searcher->get_schema,
11 fields => ['body'],
12 );
13 my $query = $query_parser->parse( $query_string );
14 my $hits = $searcher->hits( query => $query );
15
17 QueryParser accepts search strings as input and produces Query objects,
18 suitable for feeding into IndexSearcher and other Searcher subclasses.
19
20 The following syntactical constructs are recognized by QueryParser:
21
22 • Boolean operators XANDX, XORX, and XAND NOTX.
23
24 • Prepented +plus and -minus, indicating that the labeled entity
25 should be either required or forbidden X be it a single word, a
26 phrase, or a parenthetical group.
27
28 • Logical groups, delimited by parentheses.
29
30 • Phrases, delimited by double quotes.
31
32 Additionally, the following syntax can be enabled via
33 set_heed_colons():
34
35 • Field-specific constructs, in the form of Xfieldname:termtextX or
36 Xfieldname:(foo bar)X. (The field specified by Xfieldname:X will
37 be used instead of the QueryParserXs default fields).
38
40 new
41 my $query_parser = Lucy::Search::QueryParser->new(
42 schema => $searcher->get_schema, # required
43 analyzer => $analyzer, # overrides schema
44 fields => ['bodytext'], # default: indexed fields
45 default_boolop => 'AND', # default: 'OR'
46 );
47
48 Constructor.
49
50 • schema - A Schema.
51
52 • analyzer - An Analyzer. Ordinarily, the analyzers specified by
53 each fieldXs definition will be used, but if "analyzer" is
54 supplied, it will override and be used for all fields. This can
55 lead to mismatches between what is in the index and what is being
56 searched for, so use caution.
57
58 • fields - The names of the fields which will be searched against.
59 Defaults to those fields which are defined as indexed in the
60 supplied Schema.
61
62 • default_boolop - Two possible values: XANDX and XORX. The default
63 is XORX, which means: return documents which match any of the query
64 terms. If you want only documents which match all of the query
65 terms, set this to XANDX.
66
68 parse
69 my $query = $query_parser->parse($query_string);
70 my $query = $query_parser->parse(); # default: undef
71
72 Build a Query object from the contents of a query string. At present,
73 implemented internally by calling tree(), expand(), and prune().
74
75 • query_string - The string to be parsed. May be undef.
76
77 Returns: a Query.
78
79 tree
80 my $query = $query_parser->tree($query_string);
81
82 Parse the logical structure of a query string, building a tree
83 comprised of Query objects. Leaf nodes in the tree will most often be
84 LeafQuery objects but might be MatchAllQuery or NoMatchQuery objects as
85 well. Internal nodes will be objects which subclass PolyQuery:
86 ANDQuery, ORQuery, NOTQuery, and RequiredOptionalQuery.
87
88 The output of tree() is an intermediate form which must be passed
89 through expand() before being used to feed a search.
90
91 • query_string - The string to be parsed.
92
93 Returns: a Query.
94
95 expand
96 my $query = $query_parser->expand($query);
97
98 Walk the hierarchy of a Query tree, descending through all PolyQuery
99 nodes and calling expand_leaf() on any LeafQuery nodes encountered.
100
101 • query - A Query object.
102
103 Returns: A Query X usually the same one that was supplied after in-
104 place modification, but possibly another.
105
106 expand_leaf
107 my $query = $query_parser->expand_leaf($query);
108
109 Convert a LeafQuery into either a TermQuery, a PhraseQuery, or an
110 ORQuery joining multiple TermQueries/PhraseQueries to accommodate
111 multiple fields. LeafQuery text will be passed through the relevant
112 Analyzer for each field. Quoted text will be transformed into
113 PhraseQuery objects. Unquoted text will be converted to either a
114 TermQuery or a PhraseQuery depending on how many tokens are generated.
115
116 • query - A Query. Only LeafQuery objects will be processed; others
117 will be passed through.
118
119 Returns: A Query.
120
121 prune
122 my $query = $query_parser->prune($query);
123 my $query = $query_parser->prune(); # default: undef
124
125 Prevent certain Query structures from returning too many results.
126 Query objects built via tree() and expand() can generate Xreturn the
127 worldX result sets, such as in the case of "NOT
128 a_term_not_in_the_index"; prune() walks the hierarchy and eliminates
129 such branches.
130
131 'NOT foo' => [NOMATCH]
132 'foo OR NOT bar' => 'foo'
133 'foo OR (-bar AND -baz) => 'foo'
134
135 prune() also eliminates some double-negative constructs X even though
136 such constructs may not actually return the world:
137
138 'foo AND -(-bar)' => 'foo'
139
140 In this example, safety is taking precedence over logical consistency.
141 If you want logical consistency instead, call tree() then expand(),
142 skipping prune().
143
144 • query - A Query.
145
146 Returns: a Query; in most cases, the supplied Query after in-place
147 modification.
148
149 make_term_query
150 my $query = $query_parser->make_term_query(
151 field => $field, # required
152 term => $term, # required
153 );
154
155 Factory method creating a TermQuery.
156
157 • field - Field name.
158
159 • term - Term text.
160
161 Returns: A Query.
162
163 make_phrase_query
164 my $query = $query_parser->make_phrase_query(
165 field => $field, # required
166 terms => $terms, # required
167 );
168
169 Factory method creating a PhraseQuery.
170
171 • field - Field that the phrase must occur in.
172
173 • terms - Ordered array of terms that must match.
174
175 Returns: A Query.
176
177 make_or_query
178 my $query = $query_parser->make_or_query($children);
179 my $query = $query_parser->make_or_query(); # default: undef
180
181 Factory method creating an ORQuery.
182
183 • children - Array of child Queries.
184
185 Returns: A Query.
186
187 make_and_query
188 my $query = $query_parser->make_and_query($children);
189 my $query = $query_parser->make_and_query(); # default: undef
190
191 Factory method creating an ANDQuery.
192
193 • children - Array of child Queries.
194
195 Returns: A Query.
196
197 make_not_query
198 my $query = $query_parser->make_not_query($negated_query);
199
200 Factory method creating a NOTQuery.
201
202 • negated_query - Query to be inverted.
203
204 Returns: A Query.
205
206 make_req_opt_query
207 my $query = $query_parser->make_req_opt_query(
208 required_query => $required_query, # required
209 optional_query => $optional_query, # required
210 );
211
212 Factory method creating a RequiredOptionalQuery.
213
214 • required_query - Query must must match.
215
216 • optional_query - Query which should match.
217
218 Returns: A Query.
219
220 set_heed_colons
221 $query_parser->set_heed_colons($heed_colons);
222
223 Enable/disable parsing of "fieldname:foo" constructs.
224
226 Lucy::Search::QueryParser isa Clownfish::Obj.
227
228
229
230perl v5.32.1 2021-01-27 Lucy::Search::QueryParser(3)