1Lucy::Docs::Tutorial::BUesyeorndCSoinmtprliebLTuuutcteyod:r:iPDaeolrc(ls3:)D:oTcuutmoernitaalt:i:oBneyondSimpleTutorial(3)
2
3
4
6 Lucy::Docs::Tutorial::BeyondSimpleTutorial - A more flexible app
7 structure.
8
10 Goal
11 In this tutorial chapter, weXll refactor the apps we built in
12 SimpleTutorial so that they look exactly the same from the end userXs
13 point of view, but offer the developer greater possibilites for
14 expansion.
15
16 To achieve this, weXll ditch Lucy::Simple and replace it with the
17 classes that it uses internally:
18
19 • Schema - Plan out your index.
20
21 • FullTextType - Field type for full text search.
22
23 • EasyAnalyzer - A one-size-fits-all parser/tokenizer.
24
25 • Indexer - Manipulate index content.
26
27 • IndexSearcher - Search an index.
28
29 • Hits - Iterate over hits returned by a Searcher.
30
31 Adaptations to indexer.pl
32 After we load our modulesX
33
34 use Lucy::Plan::Schema;
35 use Lucy::Plan::FullTextType;
36 use Lucy::Analysis::EasyAnalyzer;
37 use Lucy::Index::Indexer;
38
39 X the first item weXre going need is a Schema.
40
41 The primary job of a Schema is to specify what fields are available and
42 how theyXre defined. WeXll start off with three fields: title, content
43 and url.
44
45 # Create Schema.
46 my $schema = Lucy::Plan::Schema->new;
47 my $easyanalyzer = Lucy::Analysis::EasyAnalyzer->new(
48 language => 'en',
49 );
50 my $type = Lucy::Plan::FullTextType->new(
51 analyzer => $easyanalyzer,
52 );
53 $schema->spec_field( name => 'title', type => $type );
54 $schema->spec_field( name => 'content', type => $type );
55 $schema->spec_field( name => 'url', type => $type );
56
57 All of the fields are specXd out using the FullTextType FieldType,
58 indicating that they will be searchable as Xfull textX X which means
59 that they can be searched for individual words. The XanalyzerX, which
60 is unique to FullTextType fields, is what breaks up the text into
61 searchable tokens.
62
63 Next, weXll swap our Lucy::Simple object out for an Indexer. The
64 substitution will be straightforward because Simple has merely been
65 serving as a thin wrapper around an inner Indexer, and weXll just be
66 peeling away the wrapper.
67
68 First, replace the constructor:
69
70 # Create Indexer.
71 my $indexer = Lucy::Index::Indexer->new(
72 index => $path_to_index,
73 schema => $schema,
74 create => 1,
75 truncate => 1,
76 );
77
78 Next, have the "indexer" object add_doc() where we were having the
79 "lucy" object adding the document before:
80
81 foreach my $filename (@filenames) {
82 my $doc = parse_file($filename);
83 $indexer->add_doc($doc);
84 }
85
86 ThereXs only one extra step required: at the end of the app, you must
87 call commit() explicitly to close the indexing session and commit your
88 changes. (Lucy::Simple hides this detail, calling commit() implicitly
89 when it needs to).
90
91 $indexer->commit;
92
93 Adaptations to search.cgi
94 In our search app as in our indexing app, Lucy::Simple has served as a
95 thin wrapper X this time around IndexSearcher and Hits. Swapping out
96 Simple for these two classes is also straightforward:
97
98 use Lucy::Search::IndexSearcher;
99
100 my $searcher = Lucy::Search::IndexSearcher->new(
101 index => $path_to_index,
102 );
103 my $hits = $searcher->hits( # returns a Hits object, not a hit count
104 query => $q,
105 offset => $offset,
106 num_wanted => $page_size,
107 );
108 my $hit_count = $hits->total_hits; # get the hit count here
109
110 ...
111
112 while ( my $hit = $hits->next ) {
113 ...
114 }
115
116 Hooray!
117 Congratulations! Your apps do the same thing as beforeX but now
118 theyXll be easier to customize.
119
120 In our next chapter, FieldTypeTutorial, weXll explore how to assign
121 different behaviors to different fields.
122
123
124
125perl v5.32.1 L2u0c2y1:-:0D1o-c2s7::Tutorial::BeyondSimpleTutorial(3)