KinoSearch::Docs::Tutorial::QueryObjects man page on Fedora

Man page or keyword search:  
man Server   31170 pages
apropos Keyword Search (all sections)
Output format
Fedora logo
[printable version]

KinoSearch::Docs::TutoUser:ContribuKinoSearch::Docs::Tutorial::QueryObjects(3)

NAME
       KinoSearch::Docs::Tutorial::QueryObjects - Use Query objects instead of
       query strings.

DESCRIPTION
       Until now, our search app has had only a single search box.  In this
       tutorial chapter, we'll move towards an "advanced search" interface, by
       adding a "category" drop-down menu.  Three new classes will be
       required:

       ·   QueryParser - Turn a query string into a Query object.

       ·   TermQuery - Query for a specific term within a specific field.

       ·   ANDQuery - "AND" together multiple Query objects to produce an
	   intersected result set.

   Adaptations to indexer.pl
       Our new "category" field will be a StringType field rather than a
       FullTextType field, because we will only be looking for exact matches.
       It needs to be indexed, but since we won't display its value, it
       doesn't need to be stored.

	   my $cat_type = KinoSearch::Plan::StringType->new( stored => 0 );
	   $schema->spec_field( name => 'category', type => $cat_type );

       There will be three possible values: "article", "amendment", and
       "preamble", which we'll hack out of the HTML source file's name during
       our "parse_file" subroutine:

	   my $category
	       = $filename =~ /art/	 ? 'article'
	       : $filename =~ /amend/	 ? 'amendment'
	       : $filename =~ /preamble/ ? 'preamble'
	       :			   die "Can't derive category for $filename";
	   return {
	       title	=> $title_node->as_trimmed_text,
	       content	=> $bodytext_node->as_trimmed_text,
	       url	=> "/us_constitution/$filename",
	       category => $category,
	   };

   Adaptations to search.cgi
       The "category" constraint will be added to our search interface using
       an HTML "select" element:

	   # Build up the HTML "select" object for the "category" field.
	   sub generate_category_select {
	       my $cat = shift;
	       my $select = qq|
		 <select name="category">
		   <option value="">All Sections</option>
		   <option value="article">Articles</option>
		   <option value="amendment">Amendments</option>
		 </select>|;
	       if ($cat) {
		   $select =~ s/"$cat"/"$cat" selected/;
	       }
	       return $select;
	   }

       We'll start off by loading our new modules and extracting our new CGI
       parameter.

	   use KinoSearch::Search::QueryParser;
	   use KinoSearch::Search::TermQuery;
	   use KinoSearch::Search::ANDQuery;

	   ...

	   my $category = $cgi->param('category') || '';

       QueryParser's constructor requires a "schema" argument.	We can get
       that from our IndexSearcher:

	   # Create an IndexSearcher and a QueryParser.
	   my $searcher = KinoSearch::Search::IndexSearcher->new(
	       index => $path_to_index,
	   );
	   my $qparser	= KinoSearch::Search::QueryParser->new(
	       schema => $searcher->get_schema,
	   );

       Previously, we have been handing raw query strings to IndexSearcher.
       Behind the scenes, IndexSearcher has been using a QueryParser to turn
       those query strings into Query objects.	Now, we will bring QueryParser
       into the foreground and parse the strings explicitly.

	   my $query = $qparser->parse($q);

       If the user has specified a category, we'll use an ANDQuery to join our
       parsed query together with a TermQuery representing the category.

	   if ($category) {
	       my $category_query = KinoSearch::Search::TermQuery->new(
		   field => 'category',
		   term	 => $category,
	       );
	       $query = KinoSearch::Search::ANDQuery->new(
		   children => [ $query, $category_query ]
	       );
	   }

       Now when we execute the query...

	   # Execute the Query and get a Hits object.
	   my $hits = $searcher->hits(
	       query	  => $query,
	       offset	  => $offset,
	       num_wanted => $hits_per_page,
	   );

       ... we'll get a result set which is the intersection of the parsed
       query and the category query.

Congratulations!
       You've made it to the end of the tutorial.

SEE ALSO
       For additional thematic documentation, see the KinoSearch Cookbook.

       ANDQuery has a companion class, ORQuery, and a close relative,
       RequiredOptionalQuery.

COPYRIGHT AND LICENSE
       Copyright 2008-2010 Marvin Humphrey

       This program is free software; you can redistribute it and/or modify it
       under the same terms as Perl itself.

perl v5.14.1			  2KinoSearch::Docs::Tutorial::QueryObjects(3)
[top]

List of man pages available for Fedora

Copyright (c) for man pages and the logo by the respective OS vendor.

For those who want to learn more, the polarhome community provides shell access and support.

[legal] [privacy] [GNU] [policy] [cookies] [netiquette] [sponsors] [FAQ]
Tweet
Polarhome, production since 1999.
Member of Polarhome portal.
Based on Fawad Halim's script.
....................................................................
Vote for polarhome
Free Shell Accounts :: the biggest list on the net